I am interested in creating a generic BaseAdapter which will apply to any ListView having any list_item . And it will set the item_row for the list_view.
public class GenericAdapter extends BaseAdapter
{
Context context;
ArrayList<HashMap<String, String>> list;
LayoutInflater inflater;
public GenericAdapter (Context context, ArrayList<HashMap<String, String>> list)
{
this.context = context;
this.list = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int position)
{
return list.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(view == null)
{
view = inflater.inflate(R.layout.item_of_any_list, parent, false);
// i have to do something here like there may be a method which must be call
}
/*HashMap<String, String> map = list.get(position);
TextView textViewName = (TextView) view.findViewById(R.id.name);
TextView textViewDate = (TextView) view.findViewById(R.id.date);
//TextView textViewDesc = (TextView) view.findViewById(R.id.desc);
textViewName.setText(map.get("name"));
textViewDate.setText(map.get("date"));
//textViewDesc.setText(map.get("description"));
*/
return view;
}
I think i have to create an interface that's get and set the item_row for individual item. But i don't know it may be possible . Can someone tell me how i can achieve it.
My idea is to create an interface and GenericAdapter implements that interface. In that interface there is a method to get and set the item_row. Any class which extends GenericAdapter must implement that method. it will help us make a generic adapter.
When we extend that adapter we don't need to write all code like getCount, getItem,getItemId, we just have to override a function which is to inflateLayout. which must be inside getView
Thanks in Advance