为什么android mycustom适配器无法正常工作

时间:2014-08-30 07:53:38

标签: java android baseadapter

mycustom适配器从baseadapter扩展但是当我运行我的主java文件时它显示主要活动号 列表视图项

  public class MyCustomadapter extends BaseAdapter{
    private Context mycontext;
    String[] mystringlist;

    public MyCustomadapter(Context context,String[] strText) {

        this.mycontext=context;
        this.mystringlist = strText;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mystringlist.length;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        LayoutInflater inflater = (LayoutInflater) mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.each_row, parent,false);
        TextView tv = (TextView) rowView.findViewById(R.id.rowtxt);
        tv.setText(mystringlist[position]);
        return rowView;
    }

}

mycustom适配器从baseadapter扩展但是当我运行我的主java文件时它显示主要活动号 列表视图项

4 个答案:

答案 0 :(得分:3)

<强>问题:

return 0;

您返回0意味着您的ListView中不会显示任何列表项,从而得到0结果。 您需要返回number of item in your array长度

<强>样品:

 @Override
public int getCount() {
    // TODO Auto-generated method stub
    return mystringlist.length;
}

答案 1 :(得分:1)

@Override
public int getCount() 
 {
    // TODO Auto-generated method stub
    return mystringlist.length();
 }

答案 2 :(得分:1)

我的想法是扩展ArrayAdapter,请看下面的代码

public class MyAdapter extends ArrayAdapter<String>{
    public MyAdapter(Context context, String[] items) {
        super(context, 0, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // your codes for generating rows
        String thisItem = getItem(position);
    }
}

您已完成,您也不再需要覆盖其他方法!

答案 3 :(得分:0)

作为附录,您的getItem()方法不正确。它应如下所示:

@Override
public Object getItem(int arg0) {
    return mystringlist[position];
}