Search code examples
androidandroid-listviewgetview

ArrayAdapter.getView returns only the last item being fetch


Here is my getview code. It always returns the last item being fetched. How can I resolve this problem. I am new to HolderView. *UPDATED CODES

public static final char[] ALPHA = {'a', 'b'....};
int[] ICONS = {R.drawable.a, R.drawable.b....};
public CustomList(Context context, Character[] split) {
    super(context, R.layout.activity_list, split);
    inflater = LayoutInflater.from(context);
    this.alphaSplit = split;
}
static class Holder {
    ImageView imageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.activity_list, parent, false);
        holder = new Holder();
        holder.imageView = (ImageView)convertView.findViewById(R.id.img);
        convertView.setTag(holder);
    } else {
        holder = (Holder)convertView.getTag();
    }

    setImage:
    for (int loop = 0; loop < alphaSplit.length; loop++) {
        for (int j = 0; j < ALPHA.length; j++) {
            if (alphaSplit[loop] == ALPHA[j]) {
                holder.imageView.setImageResource(ICONS[j]);
                break setImage;
            }
        }
    }
    return convertView;
}

I just wanted to get the corresponding image of each letter. that is why i am having a nested loop because from there, I could get the position of the ICONS. The outer loop is the text that the user inputs and I parse it into a Character(since it will make some errors if i am going to use char). and the inner loop is char of ALPHA = {'a', 'b'.. until 'z' and '0' to '9'}


Solution

  • In the method getView, the loop should break when matched like this:

    for (int loop = 0; loop < alphaSplit.length; loop++) {
        for (int j = 0; j < ALPHA.length; j++) {
            if (alphaSplit[loop] == ALPHA[j]) {
                holder.imageView.setImageResource(ICONS[j]);
                break; //ATTENTION PLS!!!
            }
        }
    }
    

    BTW, you need not to invoke the notifyDataSetChanged in the getView. FYI

    PS: Maybe you need to break two loops like this:

    outer:
    for (int loop = 0; loop < alphaSplit.length; loop++) {
        for (int j = 0; j < ALPHA.length; j++) {
            if (alphaSplit[loop] == ALPHA[j]) {
                holder.imageView.setImageResource(ICONS[j]);
                break outer; //ATTENTION PLS!!!
            }
        }
    }
    

    UPDATE:

    Change your code :

     setImage:
    for (int loop = 0; loop < alphaSplit.length; loop++) {
        for (int j = 0; j < ALPHA.length; j++) {
            if (alphaSplit[loop] == ALPHA[j]) {
                holder.imageView.setImageResource(ICONS[j]);
                break setImage;
            }
        }
    }
    

    like this:

    for (int j = 0; j < ALPHA.length; j++) {
       if (alphaSplit[position] == ALPHA[j]) {
            holder.imageView.setImageResource(ICONS[j]);
            break;
        }
    }
    

    and override the getCount method:

    @Override
    public int getCount() {
        return this.alphaSlipt.length;
    }