android CustomView

时间:2013-03-07 10:35:25

标签: android listview android-listview

我在解析webservice响应后获得了一个列表。我使用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);

2 个答案:

答案 0 :(得分:0)

使用以下代码替换您的代码:请参阅magic ...

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;

     }
}

答案 1 :(得分:0)

看看getCount()方法:

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

}

解决方案是返回适配器包含的完整项的计数。 你应该知道你必须实现的任何方法!不仅让eclipse生成方法。它们只是存根。如果您不知道如何在方法中执行操作,那么开发人员文档会为您提供一个更好的视图。