jeudi 30 juillet 2015

Android::I want to add spinner checked value specific into database.

I think i asked some silly question but I really get problem to insert checked data on button click to table. When i checked value is added into array but when I deselect even that value is remove from array.. But at the end when I tried to submit button click that time data get inserted but any two extra entry will added at the end.. Can u please suggest me any solution for this.I am new in android development.

My code is as follow:
## My Adapter code ##

 @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
      LayoutInflater inflator = context.getLayoutInflater();
      view = inflator.inflate(R.layout.attend_layout, null);
     //currentAttendent = bioid.get(position).getBioid();
    //  Model planet = (Model) this.getItem(position);

      final ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) view.findViewById(R.id.tvstudentfname);
     // viewHolder.textbioid=(TextView) view.findViewById(R.id.textView1);
      viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox1);

     viewHolder.checkbox
          .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
              Model element = (Model) viewHolder.checkbox.getTag();
              element.setSelected(buttonView.isChecked());
             if(viewHolder.checkbox.isChecked()){

                  valuesList.add(element.getBioid());
                  //count++;
              }
             else if(!viewHolder.checkbox.isChecked()) {
                 //count--;
                 valuesList.remove(element.getBioid());

             }

            }
          });
   view.setTag(viewHolder);
      viewHolder.checkbox.setTag(bioid.get(position));
    } else {
      view = convertView;
      ((ViewHolder) view.getTag()).checkbox.setTag(bioid.get(position));
    }

    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName()+ " " + list2.get(position).getLname()+ " " + bioid.get(position).getBioid());
   //holder.text(bioid.get(position).getBioid());

    holder.checkbox.setChecked(bioid.get(position).isSelected());
    return view;

  }
  public static ArrayList<String> getcheckeditemcount()
  {
      return valuesList;
  }

} 

My insert query which I tried to insert data into database. and call on to submit button.

Insertion query

    public static void insertdata()
{

    // for (int i = 0; i < adapter.getCount(); i++)
    // {
    //     Model element = adapter.getItem(i);
         ArrayList<String> itempositions=MyAdapter.getcheckeditemcount();
      //   if (element.isSelected())
         for(int i=0;i<itempositions.size();i++)
         { 
                db.execSQL("INSERT INTO '"+classcode+"'(bioid,date,time,subject,slot) VALUES ('"+itempositions.get(i)+"','"+dt1+"','"+timevalue+"','"+subject+"','"+slot+"')");
                //id++;
         }

   }

My problem is that it insert data into database with bioid. but my problem statement is add selected value successfully. and even deselected value delete from array but it insert randomly 2 values extra in table. which is selected or deselected. Why it is happened?? Plz check my code above. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire