-2

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

qqrrrr
  • 575
  • 2
  • 8
  • 22
  • (Bear with me, I'm trying to comprehend this on the fly) It looks to me like the problem is in the loop. The loop doesn't stop if the inner if-statement is true, so it continues until the loop is finished. If the statement was true in multiple cases, you will only see the last true case. Also, the "position" parameter isn't being used for anything, perhaps this is your intent, yet still, it may be useful in this situation. – Moon Aug 25 '15 at 01:21
  • @phixle I am doing that because I want to return every true in the "if" statement. Can you help me? – qqrrrr Aug 25 '15 at 02:11
  • Having that for loop inside `getView()` and calling `notifyDataSetChanged()` is not good. What are you trying to do there? – JanithaR Aug 25 '15 at 04:01
  • @janithar I thought putting notifyDataSetChanged would help on getting what I want. But is doesn't. I want to display the image if the "if statement" is true. but when I tried to run that lines of codes. It only displays the last item being fetch. What I am really trying to do is to get the corresponding image of each letter. for example: i inputted "hi", so the output should be the corresponding image of "h" and "i" – qqrrrr Aug 26 '15 at 01:25
  • the whole loop part does not depend on the position of the view. it can only always return the same thing. exactly how do you suppose the image set is choosen? – njzk2 Aug 26 '15 at 02:08
  • @njzk2 the image set is choosen, for example: the alphaSplit contains = {'a', 'b'} and the ALPHA = {'a', 'b', 'c'} if(alphaSplit[0] == ALPHA[0]) //since it's true then the image would be the id of j. i dont know if i am still making sense – qqrrrr Aug 26 '15 at 02:26
  • @tin but how do you expect a different image to be selected for the different items? – njzk2 Aug 26 '15 at 02:28
  • i think you want to remove the outer loop altogether and use `alphaSplit[position]` – njzk2 Aug 26 '15 at 02:29
  • @njzk2 what do you mean by different items? – qqrrrr Aug 26 '15 at 02:31
  • you question says that it returns the last item fetched. but your code shows something that always behave the same, in other words that always show the same item – njzk2 Aug 26 '15 at 03:19

1 Answers1

0

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;
}
uuku
  • 56
  • 4
  • Oh, I am sorry. I didn't know. I thought putting notifyDataSetChanged would help. If I am going to break the loop. How would I be able to display all the possible true statements in the succeeding loop? – qqrrrr Aug 25 '15 at 02:12
  • you probably need to break out of the 2 loops (with a label, for example) – njzk2 Aug 25 '15 at 02:48
  • @tin there is a good chance the outer loop will still result in the last item only being shown. hence, you have to break out of it too. One way of breaking an outer loop in java is using labels. !!!!! – njzk2 Aug 25 '15 at 03:37
  • It doesn't change anything when i am going to test the codes. It displays the last item being fetch even if i already use that labeling for breaking the outer loop. – qqrrrr Aug 26 '15 at 01:15
  • @tin Could you post more codes about the `Adapter`? – uuku Aug 26 '15 at 01:42
  • @uuku thank you so much! The updated code really helps a lot. Thank you! God bless you :) – qqrrrr Sep 01 '15 at 02:10