1

This is my code

  public void onClick(View view){
            Logger.debug(TAG, view.getId()+"-->ID");
            ImageView img = (ImageView)hasID.get(view.getId());
            ImageView sameple = (ImageView)hasID.get(isample);

        Logger.debug(TAG, "----------------------");
        Logger.debug(TAG, listFruit.size()+"--->list size");
        Logger.debug(TAG, img.getId() + "--->id");
        Logger.debug(TAG, sameple.getId() +"--->sample id");
        Logger.debug(TAG, hasName.get(img.getId()) +"--->name");
        Logger.debug(TAG, hasName.get(sameple.getId())+ "---> sample name");
        Logger.debug(TAG, "----------------------");
        if(view.getId() == isample){
            String oldScore = this.score.getText().toString();
            int newscore = Integer.valueOf(oldScore) + 1;
            this.score.setText(String.valueOf(newscore));
            boolean b = listFruit.remove(sameple);
            Logger.debug(TAG, "result:"+b);
            scorrect.start();
            change_number_click(0);
            img.setVisibility(View.INVISIBLE);
            randomSample();
        }else{//click wrong
            swrong.start();
        }
    }

public void randomSample(){
    isample = random.nextInt(listFruit.size());
    for(int i = 0;i<listFruit.size();i++){
        int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
                getApplicationContext().getPackageName());
        Logger.debug(TAG, hasName.get(id)+"-->name");
    }
    Logger.debug(TAG, "------create sample------");
    Logger.debug(TAG, listFruit.size()+"-->list size");
    Logger.debug(TAG, isample+"-->sample");
    int id = getApplicationContext().getResources().getIdentifier(fruit.get(isample).file, "drawable",
            getApplicationContext().getPackageName());
    Logger.debug(TAG, id +"-->id sample");
    Logger.debug(TAG, hasName.get(id)+"-->name");
    this.sample.setImageResource(id);
    this.sample.setVisibility(View.VISIBLE);
    isample = id;
    Logger.debug(TAG, "-------------------------");
}

And this is result on console:

07-20 02:33:50.694    annona-->name
07-20 02:33:50.694    apple-->name
07-20 02:33:50.694    banana-->name
07-20 02:33:50.694    ------create sample------
07-20 02:33:50.694    3-->list size
07-20 02:33:50.694    0-->sample
07-20 02:33:50.694    2130837563-->id sample
07-20 02:33:50.694    annona-->name
07-20 02:33:50.694    -------------------------
07-20 02:33:50.694    2130837563-->sample
07-20 02:33:50.786    Tick...
07-20 02:33:51.794    Tick...
07-20 02:33:52.694    2130837563-->ID
07-20 02:33:52.694    ----------------------
07-20 02:33:52.694    3--->list size
07-20 02:33:52.694    2130837563--->id
07-20 02:33:52.694    2130837563--->sample id
07-20 02:33:52.694    annona--->name
07-20 02:33:52.694    annona---> sample name
07-20 02:33:52.694    ----------------------
07-20 02:33:52.694    result:true
07-20 02:33:52.698    annona-->name
07-20 02:33:52.698    apple-->name
07-20 02:33:52.698    ------create sample------
07-20 02:33:52.698    2-->list size
07-20 02:33:52.698    0-->sample
07-20 02:33:52.698    2130837563-->id sample
07-20 02:33:52.698    annona-->name
07-20 02:33:52.698    -------------------------

As you see. listFruit is Arraylist

List<ImageView> listFruit = new ArrayList<>();

and this is list value

07-20 02:33:50.694    annona-->name
07-20 02:33:50.694    apple-->name
07-20 02:33:50.694    banana-->name

First, it creates a sample object

07-20 02:33:50.694    ------create sample------
07-20 02:33:50.694    3-->list size
07-20 02:33:50.694    0-->sample
07-20 02:33:50.694    2130837563-->id sample
07-20 02:33:50.694    annona-->name

is annona, in onClick function I remove if user click on sample object

//hasID is hashtable, with key is id, and value is object in listFruit
ImageView sameple = (ImageView)hasID.get(isample);

it return true But I see it always remove last item in listFruit ArrayList

07-20 02:33:52.694    result:true
07-20 02:33:52.698    annona-->name
07-20 02:33:52.698    apple-->name

Actually, It must remove annona at first item, but not. What's wrong?

More information*

I changed my code to this

  Logger.debug(TAG, "----------------\n");
                Logger.debug(TAG, "Before remove:");
                for(int i = 0;i<listFruit.size();i++){
                    int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
                            getApplicationContext().getPackageName());
                    Logger.debug(TAG, hasName.get(id)+"-->name");
                }
                Logger.debug(TAG, "remove ID:"+ksample);
                listFruit.remove(ksample);
                Logger.debug(TAG, "After remove:");
                for(int i = 0;i<listFruit.size();i++){
                    int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
                            getApplicationContext().getPackageName());
                    Logger.debug(TAG, hasName.get(id)+"-->name");
                }
                Logger.debug(TAG,"------------------\n");

I remove by index, and this is result:

07-20 03:05:59.694     Before remove:
07-20 03:05:59.694     annona-->name
07-20 03:05:59.694     apple-->name
07-20 03:05:59.694     remove ID:0
07-20 03:05:59.694     After remove:
07-20 03:05:59.694     annona-->name

I removed at position 0 - annona but it still remove last item :(

1 Answer 1

1

When you print out the contents of your list with this code you are reading from the fruit list rather than the listFruit list, you only use listFruit to get the size.

for(int i = 0;i<listFruit.size();i++){
        int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
                getApplicationContext().getPackageName());
        Logger.debug(TAG, hasName.get(id)+"-->name");
    }

Since you don't remove any item from fruit then it doesn't change, so when you print it out it just prints out one less item (because listFruit is now one item smaller).

To fix this issue you need to ensure that the items in fruit are in the same order as listFruit, and remove an item from fruit at the same time you remove an item from listFruit.

Sign up to request clarification or add additional context in comments.

1 Comment

oh, it's my stupid mistake, I get me hours headache to find out where is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.