开发者

ListView with ArrayAdapter: update model when checkbox clicked

开发者 https://www.devze.com 2023-02-28 15:08 出处:网络
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.

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().getSystem开发者_JS百科Service(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;
    }
}


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


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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消