ListView 与 ArrayAdapter:单击复选框时更新模型

发布于 2024-11-03 03:03:49 字数 1437 浏览 2 评论 0原文

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

我如何才能使单击复选框会更改相应的模型?像这样的东西:

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

I have a List, and ArrayAdapter that could display it (basically as list of CheckBox). MyObejct have few fields, one of them is boolean enabled.

How I could make, that clicking on CheckBox will change corresponding model? Something like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好听的两个字的网名 2024-11-10 03:03:49

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

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

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

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

The way I would handle this is by making a method that initialises the listview, and once anything is changed, it is re-initialised.

Doing a call like below every time an entry in the listview is changed should make it appear as you want

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

However I understand that this could be computationally expensive compared to other methods the SO community may use

倾听心声的旋律 2024-11-10 03:03:49

您需要在适配器上调用 notifyDataSetChanged() 方法。这将告诉 ListView 底层数据模型已更改并且需要更新。不需要创建新的Adapter,只需更新其中的项目,然后调用我提到的方法即可。

You need to call the notifyDataSetChanged() method on the Adapter. This will tell the ListView that the underlying data model has changed and it needs to update. There's no need to create a new Adapter, just update the items in it and then call the method I mentioned.

半窗疏影 2024-11-10 03:03:49

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

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

祝你好运:)

Here is the working example. I have implemented in my one project

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

Good Luck :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文