1

我正在尝试使用基本适配器来使用我的对象(NewsFeedObj)中的字段填充我的列表视图我目前只是想获取描述字段。当我运行我的代码时,我只是得到一个空白活动?谁能帮我

public class News_Feed_BaseAdapter extends BaseAdapter {

    private Context context;

    private List<NewsFeedObj> obj = Collections.emptyList();

    public News_Feed_BaseAdapter(Context context, List<NewsFeedObj> obj) {
        this.context = context;
        this.obj = obj;
    }

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

    @Override
    public NewsFeedObj getItem(int position) {
        // TODO Auto-generated method stub
        return obj.get(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

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.newfeed_layout, parent, false);
        }

        TextView t = (TextView) convertView.findViewById(R.id.Description);

        NewsFeedObj o = getItem(position);
        t.setText(o.get_description());

        return convertView;
    }

}

在主要活动中

ArrayList<NewsFeedObj> NewsObjs = new ArrayList<NewsFeedObj>();
NewsObjs = j.new_feed_Array_List(); // returns an array list of
                                    // NewsFeedObj

ListView l = getListView();
News_Feed_BaseAdapter A = new News_Feed_BaseAdapter(this, NewsObjs);

l.setAdapter(A);
4

3 回答 3

0

This is also the same issue what you faced in arrayadapter.. I also experienced that.. You just need to do is change the line

convertView = LayoutInflater.from(context).inflate(R.layout.newfeed_layout, parent, false);

as

convertView = LayoutInflater.from(context).inflate(R.layout.newfeed_layout, null);

for both ArrayAdapter and BaseAdapter. I'm sure this will work..

于 2013-11-13T04:53:39.987 回答
0

j.new_feed_Array_List(); 这是什么意思..你能详细说明代码吗?如果它有效,请尝试这个适配器类

static class ViewHolder {
    TextView itemTitle;

}

在视图中

ViewHolder holder;
holder.t=(textView)....
holder.t.setText(...);
于 2013-11-13T05:13:59.570 回答
0

尝试调用 A.notifyDataSetChanged(); 在 l.setAdapter(A) 之后;

这让您的适配器知道它所引用的数据已更改,并且需要更新其数据显示。当然,NewsObjs 需要是一个非空列表。

于 2013-11-11T22:43:01.980 回答