base adapter code
public class ListAdapter extends BaseAdapter{
public ArrayList<HashMap<String,String>> list;
Activity activity;
public ListAdapter(Activity activity, ArrayList<HashMap<String, String>>list ) {
// TODO Auto-generated constructor stub
super();
this.activity=(Activity)activity;
this.list=list;
for(int i=0;i<3;i++)
{
HashMap<String, String>temp=(HashMap<String, String>)list.get(i);
System.out.println(temp.get("message"));
System.out.println(temp.get("from_whom"));
}
}
public int getCount() {
// TODO Auto-generated method stub
list.size();
return 0;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView Message;
TextView fromWhom;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.custom_list_view, null);
holder = new ViewHolder();
holder.Message = (TextView) convertView.findViewById(R.id.message);
holder.fromWhom = (TextView) convertView.findViewById(R.id.fromWhom);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> item = list.get(position);
String message = item.get("message");
String from_whom = item.get("from_whom");
holder.Message.setText(message);
holder.fromWhom.setText(from_whom);
return convertView;
}
}
main class code
public class go_on extends Activity
{
ListView list;
ArrayList<String> nlist;
ArrayList<HashMap<String, String>> Hlist;
HashMap<String, String> temp;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.mylist);
Hlist = new ArrayList<HashMap<String,String>>();
for(int i=0;i<3;i++)
{
temp= new HashMap<String, String>();
temp.put("message","I am fine");
temp.put("from_whom", "123");
Hlist.add(temp);
}
ListAdapter listAdapter = new ListAdapter(this, Hlist);
list.setAdapter(listAdapter);
}
}
But list does not come . I could not find any problem. Please help me out.
You are returning zero in count.
You have return
list.size(). Change it lke this,getCount() is the method which informs your Adapter about the number of elements that has to be inflated. When you return zero it means that there is no elements present and hence you are unable to see your listview.