android多选ListView&的TextView

时间:2012-05-07 11:13:18

标签: android android-layout

我想创建如下所示的列表视图。

复选框| Textview | Textview | Textview | Textview | Textview | Textview |

所有数据都来自数据库并在textview中设置。

我试过但我遇到了一些问题

  1. 当我选择复选框并滚动列表时,它会自动取消选中。

  2. 如果我选中第一个复选框,则自动检查第5个。

  3. 检查所有&取消所有

  4. 请帮助我,我尝试了很多代码,但我没有成功。

    提前感谢。

3 个答案:

答案 0 :(得分:5)

它可能对你有所帮助。

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>

如果纠正,请将其改正。

答案 1 :(得分:1)

这是因为当您滚动列表视图时,它实际上会重新使用这些行来显示信息。这大大提高了滚动浏览长列表的性能。

但除此之外,让我们解决它。您需要做的是在选中复选框时触发事件,以便可以将值存储在数组中,最适合使用列表信息的其余部分,这样当再次显示视图时,也可以恢复该值,以及在重新使用View时正确清除已检查状态。

这可以在自定义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);
}

答案 2 :(得分:0)

这是因为每次滚动列表视图时,它都会重新创建其行并将其设置为默认值或某个时间任何值。因此,它不起作用。我已经回答过类似的问题2-3次了。 Here是解决方案。