0

基本适配器代码

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;

}
}

主类代码

 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);       
    }

}

但名单没有来。我找不到任何问题。请帮帮我。

4

1 回答 1

1

您返回的计数为零。

public int getCount() {
    // TODO Auto-generated method stub
    list.size();
    return 0;
}

你有回报list.size()。改成这样

public int getCount() {
    // TODO Auto-generated method stub

    return  list.size();
}

getCount() 是通知适配器必须膨胀的元素数量的方法。当您返回零时,这意味着不存在任何元素,因此您无法看到您的列表视图。

于 2012-06-07T11:03:17.460 回答