繁体   English   中英

如何将自定义getView绑定到ListView适配器?

[英]How to bind custom getView to ListView adapter?

我正在尝试创建一个Customer ListView我已经设置了布局和getView方法,但是我不确定如何将新视图从getView绑定到我的适配器?

我有一个从ArrayAdapter<String>扩展的feedClass ,其中有getView方法:

public class FeedClass extends ArrayAdapter<String> {
private final Context context;

public FeedClass(Context context) {
    super(context, R.layout.feed_item_layout);
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.feed_item_layout, parent,
            false);

    ImageView tweetPicture = (ImageView) rowView
            .findViewById(R.id.tweetPicture);
    TextView tweetDescription = (TextView) rowView
            .findViewById(R.id.tweetDescription);
    TextView legends = (TextView) rowView.findViewById(R.id.legends);

    tweetPicture.setImageResource(R.drawable.ic_profile);
    tweetDescription.setText("Hello");
    legends.setText("Legends");

//How to show this data in the ListView?

    return rowView;
}

}

在从ListActivity扩展的MainActivity ,我有以下内容:

private void activateFeed() {       
//Now I Want to show data in ListView that i am setting in getView?
    ArrayAdapter<String> adptr = new ArrayAdapter<String>(this,
            R.layout.feed_item_layout, ?); //What to pass here?

    setListAdapter(adptr);

}

当前它正在使用数组,现在我不知道如何将自定义视图从getView传递给它? 请帮忙。

您需要扩展BaseAdapter并重写getView方法

public class FeedClass extends BaseAdapter {

private Context context;
private ArrayList<String> arrayList;

public FeedClass(Context context, ArrayList<String> arrayList) {
    this.context = context;
    this.arrayList = arrayList;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int arg0) {
    return arrayList.get(arg0);
}

@Override
public long getItemId(int arg0) {
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.feed_item_layout, parent,
            false);

    ImageView tweetPicture = (ImageView) rowView
            .findViewById(R.id.tweetPicture);
    TextView tweetDescription = (TextView) rowView
            .findViewById(R.id.tweetDescription);
    TextView legends = (TextView) rowView.findViewById(R.id.legends);

    tweetPicture.setImageResource(R.drawable.ic_profile);
    // set the text from the ArrayList
    tweetDescription.setText(arrayList.get(position));
    // set the text from the ArrayList
    legends.setText(arrayList.get(position));

    return rowView;
}
}

我假设您有一个名称为feedClass的自定义适配器,并且要将其设置为ListActivity

  LayoutInflater inflater;
  public FeedClass(Context context)
  {
     super(context, R.layout.feed_item_layout);
     inflater = LayoutInflater.from(context);  
     // no need to initialize everytime in getview
     // so you can initialize in the constructor
  }

还可以使用ViewHolder模式来实现平滑滚动和性能

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

现在

 FeedClass fc = new FeedClass(MainActivity.this);
 setListAdapter(fc);  

我在网上发现的例子

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

ListActivity有自己的适配器实现,您可以通过正确的方式来完成它,但是如果您要使用自己的Adapter类实例,则需要将活动文件扩展为扩展Activity,然后可以使用适配器的getView方法

暂无
暂无

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

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