自定义列表视图与复选框不起作用

时间:2015-11-27 13:21:30

标签: android android-listview

我的自定义List视图有问题。我在哈希映射中有数据并添加到数组列表中,如下面的代码所示。我的问题是在我的哈希映射中包含20个值,我尝试设置到列表视图但第一个数据只显示其他或不显示。谢谢提前

这是我的CustomAdapter

package com.example.node10.databasetesting;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.example.node10.databasetesting.DataBase.*;

public class ListViewEmployee extends AppCompatActivity implements View.OnClickListener {

    private SimpleCursorAdapter dataAdapter;
    private Button btn_view;
    private Button btn_submit;
    ArrayList<HashMap<Integer,String>> emp;
    HashMap<Integer,String> map;
    Set<Integer> keyValue;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_employee);
        DataBase dataBase=new DataBase(this);
        map=new HashMap<>();
        map=dataBase.getValueEmpTable();
        emp=new ArrayList<HashMap<Integer, String>>();
        keyValue=map.keySet();
        emp.add(map);


        btn_view= (Button) findViewById(R.id.id_btn_emp_view);
        btn_view.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
       if(v.getId()==R.id.id_btn_emp_view){
           displayList();
          // selectItem();
       }


    }



    private void displayList() {

        //create the Arrayt adapter
        ListView listview= (ListView) findViewById(R.id.id_listview);
        final MyAdapter adapter=new MyAdapter(this,R.layout.custom_listview,emp);

       listview.setAdapter(adapter);

    }




    public class MyAdapter extends ArrayAdapter<HashMap<Integer,String>>{
        boolean[] checkBoxState;
        ViewHolder viewholder;

        public MyAdapter(Context context, int resource,ArrayList<HashMap<Integer,String>> map) {
            super(context, resource, map);
            // create the boolean array for check box selection
            checkBoxState= new boolean[map.size()];
        }
        // create the class for caching the view
        class ViewHolder{
            TextView txtView;
            CheckBox checkBox;
        }

        @Override
        public View getView( final int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                LayoutInflater objlayout= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView=objlayout.inflate(R.layout.custom_listview,null);
                viewholder=new ViewHolder();
                viewholder.txtView= (TextView) convertView.findViewById(R.id.id_checkbox_textview);
                viewholder.checkBox= (CheckBox) convertView.findViewById(R.id.id_checkbox);

                convertView.setTag(viewholder);

            }else{
                viewholder = (ViewHolder) convertView.getTag();}


           // viewholder.txtView.setText(emp.get(position).toString());

            viewholder.txtView.setText(emp.get(position).get(position+1).toString());
            viewholder.checkBox.setChecked(checkBoxState[position]);
            viewholder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (((CheckBox) v).isChecked()) {
                        checkBoxState[position] = true;

                    } else {
                        checkBoxState[position] = false;
                    }
                }
            });




            return convertView;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

现在你把你的适配器放到HashMap的ArrayList中。我不知道你为什么这样做,但现在你的ArrayList emp包含一个元素 - 已添加到行

emp.add(map); 

因此,对于size = 1的数组,ArrayAdapter将显示1 elemet。 如果要在列表中显示地图中的所有元素。我建议用 ArrayList emp和comvert map to map to using map.values()

ArrayList<String> emp = (ArrayList<String>)map.values();

然后将适配器更改为

public class MyAdapter extends ArrayAdapter<String>