【问题标题】:ListView with ArrayAdapter: update model when checkbox clicked带有 ArrayAdapter 的 ListView:单击复选框时更新模型
【发布时间】:2013-09-10 15:37:50
【问题描述】:

我有一个列表和可以显示它的 ArrayAdapter(基本上作为 CheckBox 的列表)。 MyObejct 的字段很少,其中一个是启用布尔值的。

我怎么做,点击 CheckBox 会改变相应的模型?比如:

  public class MyObjectAdapter extends ArrayAdapter<MyObject>{

    private List<MyObject> items;

    public MyObjectAdapter(Context context, int textViewResourceId, List<MyObject> objects) {
        super(context, textViewResourceId, objects);

        this.items = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {

            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.myobject_checkbox, null);
        }
        MyObject myobject = items.get(position);

        if (myobject!=null){
            //checkbox
            CheckBox cbEnabled = (CheckBox) v.findViewById(R.id.enabled);
            if(cbEnabled != null){
                cbEnabled.setChecked(myobject.isEnabled());
                cbEnabled.setText(myobject.getName());
                cbEnabled.setTag(position);
                cbEnabled.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                    //how change model. E.g. myobject.setEnabled(((CheckBox) v).isChecked())
                  }
              });
            }
        }

        return v;
    }
}

【问题讨论】:

  • 您能在此处发布更多代码,即您的适配器吗?

标签: android checkbox onclick android-arrayadapter


【解决方案1】:

我处理这个问题的方法是创建一个初始化列表视图的方法,一旦发生任何变化,它就会重新初始化。

每次更改列表视图中的条目时进行如下调用应该使其显示为您想要的样子

setListAdapter(new ArrayAdapter<String>(this, R.layout.myLayout, myVector));

但我知道,与 SO 社区可能使用的其他方法相比,这在计算上可能会很昂贵

【讨论】:

    【解决方案2】:

    您需要在 Adapter 上调用 notifyDataSetChanged() 方法。这将告诉 ListView 底层数据模型已更改并且需要更新。不需要新建Adapter,更新里面的item,然后调用我说的方法就可以了。

    【讨论】:

    • 另外,注意 ListViews 中的复选框,你想让 ListItem 和 Checkbox 都可以点击吗?通常不鼓励这样做(但并非不可能完成)。处理这个问题的最好方法是有一个 ItemOnClickListener,当他们点击你的 ListItem 时,你会更新你的复选框视图以进行检查。
    • 只有复选框可点击就足够了。但是我在哪里更新 MyObject (myobject.setEnabled(boolean))?能否请您给出代码示例?
    • 对,看到这个 (developer.android.com/guide/topics/ui/binding.html),寻找处理用户选择。您可以在 OnItemClickListener 的 onClick 方法中更新对象的模型。在其中,使用适配器变量来检索您的对象(adapter.getItem(position))并在那里设置启用标志。此外,如果您遇到焦点问题,请参阅以下问题:stackoverflow.com/questions/1518338/…
    【解决方案3】:

    这是一个工作示例。我已经在我的一个项目中实现了

    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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
       <Button android:id="@+id/checkAll"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Select All"/>
    
       <Button android:id="@+id/uncheckAll"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Unselect All"
           android:layout_toRightOf="@id/checkAll"/>
    
        <ListView 
            android:id="@+id/listView"
            android:layout_below="@id/checkAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
        </ListView>
    
    </RelativeLayout>
    

    checkboxrow.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <CheckBox android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"/>
    
    </LinearLayout>
    

    MainActivity.java

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
    
        private ListView listView;
        private Boolean checkAll=false;
        private MySimpleArrayAdapter adapter;
        private Button buttoncheckAll;
        private Button buttonuncheckAll;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            listView=(ListView)findViewById(R.id.listView);
    
            String[] array={"one","two","three","four","five"};
    
    
            adapter = new MySimpleArrayAdapter(MainActivity.this, array);
    
            listView.setAdapter(adapter);
            listView.setItemsCanFocus(false);
              listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
            buttoncheckAll=(Button)findViewById(R.id.checkAll);
            buttoncheckAll.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    checkAll=true;
                    adapter.notifyDataSetChanged();
                }
            });
    
            buttonuncheckAll=(Button)findViewById(R.id.uncheckAll);
            buttonuncheckAll.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    checkAll=false;
                    adapter.notifyDataSetChanged();
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        class MySimpleArrayAdapter extends ArrayAdapter<String> {
              private final Context context;
              private final String[] values;
    
              public MySimpleArrayAdapter(Context context, String[] values) {
                super(context, R.layout.checkboxrow, values);
                this.context = context;
                this.values = values;
              }
    
              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = inflater.inflate(R.layout.checkboxrow, parent, false);
                CheckBox checkbox=(CheckBox)rowView.findViewById(R.id.checkBox);
                checkbox.setText(values[position]);
                if(checkAll){
                    checkbox.setChecked(true);
                }else{
                    checkbox.setChecked(false);
                }
    
                return rowView;
              }
            } 
    }
    

    祝你好运:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多