为什么我无法在适配器中接收数据

时间:2013-12-17 09:35:05

标签: java android listview android-arrayadapter

此代码在我的MainActivity.java中,我可以从这里打印log cat中的标题

feedsDataArray = feedsData.toArray(feedsDataArray);
        for(int i = 0 ; i < feedsDataArray.length ; i++){
            Log.v("Array",feedsDataArray[i].getTitle());
        }

listview = (ListView) findViewById(R.id.list);
feedsAdapter = new FeedsAdapter(NewsFeedsActivity.this, R.layout.feeds_list,
            feedsDataArray);
listview.setAdapter(feedsAdapter);

但是当我将它传递给适配器时,我无法获得数据

public class FeedsAdapter extends ArrayAdapter<FeedsItems>{

    Context context;
    int id;
    FeedsItems [] dataArray;

    public FeedsAdapter(Context context, int id,FeedsItems [] dataArray) {
        super(context,id);

        this.context = context;
        this.id = id;
        this.dataArray = dataArray;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        FeedsHolder holder = null;

        if (row == null) {                  

            LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
            row = inflater.inflate(id, parent, false);
            holder = new FeedsHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.feeds_image);
            holder.txtTitle = (TextView) row.findViewById(R.id.feeds_title);

            row.setTag(holder);
        }
        else{
            holder = (FeedsHolder)row.getTag();
        }

        //holder.imgIcon.setImageResource();
        holder.txtTitle.setText(data.get(position).getTitle());
        Log.v("Title"+position,dataArray[position].getTitle());

        return row;
    }

    static class FeedsHolder{
        ImageView imgIcon;
        TextView txtTitle;
    }


}

任何建议或帮助将不胜感激。日Thnx

2 个答案:

答案 0 :(得分:4)

您必须覆盖getCount并让它返回dataArray.lenght或将dataArray传递给ArrayAdapter的超级构造函数。

查看documentation

答案 1 :(得分:0)

只需将此添加到适配器类...

@Override
    public int getCount() {
        return dataArray.length;
    }