I’ve set a button as invisible in the layout and now i need to set it visible on long press of a list item..
I’ve created a class extending array adapter and i’m declaring the button in this class..
Now i need to access this button in the code for long press of the list item to set it visible..
How can i access this button in setOnItemLongClickListener..
Also the app force closes when declaring the button in arrayadapter..
here’s my code..
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//arg1.findViewById(R.id.btndelete).setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "long press", Toast.LENGTH_LONG).show();
return false;
}
});
class myAdapter extends ArrayAdapter<String>
{
Button btndlt;
View row;
public myAdapter(Context context,ArrayList<String> objects)
{
super(context, android.R.layout.simple_list_item_1, objects);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.list_item, parent, false);
btndlt = (Button) row.findViewById(R.id.btndelete);
}
}
If you are having a button in ListItem row then you can use
setTag()andgetTag()to get the instance of Button inonItemLongClick(),Pusedo Code,
inside
getView(),inside
onItemLongClick()Also, if you want to detect Swipe on an ListItem you can check my demo example
herewhich enables a delete button when ListItem is swiped from right to left.