2011-04-24 77 views
0

我有一个列表和ArrayAdapter可以显示它(基本上作为CheckBox的列表)。 MyObejct只有几个字段,其中一个是启用布尔值的。ListView与ArrayAdapter:点击复选框时的更新模型

我该怎么做,点击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; 
    } 
} 
+0

你能在这里发布更多的代码,即您的Adapte R' – 2011-04-24 12:03:50

回答

1

的方式,我会处理这个问题是使该初始化列表视图的方法,一旦任何改变,它被重新初始化。

执行CALL像每一个在列表视图中的条目的变化,否则让你想

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

不过据我所知,这可能与其它方法相比多所社区可以使用在计算上昂贵的它出现的时间如下

0

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

+1

此外,请注意ListViews内的复选框,您是否希望ListItem和Checkbox都可点击?这通常是不鼓励的(但并非不可能完成)。处理这个问题的最好方法是拥有一个ItemOnClickListener,当他们点击ListItem时,然后更新复选框视图进行检查。 – dmon 2011-04-24 16:23:49

+0

只有可点击复选框就足够了。但我在哪里更新MyObject(myobject.setEnabled(布尔))?请问,请给出代码示例? – 2011-04-24 23:29:56

+0

对,请查看(http://developer.android.com/guide/topics/ui/binding.html),查找处理用户选择。您可以使用OnItemClickListener的onClick方法更新对象的模型。在它中,使用适配器变量来检索你的对象(adapter.getItem(position))并在那里设置启用标志。 此外,如果您遇到焦点问题,请参阅此问题:http://stackoverflow.com/questions/1518338/setonitemclicklistener-not-working-on-custom-listview-android – dmon 2011-04-24 23:41:00

0

这里是工作示例。我在一个项目实施

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

好运:)