Question

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);
            }
}    
Was it helpful?

Solution

If you are having a button in ListItem row then you can use setTag() and getTag() to get the instance of Button in onItemLongClick(),

Pusedo Code,

inside getView(),

convertView.setTag(R.id.button, button);

inside onItemLongClick()

Button button = (Button)view.getTag(R.id.button);

Also, if you want to detect Swipe on an ListItem you can check my demo example here which enables a delete button when ListItem is swiped from right to left.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top