How to create dynamic List View for displaying texts and image in a row of a list in android?
I need to create a dynamic list view which displays images and texts from the database. The number of rows in the list depends upon the number of rows in the database table. And I need the list to be presented as image, text and a button for deleting or editing each rows in the list.
Please find me a right method to do this. I have gone through many tu开发者_JAVA百科torials but I could not find a dynamic list view creation from database which displays images, texts and buttons. Please help.
Thanks
You can create dynamic listview in android by using BaseAdapter class which is available in android. You can create one class which extends BaseAdapter. You need to create two xml files. One for ListView and second for contents which you display in your ListView.
For example:
private class ListAdapter extends BaseAdapter
{
LayoutInflater inflater;
ViewHolder viewHolder; // your private class ViewHolder please see at bottom
public ListAdapter(Context context)
{
inflater = LayoutInflater.from(context);
}
public int getCount()
{
return _empList.size(); // public ArrayList<Emp_detail> _empList = new ArrayList<Emp_detail>();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent)
{
if (convertView == null) {
convertView = inflater.inflate(R.layout.emp_list_row, null); // Your second xml file for contents in ListView
viewHolder = new ViewHolder();
viewHolder.txt_idno = (TextView) convertView
.findViewById(R.id.txt_idno);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_idno
.setText(_empList.get(position).getIDNO().trim());
return convertView;
}
}
private class ViewHolder {
TextView txt_idno;
}
精彩评论