Pages

Tuesday, March 8, 2011

ListView in android

Listadapter
@Override
public int getCount() {
// TODO Auto-generated method stub
return lists.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return lists.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ListItems listItems = lists.get(position);
LayoutInflater layoutInflater= LayoutInflater.from(context);
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.listitem, null);
}

TextView textView =(TextView)convertView.findViewById(R.id.textView1);
textView.setText(listItems.getName());

return convertView;
}

}

ListActivity

list = new ArrayList..ListItems...();

for(int i=0;i<=50;i++){
ListItems listItems= new ListItems();
listItems.setName("justin"+i);
list.add(listItems);
}

ListAdapter listAdapter = new ListAdapter(list, ListActivity.this);
ListView listView= (ListView)findViewById(R.id.listView1);
listView.setAdapter(listAdapter);



}