0

In my android application i'm having a list view and some items in that,now my requirement is when I select a particular item from that list the list item should be highlighted with a red color, and that selection should not be disappear because in my application i should choose an item from the list view and i should click a button(submit).

So in order to know to the user that he has selected an item from the list view it should be highlighted until he clicks submit button. I have used choice Mode attribute and set the value to single choice and i have changed the color for highlighting everything works fine but i need it not to be disappeared until user clicks the submit button The variable val consists of some names which i retrieved from database. Please help me to solve this Thanks in advance.

ArrayAdapter<String> adptr= new ArrayAdapter<String>(this,R.layout.row,R.id.member_name,array);
lv.setAdapter(adptr);
lv.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int pos,
            long id) {
        // TODO Auto-generated method stub
        val=lv.getItemAtPosition(pos).toString();
        //Toast.makeText(getApplicationContext(), val, 5000).show();
    }
});
  • You can check this http://stackoverflow.com/questions/9281000/android-keep-listviews-item-highlighted-once-one-has-been-clicked?rq=1 – Janoti Jul 24 '14 at 06:53

2 Answers2

0
use OnItemClickListener
   ListView lview = getListView();
   int pos =0;
   lview.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
           // set the background of v here 
           if (position == pos)
           // reset the background color
           // and pos = position;
           else 
             get the previous listitem view and reset it's background to original
             }
   }); 
Janoti
  • 36
  • 5
0

Actually I had the same problem sometime back. I was working on non touch andorid ui application for google tv. The solution is like this.

1) create a custom ArrayAdapter (extending ArrayAdapter)

2) onItemClick get the position of the item.

3) send that position to your adapter by some public method say setCurrentPosition(int pos)

4) in getView() of the adapter check for the position and set the background to red for that view.

Hope this will work as it worked for me.

Janoti
  • 36
  • 5