Archive for February, 2012
Einfaches XML Parsen in Android mit SimpleXML (XML Parsing)
Einfaches XML Parsen in Android mit SimpleXML
Erstmal müssen wir den .jar runterladen: http://simple.sourceforge.net/download.php
Dan geben wir ihn in den Buil Path ein:
![]()
Im Manifest muss die Permission sein:
<uses-permission android:name="android.permission.INTERNET" />
Die einfache XML die wir parsen:
<data> <name>Example Name</name> </data>
Hier unser Ergebniss:

…und der Code:
Data.java
package de.worldling;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root
public class Data {
public Data(){
super();
}
public Data(String name){
this.name = name;
}
@Element
private String name;
public String getName() {
return name;
}
}
SimpleParsingTestActivity.java
package de.worldling;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class SimpleParsingTestActivity extends Activity {
private static final String url = "http://www.worldling.de/data.xml";
TextView name;
private DefaultHttpClient client = new DefaultHttpClient();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (TextView) findViewById(R.id.tvForParsing);
parseXml();
}
private void parseXml() {
try {
String xmlData = retrieve(url);
Serializer serializer = new Persister();
Reader reader = new StringReader(xmlData);
Data user = serializer.read(Data.class, reader);
String tempName = user.getName();
name.setText(tempName);
} catch (Exception e) {
Toast.makeText(this, "Error Occured", Toast.LENGTH_LONG).show();
}
}
public String retrieve(String url) {
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
if (getResponseEntity != null) {
return EntityUtils.toString(getResponseEntity);
}
} catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
}
SharedPreferences in Android
SharedPreferences in Android
Heute ohne xml, nur Java Code für eine einfache SharedPreferences App
package de.worldling;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class PreferencesTestActivity extends Activity implements OnClickListener{
EditText editText;
SharedPreferences sharedPreferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupStuff();
sharedPreferences = getSharedPreferences("SharedString1", 0);
}
private void setupStuff() {
Button next = (Button) findViewById(R.id.bNext);
Button save = (Button) findViewById(R.id.bSave);
editText = (EditText) findViewById(R.id.et1);
next.setOnClickListener(this);
save.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.bSave:
String string = editText.getText().toString();//set up string to save
Editor editor = sharedPreferences.edit(); //edit the SharedPreferences 'sharedPreferences'
editor.putString("string1", string); //put the string and lable it as "string1"
editor.commit(); //commit it
break;
case R.id.bNext:
Intent intent = new Intent(PreferencesTestActivity.this, NewClass.class);
startActivity(intent);
break;
}
}
}
package de.worldling;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class NewClass extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
TextView tv2 = (TextView) findViewById(R.id.tv2);
SharedPreferences someData = getSharedPreferences("SharedString1", 0);
String dataReturned = someData.getString("string1", "default");
tv2.setText(dataReturned);
}
}
Auf Preferences in Android Zugreifen
Auf Preferences in Android Zugreifen
private TextView textView;
textView = (TextView) findViewById(R.id.tv1);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String string = sharedPreferences.getString("thename", "");
textView.setText(string);


Android Preferences setzen mit Menu und PreferenceActivity
Android Preferences setzen mit Menu und PreferenceActivity
Es müssen 3 .xml Dateien erschaffen werden: menu1.xml, array.xml und prefs1.xml

menu1.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/preferences" android:title="Preferences"/> <item android:id="@+id/exit" android:title="Exit App"/> </menu>

array.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="list"> <item> Option 1 </item> <item> Option 2 </item> </string-array> <string-array name="values"> <item>1</item> <item>2</item> </string-array> </resources>
prefs1.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <EditTextPreference android:key="The name" android:summary="Your name:" android:title="Name" /> <ListPreference android:entries="@array/list" android:entryValues="@array/values" android:key="List Pref1" android:summary="The List Preference" android:title="List" /> </PreferenceScreen>


Der Java Code:
PrefTestActivity.java
package de.worldling;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
public class PrefTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.preferences:
Intent intent = new Intent(PrefTestActivity.this, Prefs1.class);
startActivity(intent);
break;
case R.id.exit:
finish();
break;
}
return false;
}
}
Prefs1.java
package de.worldling;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs1 extends PreferenceActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs1);
}
}
Android – Parameters passing zu einer anderen Activity
Android – Parameters passing zu einer anderen Activity
Eine App in der man mit Button Click einen Parameter zu einer anderen Activity weiter recht.


main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:padding="40dp" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/etPar" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText> <Button android:id="@+id/bPar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=" Pass the parameter" /> </LinearLayout>
new_lay.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="40dp"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your parameter: " /> <TextView android:id="@+id/tvPar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00FFFF" android:text="..." /> </LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.worldling" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ParActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NewClass" android:label="@string/app_name" > > </activity> </application> </manifest>
ParActivity.java
package de.worldling;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ParActivity extends Activity implements OnClickListener{
EditText editText;
Button button;
String string;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initStuff();
}
private void initStuff() {
editText = (EditText) findViewById(R.id.etPar);
button = (Button) findViewById(R.id.bPar);
button.setOnClickListener(this);
}
public void onClick(View v) {
string = editText.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("key1", string);
Intent intent = new Intent(ParActivity.this, NewClass.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
NewClass.java
package de.worldling;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class NewClass extends Activity{
TextView textView;
String string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_lay);
textView = (TextView) findViewById(R.id.tvPar);
Bundle bundle = getIntent().getExtras();
string = bundle.getString("key1");
textView.setText(string);
}
}
