使用自定义对象填充listview

时间:2013-11-11 05:34:39

标签: android listview

我有一个自定义对象的arraylist(NewsFeedObj)。我想使用NewsFeedObj中的变量填充列表视图。当我运行代码时,我得到一个空白屏幕。怎么了?

 class Sadapter extends ArrayAdapter<NewsFeedObj>
  {
  Context context;
   int j;
    NewsFeedObj o = new NewsFeedObj();
     ArrayList<NewsFeedObj> l;
public Sadapter(Context context,int i,ArrayList<NewsFeedObj> g) 
{
    super(context,i,g);
    this.context=context;
    this.l=g;
    this.j=i;

    // TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row=inflater.inflate(R.layout.newfeed_layout,parent,false);
    //row.findViewById(id)
    TextView Text=(TextView) row.findViewById(R.id.Description);
    o=l.get(position);
    Text.setText( o.get_description());


    return row;
}




        }

3 个答案:

答案 0 :(得分:0)

尝试为数组适配器实现getter方法。我认为这可能是你问题的根源..

答案 1 :(得分:0)

更改getView()方法

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row= convertView;
        if (view == null) {// Added check
        LayoutInflater inflater = (LayoutInflater)   context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.newfeed_layout,parent,false);
        }
        NewsFeedObj  o=l.get(position);// Changed here
        TextView Text=(TextView) row.findViewById(R.id.Description);

        Text.setText( o.get_description());


        return row;
    }

答案 2 :(得分:0)

View row=inflater.inflate(R.layout.newfeed_layout,parent,false);

应该是这个

View row=inflater.inflate(list_row.xml,null);

如果您为每一行使用一些布局(例如list_row.xml)