Reputation: 4719
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?
Upvotes: 0
Views: 62
Reputation: 544
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:-
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.
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...
//...
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>
Upvotes: 1