简体   繁体   English

Android:以编程方式在列表视图中突出显示“当前选定的”选项

[英]Android: Highlight Currently Selected option in listview programmatically

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 函数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
    }
 }

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

Two ways you can show the selection. 有两种显示选择的方法。

1] set choicemode as "SingleChoice"to listView and use a custom "Checkable" View find here . 1]将choicemode设置为“ SingleChoice”以进行listView并使用自定义的“ Checkable” View 在此处查找

2] Override getView() and change the background based on some member variable. 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;
        }

Step 1 第1步

yourlistview.setItemChecked(iposition, true); yourlistview.setItemChecked(iposition,true); //here iposition is an int to the selected position //这里iposition是所选位置的int

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM