Populate Listview From Arraylist Of Objects
I have a listactivity which will display a list of persons name and address with data from arraylist of objects. here's the method to fill the listview so far.. private void fillDa
Solution 1:
In your activity
AdapterPerson adbPerson;
ArrayList<Person> myListItems = newArrayList<Person>();
//then populate myListItems
adbPerson= newAdapterPerson (youractivity.this, 0, myListItems);
listview.setAdapter(adbPerson);
Adapter
publicclassAdapterPersonextendsArrayAdapter<Person> {
private Activity activity;
private ArrayList<Person> lPerson;
privatestaticLayoutInflaterinflater=null;
publicAdapterPerson(Activity activity, int textViewResourceId,ArrayList<Person> _lPerson) {
super(activity, textViewResourceId, _lProducts);
try {
this.activity = activity;
this.lPerson = _lPerson;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} catch (Exception e) {
}
}
publicintgetCount() {
return lPerson.size();
}
public Product getItem(Product position) {
return position;
}
publiclonggetItemId(int position) {
return position;
}
publicstaticclassViewHolder {
public TextView display_name;
public TextView display_number;
}
public View getView(int position, View convertView, ViewGroup parent) {
Viewvi= convertView;
final ViewHolder holder;
try {
if (convertView == null) {
vi = inflater.inflate(R.layout.yourlayout, null);
holder = newViewHolder();
holder.display_name = (TextView) vi.findViewById(R.id.display_name);
holder.display_number = (TextView) vi.findViewById(R.id.display_number);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.display_name.setText(lProducts.get(position).name);
holder.display_number.setText(lProducts.get(position).number);
} catch (Exception e) {
}
return vi;
}
}
Solution 2:
This is my solution to the custom list adapter problem. First a custom arrayadapter:
publicclassMemoListAdapterextendsArrayAdapter<MemoEntry> {
privateint layoutResourceId;
privatestaticfinalStringLOG_TAG="MemoListAdapter";
publicMemoListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
layoutResourceId = textViewResourceId;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
try {
MemoEntryitem= getItem(position);
Viewv=null;
if (convertView == null) {
LayoutInflaterinflater= (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(layoutResourceId, null);
} else {
v = convertView;
}
TextViewheader= (TextView) v.findViewById(R.id.list_memo_item_header);
TextViewdescription= (TextView) v.findViewById(R.id.list_memo_item_text);
header.setText(item.getHeader());
description.setText(item.getValue());
return v;
} catch (Exception ex) {
Log.e(LOG_TAG, "error", ex);
returnnull;
}
}
}
and in the onCreate method of the activity:
adapter = newMemoListAdapter(this, R.layout.list_memo_item_layout); // the adapter is a member field in the activity
setContentView(R.layout.activity_view_memo_layout);
ListViewlv= (ListView) findViewById(R.id.view_memo_memo_list);
lv.setAdapter(adapter);
and then i fill the adapter after some fetching with a call like this:
ArrayList<MemoEntry> memoList = new ArrayList<MemoEntry>(); //here you should use a list with some content in it
adapter.addAll(memoList);
So to adapt this to your solution, create your own customadapter, and instead of my object MemoEntry, use your Person class. In the getView method, change it to suit your needs. It's about the same as what i'm doing so shouldn't be too hard.
Hope this helps you a bit!
Post a Comment for "Populate Listview From Arraylist Of Objects"