android Multiple selection ListView & Textview

前端 未结 3 1057
借酒劲吻你
借酒劲吻你 2020-11-28 15:49

i want to create listview like below.

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

all data come from database and set in te

相关标签:
3条回答
  • 2020-11-28 16:21

    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("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>
    

    make it right it if it correct.

    0 讨论(0)
  • 2020-11-28 16:21

    This is because when you scroll a listview it actually re-uses the rows to display the information. This greatly improves the performance of scrolling through long lists.

    But that aside, lets get it fixed. What you need to do is trigger an event when the checkbox is selected so the value can be stored in the array, most suitably with the rest of the list information, so that when the view is shown again the value can be restored, as well as correctly clearing the checked state when the View is re-used.

    This can all be done inside the getView() method within your custom ArrayAdapter.

    @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);
    }
    
    0 讨论(0)
  • 2020-11-28 16:24

    That is because every time when you scroll your listview it recreates its row and set it default value or some time any value. So because of that it is not working. I have answered similar question 2-3 times already. Here is the solution.

    0 讨论(0)
提交回复
热议问题