OnItemClickListener doesn't work, but OnLongItemClickListener does work in Custom Listview
I have a custom one listview
with a custom adapter. I want to click on elements listview
and do something. OnItemClickListener
does not work. But I have implemented OnLongItemClickListener
and it works great.
MainActivity
public class MainActivity extends Activity {
ArrayList<Product> products = new ArrayList<Product>();
Adapter listviewAdapter; //custom adapter object
ListView listview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.lvMain);
listview.setLongClickable(true);
listviewAdapter = new Adapter(this, products);
listview.setAdapter(listviewAdapter);
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) { //this works
Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
return false;
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) { //does not work
Toast.makeText(getApplicationContext(), " pressed", Toast.LENGTH_SHORT).show();
}
});
}
UPDATE custom adapter Adapter
public class Adapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
TextView itemname,itemprice;
Adapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
itemname= ((TextView) view.findViewById(R.id.tvDescr));
itemname.setText(p.name);
itemprice=((TextView) view.findViewById(R.id.tvPrice));
itemprice.setText(p.price + "");
CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
cbBuy.setOnCheckedChangeListener(myCheckChangList);
cbBuy.setTag(position);
cbBuy.setChecked(p.selected);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
return view;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
ArrayList<Product> getBox() {
ArrayList<Product> selected = new ArrayList<Product>();
for (Product p : objects) {
if (p.selected)
selected.add(p);
}
return selected;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
getProduct((Integer) buttonView.getTag()).selected = isChecked;
}
};
}
custom listview item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/cbBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
</CheckBox>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lvMain"
android:layout_width="match_parent"
android:layout_height="0dp"
android:longClickable="true">
</ListView>
source to share
@Amsheer your code is working. But for this you had to change the custom adapter and write actions for events onClick
in it. I wanted to write events onClick
in ManiActivity. I found a workaround. I removed this from the custom adapter
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
source to share
This is because of your CheckBox. You can decide the following:
Add an id to Linringayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:id="@+id/main_layout">
And in your getView
LinearLayout main_layout = (LinearLayout)view.findViewById(R.id.main_layout));
main_layout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listview .performItemClick(view,
listview.getPositionForView(view),
listview.getPositionForView(view));
}
});
Edit: Use the following code for long press
main_layout.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
listview.performLongClick();
return true;
}
});
source to share
You can specify the attribute android:onClick
in xml:
<CheckBox
android:id="@+id/cbBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:onClick="onCheckboxClicked" />
This will work when you check or uncheck the box.
Reason why onClick doesn't work but onLongClick does:
You applied onListItemClickListener
on the list row, so when you clicked on this checkbox, the event was consumed by this listener
listview.setOnItemClickListener(new OnItemClickListener() {
Update:
in getView()
:
// for list row
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("onClick","row click");
}
});
// for list row check box
view.cbBuy.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("onClick","checkbox click");
}
});
In MainActivity :
Remove this part:
/* listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) { //does not work
Toast.makeText(getApplicationContext(), " pressed", Toast.LENGTH_SHORT).show();
}
}); */
If the checkbox has the property android:focusable="true"
or false
, this code will run This is compiled and executable code Thank you
source to share