Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

android Multiple selection ListView & Textview

i want to create listview like below.

check box | Textview | Textview | Textview | Textview | Textview | Textview |

all data come from database and set in textview.

i tried but i got some problem

  1. when i select checkbox and scroll list then its automaticaly uncheked.

  2. if i select first check box then 5th is automaticaly checked.

  3. check all & un-check all

please help me i tried many code but i am not success.

thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

it may help you.

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("[email protected]");

        item_id.add("2");
        item_name.add("abc");
        item_balance.add("4000");
        item_email.add("[email protected]");

        item_id.add("3");
        item_name.add("xyz");
        item_balance.add("4000");
        item_email.add("[email protected]");

        item_id.add("4");
        item_name.add("xyza");
        item_balance.add("40070");
        item_email.add("[email protected]");

        item_id.add("5");
        item_name.add("xyzc");
        item_balance.add("1000");
        item_email.add("[email protected]");

        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>

make it right it if it correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...