Listview Setonitemclicklistener Not Working by Adding Button

ListView setOnItemClickListener not working in custom list view

Set these properties

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

for your all UI elements in your create_list_item xml file.

Also remove that properties from ListView.

So your ListView will be

 <ListView
android:id="@+id/createlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>

ListView setOnItemClickListener not working by adding button

Try setting your buttons (or any other views you want to handle click inside a list item) like this:

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

ListView setOnItemClickListener not work

In your ListView remove the xml attribute android:clickable="true"

This is taking your click event and not sending it down to the child views.

Edit

You are passing in getApplicationContext() which is the wrong context. The listener does work, but the Toast is not displaying in the right context. You need to pass in getContext(). However, since this is an Activity, you can just pass in this instead.

public void outputListView(ArrayList<String> list) {
Adapter adapter = new Adapter(this, R.layout.item, list);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);

listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(this, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}

Listview setOnItemClickListener not working

try this

list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name));

Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(view.getContext(), LessonActivity.class);
startActivity(intent);

}
});

or

list.setOnItemClickListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name));

Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(view.getContext(), LessonActivity.class);
startActivity(intent);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

Alternate method

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View lessonView = convertView;
if(lessonView == null)
{
lessonView = getLayoutInflater().inflate(R.layout.lesson_item, parent, false);
}

Lesson currentLesson = lessons.get(position);

//here I add all the needed info into the item.
//This part works fine and displays everything I need properly.

lessonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(""testing123,"testing");
Toast.makeText(MyActivity.this, "Your toast message.",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(MyActivity.this, LessonActivity.class);
startActivity(intent);
}
});

return lessonView;
}

Android ListView setOnItemClickListener not working if item has a button with onClick

This is kind of View focus problem everyone newbie suffers from ;)

Simple solution:

Make all the children of your row_layout to
focusable=false, focusableInTouchMode = false
and add android:descendantFocusability="blocksDescendants" to your parent.

setOnItemClickListener doesn't work for a button in a ListView

Make a custom list adapter like this

public class CustomListAdapter extends BaseAdapter {
private String[] items;
private LayoutInflater inflater;
Context context;
public CustomListAdapter(Context c, String[] items) {
this.items = items;
inflater =LayoutInflater.from(c);
context = c;
}

@Override
public int getCount() {
return items.length;
}

@Override
public Object getItem(int location) {
return items[location];
}

@Override
public long getItemId(int position) {
return position;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, null);
TextView txt = (TextView) convertView.findViewById(R.id.list_item_textview);
ImageButton button = (ImageButton) convertView.findViewById(R.id.deleteButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "clicked ",
Toast.LENGTH_SHORT).show();
}
});
txt.setText(""+items[position]);

return convertView;
}

}

and in your fragment

    CustomListAdapter customListAdapter;
customListAdapter = new CustomListAdapter(getActivity(),str);
list.setAdapter(customListAdapter);

hope this will help you !!!

listview.setOnItemClickListener not working using CursorAdapter

Try setting your buttons (or any other views you want to handle click inside a list item) like this:

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

Reference: https://stackoverflow.com/a/6703671/1084174

setOnItemClickListener() not working on ListView (select always first row)

Replace your PhoneBook_row.xml with the one I am putting up here, I have tried this and it worked for me. I have changed all your android:clickable="true" to android:clickable="false". The reason for doing this is, all of your child views consume click and the result is your parent view i.e ListView not getting the click event. Hope this helps.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false">

<ImageView
android:id="@+id/imgAvatar"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="fitCenter"
android:src="@drawable/image"
android:clickable="false"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="false">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:textStyle="bold"
android:clickable="false"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvPhone"
android:textStyle="bold"
android:clickable="false"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvEmail"
android:textStyle="bold"
android:clickable="false"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvID"
android:textStyle="bold"
android:clickable="false"/>

</LinearLayout>

</LinearLayout>


Related Topics



Leave a reply



Submit