lemoncodes
lemoncodes

Reputation: 2421

Android: Click Listener Actions

I don't know to explain this but here i go, so i have a populated list... so each item i long press a item from that list an image button will appear its a Delete button actually.. so when i long press another item on the list i want to make that button on the first item i clicked to be hidden... its like every time i long click an item a image button appears and when i click another item, that button will be hidden and the button from which i clicked the new item will be shown.. any inputs as to how to do this will be much appreciated, or if u can give me some effecient way in doing this.. please do share. tnx much

Upvotes: 0

Views: 355

Answers (3)

Ben Ruijl
Ben Ruijl

Reputation: 5123

You could just keep track of the previous delete button, since only one is allowed. In the long click listener you can delete the previous button and create a new one.

ImageButton prevDelete = null;

...

list.setOnItemLongClickListener (new OnItemLongClickListener() {
  public boolean onItemLongClick(AdapterView parent, View view, int position, long id)      {
        deleteButton(prevButton); // check if not null in the function
        prevDelete = createButton(view); // draw button at view
        return true;
  }
});

You have probably already implemented the functions createButton and deleteButton.

Upvotes: 1

Rahmathullah M
Rahmathullah M

Reputation: 2716

Try this,

Assuming you're using a Adapter for the list...

    final ImageButton prevButton=null;
    row.setOnLongClickListener(new OnLongClickListener() {          
        @Override
        public boolean onLongClick(View arg0) {
            ImageButton currButton=row.findViewById(R.id.<button_id>);
            currButton.setVisibility(View.VISIBLE);
            if(prevButton!=null)
                prevButton.setVisibility(View.gone);
            prevButton=currButton;
            return true;
        }
    });

Upvotes: 1

Errol Dsilva
Errol Dsilva

Reputation: 177

The question is pretty confusing... But from what understood i figure out you need to detect a long press on a list item. Have you tried the AdapterView.OnItemLongClickListener.

somelist.setOnItemLongClickListener (new OnItemLongClickListener() {
  public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
    //do your stuff of showing\hiding button here...
  }
});

Upvotes: 0

Related Questions