【问题标题】:Android: Highlight Currently Selected option in listview programmaticallyAndroid:以编程方式在列表视图中突出显示当前选择的选项
【发布时间】:2015-08-10 09:31:40
【问题描述】:

我有一个列表,其中向用户显示了一些选项。我还有两个按钮用于下一个和上一个。在下一个新选项从数据库中绑定。

问题:

按下上一个按钮时,我想显示之前选择的状态。遗憾的是,我无法突出显示选定的行。

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

我在上一个按钮上的尝试

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

【问题讨论】:

  • 如果您想突出显示选定的行,请按照我在以下链接中提到的步骤stackoverflow.com/questions/31805381/…
  • 您需要使用选择器文件设置行项目的背景,并按照我在之前评论中提到的步骤告诉我。
  • 我可以在触摸时突出显示.. 我希望以编程方式实现它
  • 为所选项目维护一个布尔变量,并根据该布尔值在适配器中查找布局或视图,您可以在 getview() 方法中更改视图背景

标签: android listview


【解决方案1】:

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

1] 将choicemode 设置为“SingleChoice”来listView 并使用自定义的“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;
        }

【讨论】:

  • Activity类型的getView(int, View, ViewGroup)方法未定义
  • @tusharnarang get View 方法适用于列表适配器。覆盖适配器检查stackoverflow.com/questions/8166497/…
  • 请在我的问题中查看更新的部分......它也不起作用
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 2014-05-28
    • 2019-03-12
    相关资源
    最近更新 更多