Android:向Checkbox插入1个值,但它显示2个值?

时间:2014-04-21 14:01:36

标签: android checkbox android-checkbox

我只是想问一下关于向数据显示复选框的内容,现在,我只是成功将数据显示到checbox但是,现在我遇到麻烦,当我的数据库中有1个数据时,复选框显示2个数据?这是我的示例数据库

表Jurusan

id | nama

1    ipa

然后我有阅读这个数据库的功能

 public ArrayList<Jurusan> getAllLabel_jurusan()
    {
     ArrayList <Jurusan> point = new ArrayList<Jurusan>();
        String selectQuery = "SELECT id, nama_jurusan from jurusan";

       Cursor c = database.rawQuery(selectQuery, null);
       if( c.moveToFirst() );
       do
            {
                Jurusan j = new Jurusan();
                j.setId(c.getLong(0));
                j.setNama_jurusan(c.getString(1));
                // adding to todo list
                point.add(j);
            }
         while(c.moveToNext());   

        return point;
    }

这是我的MainActivity

 public class MainActivity extends Activity implements OnItemSelectedListener
 {
     Button myButton;
     String tmp_nama;
     long id_tampung;
     @Override
 public void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
      myButton = (Button) findViewById(R.id.findSelected);
      displayListView();
  checkButtonClick();
 }
  private void displayListView() 
  {
         ArrayList<Jurusan> stateList = dataSource.getAllLabel_jurusan();

                         //GETTING THE DATA FROM DATABASE
             id_tampung = stateList.get(0).getId();
             tmp_nama = stateList.get(0).getNama_jurusan().toString();
                             //THE SET INTO CONSTRUCTOR
             Jurusan _states = new Jurusan(id_tampung, tmp_nama);
                             //ADD TO ARRAY
             stateList.add(_states);
                    // create an ArrayAdaptar from the String Array
                    //SETTING INTO CustomAdapter
        dataAdapter = new MyCustomAdapter(this, R.layout.state_info,stateList);
        ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);
 }
  private class MyCustomAdapter extends ArrayAdapter<Jurusan> 
 {
        private ArrayList<Jurusan> stateList;

        public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<Jurusan> stateList) 
        {
            super(context, textViewResourceId, stateList);
            this.stateList = new ArrayList<Jurusan>();
            this.stateList.addAll(stateList);
        }
        private class ViewHolder
        {
                TextView id;
                CheckBox name;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ViewHolder holder = null;

            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null) 
            {

                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = vi.inflate(R.layout.state_info, null);

                holder = new ViewHolder();
                holder.id = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);

                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            Jurusan state = stateList.get(position);

            holder.id.setText(" (" + state.getId() + ")");
            holder.name.setText(state.getNama_jurusan());

            holder.name.setTag(state);

            return convertView;
            }
    }

我的问题是,复选框应该显示来自数据库的1个数据,但是当我运行此代码时,复选框显示2个具有相同值的数据,任何人都可以帮助我吗?

example:
checbox1  (value:ipa)
checkbox2 (value:ipa)

我希望它应该是这样的......

checkbox1 (value:ipa)

2 个答案:

答案 0 :(得分:1)

displayListView()中,您正在复制stateList中的第一个条目:

ArrayList<Jurusan> stateList = dataSource.getAllLabel_jurusan();

 if(stateList.size()>0)
 {
                     //GETTING THE DATA FROM DATABASE
         id_tampung = stateList.get(0).getId();
         tmp_nama = stateList.get(0).getNama_jurusan().toString();
                         //THE SET INTO CONSTRUCTOR
         Jurusan _states = new Jurusan(id_tampung, tmp_nama);
                         //ADD TO ARRAY
         stateList.add(_states);
 }

我不知道你的意图是什么,但你可能只是完全删除这个if块。

答案 1 :(得分:1)

问题在于您的代码,您在代码中添加了双重值。我在代码行之前添加了我的评论。

//statelist contains an arraylist with your required data.
ArrayList<Jurusan> stateList = dataSource.getAllLabel_jurusan();
 //you are checking here for zero.
 if(stateList.size()>0)
 {
     //GETTING THE DATA FROM DATABASE
     //here you are get id;
     id_tampung = stateList.get(0).getId();
     //here you are get name;
     tmp_nama = stateList.get(0).getNama_jurusan().toString();
                     //THE SET INTO CONSTRUCTOR
     //making an object of jurusan 
     Jurusan _states = new Jurusan(id_tampung, tmp_nama);
                     //ADD TO ARRAY
     // and adding again the same arraylist. so that cause duplication.
     //stateList already contained the data , which are you add this line.  
     stateList.add(_states);
}

是的,请将功能displayListView替换为如下。

private void displayListView() 
 {
     ArrayList<Jurusan> stateList = dataSource.getAllLabel_jurusan();
        /*
         //GETTING THE DATA FROM DATABASE
         id_tampung = stateList.get(0).getId();
         tmp_nama = stateList.get(0).getNama_jurusan().toString();
                         //THE SET INTO CONSTRUCTOR
         Jurusan _states = new Jurusan(id_tampung, tmp_nama);
                         //ADD TO ARRAY
         stateList.add(_states);*/
                // create an ArrayAdaptar from the String Array
                //SETTING INTO CustomAdapter
    dataAdapter = new MyCustomAdapter(this, R.layout.state_info,stateList);
    ListView listView = (ListView) findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);
}