android多选ListView和Textview
我想创build像下面的列表视图。
checkbox| Textview | Textview | Textview | Textview | Textview | Textview |
所有数据都来自数据库,并在textview中设置。
我试过,但我有一些问题
-
当我selectcheckbox和滚动列表,然后它的automaticaly uncheked。
-
如果我select第一个checkbox,那么第五个是自动检查。
-
全部检查并取消全部检查
请帮我,我尝试了很多代码,但我不成功。
提前致谢。
它可能会帮助你。
package com.Sample_MultipleSelection; import java.util.ArrayList; import android.R.integer; import android.app.Activity; import android.app.ListActivity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView; public class Sample_MultipleSelectionActivity extends ListActivity { private MyListAdapter adapter; ArrayList<String> item_id = new ArrayList<String>(); ArrayList<String> item_name = new ArrayList<String>(); ArrayList<String> item_balance = new ArrayList<String>(); ArrayList<String> item_email = new ArrayList<String>(); ArrayList<String> items = new ArrayList<String>(); private CheckBox chk_main; boolean flag = false; boolean[] selection; ArrayList<String> selection_val; private Button btn_select; private CheckBox chk_select; ViewHolder holder12; boolean select_all; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); item_id.add("1"); item_name.add("China"); item_balance.add("4000"); item_email.add("china@gmail.com"); item_id.add("2"); item_name.add("abc"); item_balance.add("4000"); item_email.add("abca@gmail.com"); item_id.add("3"); item_name.add("xyz"); item_balance.add("4000"); item_email.add("xyz@gmail.com"); item_id.add("4"); item_name.add("xyza"); item_balance.add("40070"); item_email.add("xyze@gmail.com"); item_id.add("5"); item_name.add("xyzc"); item_balance.add("1000"); item_email.add("xyz123@gmail.com"); final ViewHolder holder = new ViewHolder(); selection = new boolean[item_id.size()]; selection_val = new ArrayList<String>(); adapter = new MyListAdapter(this); setListAdapter(adapter); holder12 = new ViewHolder(); btn_select = (Button) findViewById(R.id.button1); btn_select.setOnClickListener(new OnClickListener() { public void onClick(View v) { int len = selection.length; int cnt = 0; String selectIds = ""; for (int i = 0; i < len; i++) { if (selection[i]) { cnt++; } } for (int i = 0; i < selection_val.size(); i++) { selectIds = selectIds + " | " + selection_val.get(i); } if (cnt == 0) { Toast.makeText(getApplicationContext(), "NO Selection", 1) .show(); } else { Toast.makeText( getApplicationContext(), "Your are Selected " + cnt + " ids. " + " " + selectIds, 1).show(); } } }); } public class MyListAdapter extends BaseAdapter { Context con; private LayoutInflater layoutinf; ArrayList<Boolean> itemChecked = new ArrayList<Boolean>(); ArrayList<String> items_ = new ArrayList<String>(); public MyListAdapter( Sample_MultipleSelectionActivity sample_MultipleSelectionActivity) { con = sample_MultipleSelectionActivity; } public int getCount() { return item_id.size(); } public Object getItem(int arg0) { return item_id.size(); } public long getItemId(int arg0) { return item_id.get(arg0).hashCode(); } public View getView(final int arg0, View arg1, ViewGroup arg2) { View v = arg1; ViewHolder holder = null; if (v == null) { layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = layoutinf.inflate(R.layout.row, null); holder = new ViewHolder(); holder.chk = (CheckBox) v.findViewById(R.id.checkBox1); holder.tv_name = (TextView) v.findViewById(R.id.textView1); holder.tv_bal = (TextView) v.findViewById(R.id.textView2); holder.tv_email = (TextView) v.findViewById(R.id.textView3); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } holder.chk.setId(arg0); holder.tv_name.setId(arg0); holder.chk.setOnClickListener(new OnClickListener() { public void onClick(View v) { try { CheckBox cb = (CheckBox) v; int id = cb.getId(); String val = cb.getText().toString(); if (selection[id]) { cb.setChecked(false); selection[id] = false; selection_val.remove("" + val); } else { cb.setChecked(true); selection[id] = true; selection_val.add("" + val); } adapter.notifyDataSetChanged(); } catch (Exception e) { Log.e("error", "" + e.toString()); } } }); holder.chk.setChecked(selection[arg0]); if(selection[arg0] == true) { holder.tv_name.setBackgroundColor(Color.GRAY); holder.tv_bal.setBackgroundColor(Color.GRAY); holder.tv_email.setBackgroundColor(Color.GRAY); } else { holder.tv_name.setBackgroundColor(Color.TRANSPARENT); holder.tv_bal.setBackgroundColor(Color.TRANSPARENT); holder.tv_email.setBackgroundColor(Color.TRANSPARENT); } holder.chk.setText(item_id.get(arg0)); holder.tv_name.setText("" + item_name.get(arg0)); holder.tv_bal.setText(item_balance.get(arg0)); holder.tv_email.setText(item_email.get(arg0)); return v; } } public class ViewHolder { private CheckBox chk; private TextView tv_name; private TextView tv_bal; private TextView tv_email; } }
main.xml中
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/chk_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:text="" /> <TextView android:id="@+id/textView1" android:layout_width="100dp" android:layout_height="wrap_content" android:padding="5dp" android:text="Name" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#66cc33" /> <TextView android:id="@+id/textView2" android:layout_width="100dp" android:layout_height="wrap_content" android:padding="5dp" android:text="Balance" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#66cc33" /> <TextView android:id="@+id/textView3" android:layout_width="100dp" android:layout_height="wrap_content" android:padding="5dp" android:text="Email-Id" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#66cc33" /> </LinearLayout> <View android:id="@+id/view1" android:layout_width="wrap_content" android:layout_height="2dp" android:background="#fff" /> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="314dp" android:layout_weight="1" > </ListView> <Button android:id="@+id/button1" android:focusable="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="select" /> </LinearLayout>
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" /> <TextView android:id="@+id/textView1" android:layout_width="100dp" android:layout_height="wrap_content" android:padding="3dp" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:padding="3dp" android:id="@+id/textView2" android:layout_width="100dp" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:padding="3dp" android:id="@+id/textView3" android:layout_width="100dp" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
如果它正确的话,就把它做对吧。
这是因为当你滚动列表视图时,它实际上会重新使用行来显示信息。 这极大地提高了通过长列表滚动的性能。
但是,这一点,让我们得到解决。 你需要做的是在checkbox被选中的情况下触发一个事件,这样该值可以被存储在数组中,最适合的是列表信息的其余部分,这样当再次显示视图时,该值可以被恢复正如在重新使用视图时正确地清除选中的状态。
这可以在自定义ArrayAdapter中的getView()方法内部完成。
@Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; if(row == null) { LayoutInflater inflater = LayoutInflater.from(this.getContext()); row = inflater.inflate(R.layout.category_color, parent, false); } if(this.getCount() > position) { DataObject data = this.getItem(position); CheckBox checkbox = (CheckBox)row.findViewById(R.id.checkbox); checkbox.setChecked(data.isChecked()); checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { data.setChecked(isChecked); }}); ... } return(row); }
这是因为每当你滚动你的列表视图时,它会重新创build它的行,并将其设置为默认值或一些时间的任何值。 所以,因为这是行不通的。 我已经回答了2-3次类似的问题。 这是解决scheme。