3

I wrote the following code but it only works for first 3 list items and for the remaining it raises null pointer exception.

list.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,final int arg2, long arg3) {
            // TODO Auto-generated method stub

            for(int i = 0; i <=list.getLastVisiblePosition(); i++)
            {
                System.out.println("onItemLongClick"+i);
                if(i==arg2)
                {
                    (list.getChildAt(i).findViewById(R.id.mark)).setVisibility(View.VISIBLE);
                    (list.getChildAt(i).findViewById(R.id.deleteitem)).setVisibility(View.VISIBLE);

                }
                else{
                    (list.getChildAt(i).findViewById(R.id.mark)).setVisibility(View.GONE);
                    (list.getChildAt(i).findViewById(R.id.deleteitem)).setVisibility(View.GONE);
                }
            }

            (list.getChildAt(arg2).findViewById(R.id.deleteitem)).setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(AllMessagesActivity.this, "Delete at" +arg2, Toast.LENGTH_LONG).show();
                }
            });
         (list.getChildAt(arg2).findViewById(R.id.mark)).setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(AllMessagesActivity.this, "Mark Spam at" +arg2, Toast.LENGTH_LONG).show();
                }
            });
            return false;
        }
    });

In the list item only one text item and two buttons. When I click on one list item then only the two buttons of that item has to visible and the remaining list item buttons should not visible. And when you click other list item then the previous selected item buttons also disable. The above code works only for the first 3 items of list but it is not working for all.

Thank you in advance.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
NaseerKhan
  • 65
  • 2
  • 10

1 Answers1

1

You need to handle this in your adapter for this list view. See similar questions:

How to disable a child item in a listview(make its color gray and unclickable)

Android: How to disable list items on list creation

How to disable items in a List View?

This tutorial is also useful:

http://www.vogella.com/articles/AndroidListView/article.html

Specifically, you probably want to look at section 2 of that tutorial where he shows how to change the list item that gets selected. You'll do something similar in your case, except you'll change everyone else, and maybe do some book-keeping to keep track of the "active" list item.

Good luck.

Community
  • 1
  • 1
Joey
  • 464
  • 2
  • 9