Android(以编程方式突出显示listview中的当前选定选项)

敢说敢作敢为, 无怨无恨无悔。这篇文章主要讲述Android:以编程方式突出显示listview中的当前选定选项相关的知识,希望能为你提供帮助。
我有一个列表,其中向用户显示了一些选项。我还有两个按钮用于下一个和上一个。在下一个新选项从数据库绑定。
问题:
按下上一个按钮,我想显示之前选择的状态。可悲的是,我无法突出显示所选行。

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

功能GetPrevious选择选项
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 } }

我在前一个按钮上尝试的是什么
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); }

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

答案有两种方法可以显示选择。
1]将choicemode设置为listView的“SingleChoice”并使用自定义“Checkable”查看find here 。
2]覆盖getView()并根据某个成员变量更改背景。
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; }

另一答案步骤1
yourlistview.setItemChecked(iposition,true); //这里iposition是所选位置的int
【Android(以编程方式突出显示listview中的当前选定选项)】第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);


    推荐阅读