0

I have a list in which I show a few options to the user. I also have two buttons for next and previous. On next new options are bound from the database.

Issue:

On press of the previous button I want to show the previously selected state. Sadly I am unable to highlight the selected row.

 listviewoptions = (ListView)findViewById(R.id.lstviewoptionAptitude);
            
    listviewoptions.setOnItemClickListener(new OnItemClickListener() {
        

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,int position ,
                long arg3) {
            
             if (previouslySelectedItem != null)
                {
                    previouslySelectedItem.setBackgroundColor(Color.TRANSPARENT);
                            //getResources().getColor(R.color.pressed_color));
                }

             String Selectedcolor = "#fdc500";
             arg1.setBackgroundColor(Color.parseColor(Selectedcolor));
                       // getResources().getColor(R.color.default_color));

                previouslySelectedItem = arg1;
            // TODO Auto-generated method stub
             Toast.makeText(getApplicationContext(),"programitically Clicked", Toast. LENGTH_SHORT).show();
             response = position+1;

        }
    });

Function GetPrevious Selected Option

public void getoptionSelected(Integer StudentIDResponse , String QuestionIDResponse)
{
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor c = null;
    String selectQuestion = "Select * from TableResponse where StudentID = "+StudentIDResponse+" AND QuestionID ="+QuestionIDResponse;
    c = db.rawQuery(selectQuestion, null);
    if(  c.getCount() >0) {
     if (c.moveToFirst()) {
            do {
                
             strOptionResponseID = c.getString(c.getColumnIndex("QuestOptionID"));  
                
            } while (c.moveToNext());        
        }
    }
    else
    {
        //No response Found
    }
 }

What I am trying on previous Button

  public void PrevQuestion()
   {
       getoptionSelected(StudentID,QuestionID);
       
       if (strOptionResponseID !=null)
       {
          **1st Method I tried** 
           
           response = Integer.parseInt(strOptionResponseID);
           listviewoptions.performItemClick(
                   listviewoptions.getAdapter().getView(response, null, null),
                   response,
                    listviewoptions.getAdapter().getItemId(response));
           
          **2nd Method I tried**

           listviewoptions.performItemClick(listviewoptions.getAdapter().getView(3, null, null), 3, listviewoptions.getItemIdAtPosition(3));
           listviewoptions.setSelection(response);
           listviewoptions.getSelectedView().setSelected(true);
       }

Update

public class ListAdapter extends ArrayAdapter<Item> {

    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public ListAdapter(Context context, int resource, List<Item> items) {
        super(context, resource, items);
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        if (position == 1)
        {
             String Selectedcolor = "#fdc500";
            // arg1.setBackgroundColor(Color.parseColor(Selectedcolor));
            view.setBackgroundColor(Color.parseColor(Selectedcolor));
        }
        else
        {
            view.setBackgroundColor(Color.RED);
        }
        return view;
    }
}
Community
  • 1
  • 1
Tushar Narang
  • 1,997
  • 3
  • 21
  • 49
  • If you want highlight the selected row follow the steps that I have mentioned in the below link http://stackoverflow.com/questions/31805381/list-item-selector-does-not-show-rounded-corner-on-press?noredirect=1#comment51537586_31805381 – Somasundaram NP Aug 10 '15 at 10:33
  • you need to set the background for row item with selector file and follow the steps that I have mentioned in previous comment and let me know. – Somasundaram NP Aug 11 '15 at 09:49
  • I am able to highlight on touch.. I wan to achieve it programitically – Tushar Narang Aug 11 '15 at 10:03
  • Maintain a boolean variable for selected item and find the layout or view in adapter based on that boolean value you can change the view background in getview() method – Somasundaram NP Aug 11 '15 at 10:07

2 Answers2

2

Two ways you can show the selection.

1] set choicemode as "SingleChoice"to listView and use a custom "Checkable" View find here .

2] Override getView() and change the background based on some member variable.

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            if (this_is_the_selected_item)
            {
                view.setBackgroundColor(selectedcolor);
            }
            else
            {
                view.setBackgroundColor(Normal_color);
            }
            return view;
        }
manjusg
  • 2,275
  • 1
  • 22
  • 31
0

Step 1

yourlistview.setItemChecked(iposition, true); //here iposition is an int to the selected position

Step 2

List<String> options = db.getAllOptions(QuestionID);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                   R.layout.simple_list_item_activated_1, R.id.text1, options);

            listviewoptions.setAdapter(adapter);
Tushar Narang
  • 1,997
  • 3
  • 21
  • 49