简体   繁体   English

android CustomView

[英]android CustomView

I have a list obtained after parsing the webservice response. 解析Web服务响应后,我得到了一个列表。 I have created one customview using BaseAdapter . 我已经使用BaseAdapter创建了一个customview。 The problem I am facing is I am not able to bind the data to the ListView using the customview I have created. 我面临的问题是我无法使用创建的customview 将数据绑定到ListView Here is my custom view: 这是我的自定义视图:

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

this is how I am trying to bind: 这就是我试图绑定的方式:

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

replace you code with below code : see 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;

     }
}

Just have a look at the getCount() method: 看看getCount()方法:

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

}

the solution is to return the count of the complete items, the adapter contains. 解决方案是返回适配器包含的完整项目的数量。 You should be aware of any methods you have to implement! 您应该知道必须实现的任何方法! Not only let eclipse generate the methods. 不仅让eclipse生成方法。 they are only stubs then. 他们只是那时的存根。 If you don't know what to do in the methods, the developer documentation gives you a greate view on that. 如果您不知道在方法中要做什么,则开发人员文档可为您提供绝佳的视角。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM