0

我正在实现一个 ViewHolder。我正在使用这个 tut:链接

这是代码:

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

        ViewHolder viewHolder;

        /*
         * The convertView argument is essentially a "ScrapView" as described is Lucas post 
         * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
         * It will have a non-null value when ListView is asking you recycle the row layout. 
         * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
         */
        if(convertView==null){

            // inflate the layout
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(position, parent, false);

            // well set up the ViewHolder
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.image= (ImageView) convertView.findViewById(R.id.image);

            // store the holder with the view.
            convertView.setTag(viewHolder);

        }else{
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // object item based on the position
        ObjectItem objectItem = data[position];

        // assign values if the object is not null
        if(objectItem != null) {
            // get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
            viewHolder.title.setText(objectItem.itemName);
            viewHolder.title.setTag(objectItem.itemId);
        }

        return convertView;

    }

现在的问题是我不知道什么是“ObjectItem”。那门课是从哪里来的?

同一行上的“数据”也无法识别。

我该怎么做?教程没有解释..

谢谢!

4

2 回答 2

0

查看同一博客中的上一个链接。我想这是一个延续,所以您需要查看上一篇文章并完整阅读博客。

http://www.codeofaninja.com/2013/09/android-listview-with-adapter-example.html

//another class to handle item's id and name
public class ObjectItem {

    public int itemId;
    public String itemName;

    // constructor
    public ObjectItem(int itemId, String itemName) {
        this.itemId = itemId;
        this.itemName = itemName;
    }

}

对于使用 h 进行填充,请使用以下内容(使用 for 循环更好)

    ObjectItem[] ObjectItemData = new ObjectItem[20];

    ObjectItemData[0] = new ObjectItem(91, "Mercury");
    ObjectItemData[1] = new ObjectItem(92, "Watson");
    ObjectItemData[2] = new ObjectItem(93, "Nissan");
    ObjectItemData[3] = new ObjectItem(94, "Puregold");
    ObjectItemData[4] = new ObjectItem(95, "SM");
    ObjectItemData[5] = new ObjectItem(96, "7 Eleven");
    ObjectItemData[6] = new ObjectItem(97, "Ministop");
    ObjectItemData[7] = new ObjectItem(98, "Fat Chicken");
    ObjectItemData[8] = new ObjectItem(99, "Master Siomai");
    ObjectItemData[9] = new ObjectItem(100, "Mang Inasal");
    ObjectItemData[10] = new ObjectItem(101, "Mercury 2");
    ObjectItemData[11] = new ObjectItem(102, "Watson 2");
    ObjectItemData[12] = new ObjectItem(103, "Nissan 2");
    ObjectItemData[13] = new ObjectItem(104, "Puregold 2");
    ObjectItemData[14] = new ObjectItem(105, "SM 2");
    ObjectItemData[15] = new ObjectItem(106, "7 Eleven 2");
    ObjectItemData[16] = new ObjectItem(107, "Ministop 2");
    ObjectItemData[17] = new ObjectItem(108, "Fat Chicken 2");
    ObjectItemData[18] = new ObjectItem(109, "Master Siomai 2");
    ObjectItemData[19] = new ObjectItem(110, "Mang Inasal 2");

以及使用视图持有者模式的第二个链接中的延续

http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html

    public void showPopUp(){

    // we'll specify the number of items we want our ListView to have.
    int numberOfItems = 1000;

    // add your items, this can be done programatically
    // your items can be from a database
    ObjectItem[] ObjectItemData = new ObjectItem[numberOfItems];

    // we'll use a for loop 
    // created objects = number of items specified above
    for(int x=0; x<numberOfItems; x++){

        int sampleId = 90 + x;
        ObjectItemData[x] = new ObjectItem(sampleId, "Store # " + (x+1));

    }

    // our adapter instance
    ArrayAdapterItem adapter = new ArrayAdapterItem(this, R.layout.list_view_row_item, ObjectItemData);

    // create a new ListView, set the adapter and item click listener
    ListView listViewItems = new ListView(this);
    listViewItems.setAdapter(adapter);
    listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem());

    // put the ListView in the pop up
    alertDialogStores = new AlertDialog.Builder(MainActivity.this)
        .setView(listViewItems)
        .setTitle("Stores")
        .show();

}

所以你需要关注2个链接

  1. http://www.codeofaninja.com/2013/09/android-listview-with-adapter-example.html

  2. http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html

于 2013-11-02T13:30:51.177 回答
0

ObjectItem是数据的包装器。从你写的,我可以看到:

public class ObjectItem {
 String itemName;
 String itemId;
}

你可以这样填满它:

ObjectItem[] data = new ObjectItem[10];
for (int i = 0; i < 10; i++) {
  ObjectItem tmp = new ObjectItem();
  tmp.itemName = "name_"+i
  tmp.itemId = "id_"+i;
  data[i] = tmp;
}
于 2013-11-02T13:06:26.047 回答