簡體   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