T
theAydinator
Neues Mitglied
- 0
Hallo lieber Forum,
bin neu hier und ich hoffe und wünsche mir,
dass ich hier dazulerne.
In meinem Skript versuche ich eine XML Datei (res/xml) mit Hilfe eines custom Adapters in eine Liste zu laden.
EmployeeXmlParser.java
ListViewAdapter.java
MainActivity.java
Wie man in der MainActivity sehen kann, habe ich den ArrayAdapter kommentiert nachdem es problemlos lief und wollte mit dem custom adapter versuchen, welches gescheitert ist.
Bittgebete.java
ICH WÜRDE MICH ÜBER JEGLICHE HILFE SEHR FREUEN
Danke schonmal im Vorraus
Grüße
Aydin
Der ursprüngliche Beitrag von 22:38 Uhr wurde um 22:41 Uhr ergänzt:
main.xml
list_item.xml
bin neu hier und ich hoffe und wünsche mir,
dass ich hier dazulerne.
In meinem Skript versuche ich eine XML Datei (res/xml) mit Hilfe eines custom Adapters in eine Liste zu laden.
EmployeeXmlParser.java
Code:
package de.jfd.xmlparsing;
import java.io.IOException;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
public class EmployeeXmlParser {
// names of the XML tags
static final String BITTGEBETE = "bittgebete";
static final String BITTGEBET = "bittgebet";
static final String ID = "id";
static final String NAME = "name";
static final String AR = "ar";
static final String DE = "de";
static final String QUELLE = "quelle";
ArrayList<Bittgebete> bittgebeteList = null;
private Bittgebete currentBittgebet = null;
private boolean done = false;
private String currentTag = null;
public ArrayList<Bittgebete> parse(Context context) {
XmlPullParser parser = context.getResources().getXml(R.xml.hisnulmuslim);
try {
int eventType = parser.getEventType();
// Following logic modified from http://www.ibm.com/developerworks/library/x-android/
// Also look at http://developer.android.com/training/basics/network-ops/xml.html
while (eventType != XmlPullParser.END_DOCUMENT && !done) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
bittgebeteList = new ArrayList<Bittgebete>();
break;
case XmlPullParser.START_TAG:
currentTag = parser.getName();
if (currentTag.equalsIgnoreCase(BITTGEBET)) {
currentBittgebet = new Bittgebete();
} else if (currentBittgebet != null) {
if (currentTag.equalsIgnoreCase(ID)) {
currentBittgebet.setId(Integer.parseInt(parser.nextText()));
// currentBittgebet.setId(parser.nextText());
} else if (currentTag.equalsIgnoreCase(NAME)) {
currentBittgebet.setname(parser.nextText());
} else if (currentTag.equalsIgnoreCase(AR)) {
currentBittgebet.setar(parser.nextText());
} else if (currentTag.equalsIgnoreCase(DE)) {
currentBittgebet.setde(parser.nextText());
} else if (currentTag.equalsIgnoreCase(QUELLE)) {
currentBittgebet.setquelle(parser.nextText());
}
}
break;
case XmlPullParser.END_TAG:
currentTag = parser.getName();
if (currentTag.equalsIgnoreCase(BITTGEBET) && currentBittgebet != null) {
bittgebeteList.add(currentBittgebet);
} else if (currentTag.equalsIgnoreCase(BITTGEBETE)) {
done = true;
}
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bittgebeteList;
}
}
ListViewAdapter.java
Code:
package de.jfd.xmlparsing;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
private List<Bittgebete> bittgebete = null;
private ArrayList<Bittgebete> arraylist;
public ListViewAdapter(Context context,
List<Bittgebete> bittgebete) {
this.context = context;
this.bittgebete = bittgebete;
inflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<Bittgebete>();
this.arraylist.addAll(bittgebete);
}
public class ViewHolder {
TextView id;
TextView name;
TextView ar;
TextView de;
TextView quelle;
}
@Override
public int getCount() {
return bittgebete.size();
}
@Override
public Object getItem(int position) {
return bittgebete.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.list_item, null);
// Locate the TextViews in listview_item.xml
holder.id = (TextView) view.findViewById(R.id.id);
holder.name = (TextView) view.findViewById(R.id.name);
holder.ar = (TextView) view.findViewById(R.id.ar);
holder.de = (TextView) view.findViewById(R.id.de);
holder.quelle = (TextView) view.findViewById(R.id.quelle);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.id.setText(bittgebete.get(position).getId());
holder.name.setText(bittgebete.get(position).getname());
holder.ar.setText(bittgebete.get(position).getar());
holder.de.setText(bittgebete.get(position).getde());
holder.quelle.setText(bittgebete.get(position).getquelle());
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(context, SingleItemView.class);
intent.putExtra("id",(bittgebete.get(position).getId()));
intent.putExtra("name",(bittgebete.get(position).getname()));
intent.putExtra("ar",(bittgebete.get(position).getar()));
intent.putExtra("de",(bittgebete.get(position).getde()));
intent.putExtra("quelle",(bittgebete.get(position).getquelle()));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
bittgebete.clear();
if (charText.length() == 0) {
bittgebete.addAll(arraylist);
} else {
for (Bittgebete wp : arraylist) {
if (wp.getname().toLowerCase(Locale.getDefault())
.contains(charText)) {
bittgebete.add(wp);
}
}
}
notifyDataSetChanged();
}
}
MainActivity.java
Code:
package de.jfd.xmlparsing;
import java.util.List;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
// GOAL: Build a native android Mobile Employee Directory
// ** The result is similar to the sample with Flex and Flash Builder
// see http://www.adobe.com/devnet/flex/articles/employee-directory-android-flex.html
// PURPOSE: Learning how to build an Android App.
// Step 2: Load data into the ListActivity from an XML file via XmlParser
// 1) employee_list.xml (in res/xml)
// 2) Employee.java
// 3) EmployeeXmlParser.java
public class MainActivity extends ListActivity {
public List<Bittgebete> bittgebete = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* This will work, but best practice is to place in a non-ui thread like below.
*
* EmployeeXmlParser parser = new EmployeeXmlParser();
* bittgebete = parser.parse(this);
*
* ArrayAdapter<Employee> adapter = new ArrayAdapter<Employee>(this, android.R.layout.simple_list_item_1, bittgebete);
* setListAdapter(adapter);
*/
// Parse xml data in a non-ui thread
new LoadbittgebeteTask().execute();
}
private class LoadbittgebeteTask extends AsyncTask<String, Void, List<Bittgebete>> {
@Override
protected List<Bittgebete> doInBackground(String... args) {
// CALL XMLPULLPARSER & RETURN A LIST
EmployeeXmlParser parser = new EmployeeXmlParser();
bittgebete = parser.parse(getBaseContext());
return bittgebete;
}
@Override
protected void onPostExecute(List<Bittgebete> bittgebete) {
//ArrayAdapter<Bittgebete> adapter = new ArrayAdapter<Bittgebete>(getBaseContext(), android.R.layout.simple_list_item_1, bittgebete);
ListViewAdapter adapter = new ListViewAdapter(MainActivity.this, bittgebete);
setListAdapter(adapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Wie man in der MainActivity sehen kann, habe ich den ArrayAdapter kommentiert nachdem es problemlos lief und wollte mit dem custom adapter versuchen, welches gescheitert ist.
Bittgebete.java
Code:
package de.jfd.xmlparsing;
public class Bittgebete {
Integer _id;
String _name;
String _ar;
String _de;
String _quelle;
// constructor
public Bittgebete() {
}
// constructor with parameters
public Bittgebete(Integer id, String name, String ar, String de, String quelle) {
this._id = id;
this._name = name;
this._ar = ar;
this._de = de;
this._quelle = quelle;
}
// All set methods
public void setId(Integer id) {
this._id = id;
}
public void setname(String name) {
this._name = name;
}
public void setar(String ar) {
this._ar = ar;
}
public void setde(String de) {
this._de = de;
}
public void setquelle(String quelle) {
this._quelle = quelle;
}
// All get methods
public Integer getId() {
return this._id;
}
public String getname() {
return this._name;
}
public String getar() {
return this._ar;
}
public String getde() {
return this._de;
}
public String getquelle() {
return this._quelle;
}
//
@Override
public String toString() {
return _name + " " + _ar + "\n" + _de;
}
}
ICH WÜRDE MICH ÜBER JEGLICHE HILFE SEHR FREUEN
Danke schonmal im Vorraus
Grüße
Aydin
Der ursprüngliche Beitrag von 22:38 Uhr wurde um 22:41 Uhr ergänzt:
main.xml
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
list_item.xml
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="@+id/de"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:textSize="0sp"
android:padding="0dp" />
<TextView
android:id="@+id/ar"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:textSize="0sp"
android:padding="0dp" />
<TextView
android:id="@+id/quelle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:textSize="0sp"
android:padding="0dp" />
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="10dp"
android:gravity="center_vertical" />
</LinearLayout>