Question

I just want to ask something about showing data to checkbox, right now, I just have successful to showing data into checbox but, now I get trouble, when I have 1 data in my database, the checkbox show 2 data? here's my example database

Table Jurusan

id | nama

1    ipa

then I have function to read this database

 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;
    }

and this is my 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;
            }
    }

My question is, the checkbox should showing 1 data from database, but when i run this code, the checkbox is showing 2 data with same value, can anybody help me?

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

i hope it should be like this...

checkbox1 (value:ipa)
Was it helpful?

Solution 2

The problem is in your code, you are add double values in your code. i added my comment before line of code.

//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);
}

yes , please replace the function displayListView as below.

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);
}

OTHER TIPS

In your displayListView(), you're duplicating the first entry in the 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);
 }

I don't know what your intention here is but you probably could just remove this if block altogether.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top