1

我在解析 web 服务响应后获得了一个列表。我已经使用BaseAdapter. 我面临的问题是我无法ListView使用我创建的自定义视图将数据绑定到。这是我的自定义视图:

public class ItemListBaseAdapter extends BaseAdapter {

    int layoutResourceId;  

    private static List<Item> itemDetailsarrayList;


    private LayoutInflater l_Inflater;

    public ItemListBaseAdapter(Context context, List<Item> results) {
        itemDetailsarrayList = results;
        l_Inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.row_layout, null);
            holder = new ViewHolder();
            holder.txtTitle = (TextView) convertView.findViewById(R.id.textView1);         
        convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtTitle.setText(itemDetailsarrayList.get(position).getCity_name());
        return convertView;
     }

     static class ViewHolder {   
        TextView txtTitle;
     }
}

这就是我试图绑定的方式:

llstCities = Objparsecities.parse();
ItemListBaseAdapter adapter = new ItemListBaseAdapter(this, lstCities);
lstcities = (ListView)findViewById(R.id.listView1);
lstcities.setAdapter(adapter);
4

2 回答 2

0

用下面的代码替换你的代码:看看魔法......

public class ItemListBaseAdapter extends BaseAdapter {

     int layoutResourceId;  

    private static List<Item> itemDetailsarrayList;


    private LayoutInflater l_Inflater;

     public ItemListBaseAdapter(Context context, List<Item> results) {
      itemDetailsarrayList = results;
      l_Inflater = LayoutInflater.from(context);
     }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemDetailsarrayList.size();


    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         ViewHolder holder;
          if (convertView == null) {
           convertView = l_Inflater.inflate(R.layout.row_layout, null);
           holder = new ViewHolder();
           holder.txtTitle = (TextView) convertView.findViewById(R.id.textView1);          
           convertView.setTag(holder);
          } else {
           holder = (ViewHolder) convertView.getTag();
          }

          holder.txtTitle.setText(itemDetailsarrayList.get(position).getCity_name());
          return convertView;
     }

     static class ViewHolder {

         TextView txtTitle;

     }
}
于 2013-03-07T10:39:23.200 回答
0

看看 getCount() 方法:

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

}

解决方案是返回适配器包含的完整项目的计数。你应该知道你必须实现的任何方法!不仅让eclipse生成方法。他们只是存根。如果您不知道在这些方法中做什么,开发者文档会为您提供更好的视角。

于 2013-03-07T11:02:02.757 回答