【问题标题】:How to select item in the list view programmatically如何以编程方式在列表视图中选择项目
【发布时间】:2016-06-16 09:05:43
【问题描述】:

我有一个ArrayList<String> List,其中包含listview all_list 中的一些项目。如何通过检查ArrayList<String> List 内容以编程方式在列表视图 all_list 中选择这些项目?

例如,listview all_list 包含 [0] 苹果 [1] 橙色 [2] 香蕉

ArrayList<String> List 中,我有橙色,所以我希望自动选择(突出显示)listview all_list 中位置 1 上的项目。

我尝试过使用all_list.setItemChecked(),但它什么也没做并关闭了应用程序。我在列出适配器后执行操作。

【问题讨论】:

  • @A KA 你可以使用 ListView 方法 setSelection(position)
  • 我可以看看你正在尝试的代码吗?
  • all_list.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL); all_list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, final int position, long id, boolean check) { if (all_list.isItemChecked(position)) { all_list.setItemChecked(2,true); int what = (int) all_list.getItemIdAtPosition(position); }
  • stackoverflow.com/questions/19274013/…我希望这是你想做的。

标签: android arrays list selection


【解决方案1】:

将 onItemClickListener 设置为列表视图,以便在单击时设置一个布尔标志,将每行中的复选框设置为选中。然后调用 notifyDataSetChanged()

【讨论】:

    【解决方案2】:

    试试这个

    MainActivity.java

    package com.example.multiseekbar;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;
    
    
    public class MainActivity extends Activity {
    
        ListView listView1;
    
        ArrayList<ModelClass> modelClass = new ArrayList<ModelClass>();
        FruitSelectAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            modelClass.add(new ModelClass("Orange", true));
            modelClass.add(new ModelClass("Apple", false));
            modelClass.add(new ModelClass("Banana", false));
            modelClass.add(new ModelClass("Grapes", false));
    
            listView1 = (ListView) findViewById(R.id.listView1);
            adapter = new FruitSelectAdapter(MainActivity.this, modelClass);
            listView1.setAdapter(adapter);
    
            listView1.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    if(modelClass.get(arg2).isSelected()){
                        modelClass.get(arg2).setSelected(false);
                    }else{
                        modelClass.get(arg2).setSelected(true);
                    }
    
                    adapter.notifyDataSetChanged();
                }
            });
    
    
        }
    
    }
    

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.multiseekbar.MainActivity" >
    
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
             >
        </ListView>
    
    </RelativeLayout>
    

    FruitSelectAdapter.java

    package com.example.multiseekbar;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Color;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class FruitSelectAdapter extends BaseAdapter
    {
    
        private Activity activity;
        private LayoutInflater inflater;
    
        private ArrayList<ModelClass> modelClass=null;
    
        public FruitSelectAdapter(Activity activity, ArrayList<ModelClass> modelClass) {
            this.activity = activity;
            this.modelClass = modelClass;
        }
    
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return modelClass.size();
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return modelClass.get(position);
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final ViewHolder holder;    
            if (inflater == null)
                inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            if (convertView == null) {
                holder =new ViewHolder();
                convertView = inflater.inflate(R.layout.row1, null);            
                holder.txtFruitName = (TextView)convertView.findViewById(R.id.txtFruitName);
                holder.cbFruitSelectStatus = (CheckBox)convertView.findViewById(R.id.cbFruitSelectStatus);
                holder.linLayBackground = (LinearLayout) convertView.findViewById(R.id.linLayBackground);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
    
            holder.txtFruitName.setText(modelClass.get(position).getFruitName());
            holder.cbFruitSelectStatus.setChecked(modelClass.get(position).isSelected());
    
            if(modelClass.get(position).isSelected()){
                holder.linLayBackground.setBackgroundColor(Color.parseColor("#80ccff"));
            }else{
                holder.linLayBackground.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }
    
            return convertView;
        }
    
        class ViewHolder{       
            TextView txtFruitName;
            CheckBox cbFruitSelectStatus;
            LinearLayout linLayBackground;
        }
    
    }
    

    row1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/linLayBackground"
        android:layout_height="70dp"
        android:orientation="horizontal"
         >
    
        <TextView 
            android:id="@+id/txtFruitName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Fruit name"
            android:layout_weight="1"
            android:textSize="16sp"
            android:textColor="#000000" />
    
    
        <CheckBox 
            android:id="@+id/cbFruitSelectStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false" />
    
    </LinearLayout>
    

    ModelClass.java

    package com.example.multiseekbar;
    
    public class ModelClass {
    
        String fruitName;
        boolean isSelected=false;
    
        public ModelClass(String fruitName, boolean isSelected) {
            this.fruitName = fruitName;
            this.isSelected = isSelected;
        }
    
        public String getFruitName() {
            return fruitName;
        }
    
        public void setFruitName(String fruitName) {
            this.fruitName = fruitName;
        }
    
        public boolean isSelected() {
            return isSelected;
        }
    
        public void setSelected(boolean isSelected) {
            this.isSelected = isSelected;
        }
    
    
    }
    

    【讨论】:

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