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

时间:2015-08-10 09:31:40

标签: 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;
    }
}

2 个答案:

答案 0 :(得分:2)

您可以通过两种方式显示选择。

1]将choicemode设置为&#34; SingleChoice&#34; listView并使用自定义&#34; Checkable&#34;查看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 :(得分:0)

第1步

yourlistview.setItemChecked(iposition,true); //这里iposition是选定位置的int

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