Home > Software engineering >  Android onItemClickListener works only once, I don't know why
Android onItemClickListener works only once, I don't know why

Time:04-10

I want the onItemClickListener always works, but after it works for the first time, it doesn't work anymore. This is my code in MainActivity.java Please help, I can't seem to find problem. Thank you

package com.noval.tugas_5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    private ListView listView1;
    private CustomAdapter adapter;
    private final String[] textString = new String[]{"Game", "Genre Film", "Jenis Musik", "Mobil", "Motor", "Makanan", "Exit"};
    private final String[] textStringGame = new String[]{"Mobile Legends", "Clash of Clans", "Dota II", "Call of Duty", "Masuk ke Jenis Musik", "Menu Utama"};
    private final int[] drawableIds = new int[]{R.drawable.utama1, R.drawable.utama2, R.drawable.utama3,
            R.drawable.utama4, R.drawable.utama5, R.drawable.utama6, R.drawable.exit};
    private final int[] drawableIdsGame = new int[]{R.drawable.utama1, R.drawable.utama2, R.drawable.utama3,
            R.drawable.utama4, R.drawable.utama5, R.drawable.exit};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView1 = (ListView) findViewById(R.id.menuList);

        adapter = new CustomAdapter(MainActivity.this, textString, drawableIds);
        listView1.setAdapter(adapter);


        // membuat event click
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int posisi, long id) {

                // Get the item that was clicked
                // Menangkap nilai text yang diklik
                Object o = adapter.getItem(posisi);
                String pilihan = o.toString();

                if (pilihan.equals("Game")) {
                    CustomAdapter adapter = new CustomAdapter(MainActivity.this, textStringGame, drawableIdsGame);
                    listView1.setAdapter(adapter);
                } else if (pilihan.equals("Exit")) {
                    finish();
                    System.exit(0);
                } else if (pilihan.equals("Menu Utama")) {
                    CustomAdapter adapter = new CustomAdapter(MainActivity.this, textString, drawableIds);
                    listView1.setAdapter(adapter);
                } 
            }
        });
    }
}

I already try to find out what happen for hours.

CodePudding user response:

You are doing it wrong.

listView onItemClickListener gets overwritten every time you modify the list adapter. So you have to initialize it every time you update the adapter.

You can do it recursively by using something like this -

public void initAdapter(ListView listview , CustomAdapter adapter){
    listView.setAdapter(adapter);
    // membuat event click
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int posisi, long id) {
            // Get the item that was clicked
            // Menangkap nilai text yang diklik
            Object o = adapter.getItem(posisi);
            String pilihan = o.toString();

            if (pilihan.equals("Game")) {
                CustomAdapter adapter = new CustomAdapter(MainActivity.this, textStringGame, drawableIdsGame);
                initAdapter(listView,adapter);
            } else if (pilihan.equals("Exit")) {
                finish();
                System.exit(0);
            } else if (pilihan.equals("Menu Utama")) {
                CustomAdapter adapter = new CustomAdapter(MainActivity.this, textString, drawableIds);
                initAdapter(listView,adapter);
            }//Add more options here 
        }
    });
}

after adding the method, replace this entire code

        listView1.setAdapter(adapter);
        // membuat event click
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               //your code ...
            }
        });

to this -

initAdapter(listView1,adapter);

Happy Coding!

  • Related