Question

I have an Android application. In one of my Activities which is derived from ListActivity, I've implemented the OnItemLongClickListener. I want to enable a delete button within the relevant list item where the ListItem has been LongClicked. How can I do this?

    OnItemLongClickListener listener =  new OnItemLongClickListener(){
        public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
            Account a = null;
            a = (Account) av.getItemAtPosition(position);               
            Toast.makeText(AccountActivity.this, "Long Clicked : " + a.getAccountName(), Toast.LENGTH_LONG).show();

            //instead of the toast, I need to show/enable a button here...
        }
    };
    getListView().setOnItemLongClickListener(listener);
Was it helpful?

Solution 3

Suppose you had a Button inside ListView's row layout then you can make it visible true`

OnItemLongClickListener listener =  new OnItemLongClickListener(){
    public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
        Account a = null;
        a = (Account) av.getItemAtPosition(position);               
        v.findViewById(R.id.btnid).setVisiBility(View.VISIBLE);
    }
};`

OTHER TIPS

.xml

  <Button
  android:id="@+id/imgdelete"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:visibility="invisible"/>

.java

lv.setOnItemLongClickListener(new OnItemLongClickListener() {     

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int arg2, long arg3) {

       view.findViewById(R.id.imgdelete).setVisibility(View.INVISIBLE);
       return false;

 }            

});

in your get view method of adapter set unique id to your button

btn.setId(position);

then on your click listener

OnItemLongClickListener listener =  new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {    
    Button btn = (Button) v.findViewById(position);
    btn.setEnabled(true);
}
};`

You can add boolean flag isDeleteVisible to Account with default false value.

Then in OnItemLongClickListener set it to true and call adapter.notifyDataSetChanged()

In adapter's getView check isDeleteVisible and show or hide delete button.

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