I need to display a list of categories from a ListActivity. My data source is a ArrayList of type Category which is implementing the android Parcelable interface. stucture is given at the bottom. it has an id and a title
I need title to be displayed as list text and on clicking the list item, i need to get the id for further processing
It is possible to create a new array of ‘title’s by iterating the category ArrayList and use
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.label, names));
But bu using this method i will get only the title of the category upon click and not the id.
Is it possible to directly use the ArrayList of objects as data source?
public class CategoryList extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent t = new Intent(this, ParsingWebXML.class);
startActivityForResult(t,1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1 && resultCode == RESULT_CANCELED){
finish();
} else {
createCategoryList(data);
}
}
private void createCategoryList(Intent data) {
ArrayList<Category> categories = null;
try {
categories = data.getParcelableArrayListExtra("categoryList");
Iterator<Category> itr = categories.iterator();
String[] names = new String[categories.size()];
int i=0;
while (itr.hasNext()) {
Category c = itr.next();
names[i++] = c.getTitle();
}
this.setListAdapter(new ArrayAdapter<Category>(this, R.layout.rowlayout, R.id.label, categories));
// String[] names = new String[] { "Sports", "Mediacal", "Computers" };
// this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.label, names));
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
if(keyword=="Categories"){
Intent cat = new Intent(this, CategoryActivity.class);
startActivity(cat);
//Toast.makeText(this, keyword, Toast.LENGTH_LONG).show();
} else {
Intent cat = new Intent(this, CategoryActivity.class);
startActivity(cat);
}
}
}
Category.java
public class Category implements Parcelable {
private int id;
private String title;
public Category() {
}
public Category(Parcel source) {
id = source.readInt();
title = source.readString();
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(title);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
@Override
public Category createFromParcel(Parcel source) {
return new Category(source);
}
@Override
public Category[] newArray(int size) {
return new Category[size];
// TODO Auto-generated method stub
}
};
}
Here are two good examples of how to make your custom implementation of the
ArrayAdapterclass: