setOnItemClickListener of ListView doesn't work

listview setonitemclicklistener not working
setonitemclicklistener kotlin
setonitemclicklistener spinner
setonitemclicklistener recyclerview android
android listview setonitemclicklistener doesn't work
listview setonitemclicklistener not working in fragment
setonitemclicklistener cannot be used with a spinner
setonitemclicklistener in adapterview cannot be applied

I have a rather strange situation. I display in a listview some products with their details that are read from an SQLite database via a Content provider. However, when I click in any row of the list view nothing happens. So I am posting some of my code.

MainActivity

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
private static final String TAG = MainActivity.class.getSimpleName();
private static final int PRODUCT_LOADER = 0;
ProductCursorAdapter mCursorAdapter;

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAddProductDialog();
        }
    });

    ListView productListView = (ListView) findViewById(R.id.list_view_product);

    View emptyView = findViewById(R.id.empty_view);
    productListView.setEmptyView(emptyView);

    mCursorAdapter = new ProductCursorAdapter(this, null);
    productListView.setAdapter(mCursorAdapter);

    productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Intent i = new Intent(MainActivity.this,EditActivity.class);
            Uri currentProductUri = ContentUris.withAppendedId(ProductEntry.CONTENT_URI,id);
            i.setData(currentProductUri);
            startActivity(i);
        }
    });

    getLoaderManager().initLoader(PRODUCT_LOADER, null, this);
}

private void showAddProductDialog() {
    AddDialogFragment newFragment = new AddDialogFragment();
    newFragment.show(getSupportFragmentManager(), getString(R.string.add_product));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {
            ProductEntry._ID,
            ProductEntry.COLUMN_PRODUCT_NAME,
            ProductEntry.COLUMN_PRODUCT_QUANTITY,
            ProductEntry.COLUMN_PRODUCT_PRICE,
            ProductEntry.COLUMN_PRODUCT_IMAGE};

    return new CursorLoader(this,
            ProductContract.ProductEntry.CONTENT_URI,
            projection,
            null,
            null,
            null
    );
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mCursorAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mCursorAdapter.swapCursor(null);
}
}

and activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
tools:context="com.example.android.invetoryapp.MainActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_input_add"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_margin="@dimen/fab_margin" />

<ListView
    android:id="@+id/list_view_product"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_margin"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

    />
<RelativeLayout
    android:id="@+id/empty_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <TextView
        android:id="@+id/empty_title_text"
        style="@style/EmptyTextView"
        android:text="@string/empty_view_subtitle_text" />
</RelativeLayout>

As you can see I am using those attributes, as suggesting in different answers I found, but still nothing happens when I am clicking a row.

 android:focusable="false"
 android:focusableInTouchMode="false"

How can i fix that error?

Thanks,

Theo.

Try with add below lines in ListView attributes of your activity_main.xml like this:-

android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true"

ListView.setOnItemClickListener. The examples are extracted from open getIntent()); } } }; // We create our own ListView, as AlertDialog doesn't let us set a​  But The ListView doesn't seem to respond to the setOnItemClickListener() at all, So I had to find a workaround by setting click listener in the getView() of the Text inside every row which is obviously creating problem when I am trying to reuse the adapter. Does anyone have a solution?

Remove below lines from your ListView in xml;

    android:focusable="false"
    android:focusableInTouchMode="false"

setOnItemClickListener(new SlideMenuClickListener()); . } private class SlideMenuClickListener implements ListView.OnItemClickListener { @  But The ListView doesn’t seem to respond to the setOnItemClickListener() at all, So I had to find a workaround by setting click listener in the getView() of the Text inside every row which is obviously creating problem when I am trying to reuse the adapter. Does anyone have a solution?

Either remove these lines

android:focusable="false"
android:focusableInTouchMode="false"

or make them true

android:focusable="true"
android:focusableInTouchMode="true"

Your browser does not currently recognize any of the video formats available. Click here Duration: 7:53 Posted: Mar 20, 2018 listView.setOnItemClickListener() doesn't work anymore Hi, yesterday I made a listview with a custom adapter and implemented a setOnItemClickListener, which worked 100% as intended.

add this lines to listview:

android:focusable="true"
android:focusableInTouchMode="true" 

add this lines to other view which has focusable feature:

android:focusable="false"
android:focusableInTouchMode="false" 

listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener()); Spinner does not wrap text -- is this an Android bug? Android - List view - setOnItemClickListener - In this series, we are going to learn about the click Listeners in android and how to use them? Submitted by Manu Jemini, on January 26, 2018 Below is an example of how to use this technique in List Items. To show that this works, I have created a simple ListView which have a text view.

Remove these line

android:focusable="false"
 android:focusableInTouchMode="false"

And Add on Activity

listview.setOnClickListener

I've tried setting an OnItemClickListener within the View method of my custom Adapter class. Why doesn't this work? So I want to launch a new activity whenever a listview item is clicked, and not just when clicking a holder  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { click(); } }); the event is never fired. I tried to put as many items in my row not clickable or focusable.

Hmm i am looking but cannot see it either. Kind of strange but if your java file is short could you paste all of it, please? i have the same problem when add second listview "setOnItemClickListener" doesn't work. I have add these in both my listview : android:clickable="false" android:focusable="false" android:descendantFocusability="blocksDescendants" i try to add "LinearLayout" and "android:layout_weight="1" " but the result of calling "setOnItemClickListener" is

I want to filter a listview and when I click on the filtered list item, the OnItemClickListener But the problem is, the onItemClickListener is not able to get postID from the filtered I cannot figure out the problem from 2-3 days. The app preview is perfectly fine and responsive for different screen sizes but when I run the app on my Samsung Galaxy S10, it doesn't display properly. Only the background and some text are showing properly.

And somewhere inside ListView there’s setOnItemClickListener(), which should intercept item clicking events. My problem that, whenever I try to add checkbox to item – I don’t get any responces from my ListView .

Comments
  • What's in your cell layout?
  • add android:descendantFocusability="blocksDescendants"
  • I added in my listview. Doesn't work.
  • But why are you using ListView still ? RecylerView is better than ListView with its own view holder pattern.
  • @Piyush . Well that would be the next level. For now I am just using listview.
  • For now remove intent code and try this code and see in logcat. are able to see this on click log there in logcat? public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Log.d("onclick", "onclick"); }
  • Nothing. When I click a row, I am getting this D/AbsListView: onTouchUp() mTouchMode : 1