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;
}
}