0

I have seen many questions and answer regarding this issue. I tried almost all of them but none of the answers didn't work for me.

I am using Samsung S9 phone running Android 8.0.0 (API 26).

If I try the following code, I setOnItemClickListener is called.

    mListView = (ListView) findViewById(R.id.azure_photo_list);
    mListView.setDividerHeight(1);
    registerForContextMenu(mListView);
    // ListView Item Click Listener
    mListView.setOnItemClickListener((parent, view, position, id) -> {
        Intent intent = new Intent(getBaseContext(), AzureImageActivity.class);
        intent.putExtra("image", images[position]);
        startActivity(intent);

    }); 

   String[] images = ImageManager.ListImages();
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(AzurePhotoList.this,
                            android.R.layout.simple_list_item_1, android.R.id.text1, images);
   mListView.setAdapter(adapter);

Note that the layout is from the Android system and text view from the Android system. If I provide my own layout as follows:-

String[] images = ImageManager.ListImages();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AzurePhotoList.this,                         
R.layout.content_azure_photo_list,R.id.azure_list_item_name, images);

Then setOnItemClickListener is not called anymore. What is wrong?

7
  • Make sure you are setting listener only one place. Commented Sep 30, 2018 at 18:21
  • comment out this line registerForContextMenu(mListView); for a while and try Commented Sep 30, 2018 at 18:22
  • @SurajVaishnav I set listener in only one place for this listview. But I implement NavigationView.OnNavigationItemSelectedListener for this activity. Commented Sep 30, 2018 at 18:35
  • @KrishnaSharma I comment out line registerForContextMenu(mListView); It is same not called setOnItemClickListener Commented Sep 30, 2018 at 18:36
  • It's hard to comment just on the code you provided, but why are you using ListView in 2018 when RecyclerView has proven advantages over it. Morever, it gives more flexibility with better control. Commented Sep 30, 2018 at 18:45

1 Answer 1

1

I cloned the repo and found out that there are few issues in the code base.

To make the click listener work, you have to at least change the following:-

  1. In xml(mentioned below), you have used ConstraintLayout and have not give constraints to any view. As a result, every view draws to (0,0).
  2. The implementation of Handler is incorrect

To give you a head start, change the below mentioned 2 files in your code and click listener will work everytime.

Note: I have given very less time to code and this is not a production ready code. Giving you points and code below to point you in right direction.

  1. AzurePhotoList

    public class  AzurePhotoList extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    
    private final static int MY_REQUEST_PERMISSIONS_READ_EXTERNAL_STORAGE = 102;
    private String[] images;
    private ListView mListView;
    private Handler handler;
    private String[] images_lists;
    
    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_azure_photo_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                 images_lists = msg.getData().getStringArray("images_list");
     //                AzurePhotoList.this.images = images;
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(AzurePhotoList.this,
                    R.layout.content_azure_photo_list, R.id.azure_list_item_name, images_lists);
                        /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(AzurePhotoList.this,
                                R.layout.content_azure_photo_list,R.id.azure_list_item_name, images);*/
                mListView.setAdapter(adapter);
            }
        };
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    
        mListView = (ListView) findViewById(R.id.azure_photo_list);
        mListView.setDividerHeight(1);
        registerForContextMenu(mListView);
    
        // ListView Item Click Listener
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(AzurePhotoList.this.getBaseContext(), AzureImageActivity.class);
            intent.putExtra("image", images_lists[position]);
            AzurePhotoList.this.startActivity(intent);
    
        });
    
        loadImageFromAzure();
    }
    
    private void loadImageFromAzure(){
        Thread th = new Thread(new Runnable() {
            public void run() {
                try {
                    final String[] images = ImageManager.ListImages();
                    Bundle bundle = new Bundle();
                    bundle.putStringArray("images_list", images);
                    Message message = new Message();
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
                catch(Exception ex) {
                    final String exceptionMessage = ex.getMessage();
                    handler.post(new Runnable() {
                        public void run() {
                            Toast.makeText(AzurePhotoList.this, exceptionMessage, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }});
        th.start();
    }
    
    //your remaining code...
    //...
    
  2. content_azure_photo_list.xml

    <ListView
        android:id="@+id/azure_photo_list"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/azure_list_item_name"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:textAppearance="@android:style/TextAppearance.Medium" />
    
    </LinearLayout>
    

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

1 Comment

Thanks a lot! I have been busy with another project. I will check it as soon as I manage some time. I might come up with more question when I start checking the issue.

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.