Problems implementing ViewHolder, ObjectItem cannot be resolved to a type

2k Views Asked by At

Im implementing a ViewHolder. Im using this tut: Link

And this is the code:

@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;

    }

Now the problem is that I dont know what is "ObjectItem". where did that class come from?

also "data" on the same row is not recognized.

How should I do it? the tutorial doesnt explain it..

Thanks!

2

There are 2 best solutions below

0
On

ObjectItem is a wrapper for the data. From what you wrote, I can see:

public class ObjectItem {
 String itemName;
 String itemId;
}

and you can fill it up, this way:

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;
}
0
On

Look at the previous link in the same blog. Its a continuation i guess so you need to look the previous post and read the blog full.

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;
    }

}

And for populating using h uses the below (using for loop is better)

    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");

And the continuation in the second link for using view holder pattern

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();

}

So you need to follow 2 links

  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