Skip to content Skip to sidebar Skip to footer

Transfer Checked Items In List-view To Another List-view On Button Click In Android

I have a listview with data using customAdapter.class now what i want is that to transfer checked items in listview to secondActivity on button click... btest.setOnClickListener(n

Solution 1:

The method you followed to send custom list to another activity will not work. In order to transfer your custom list between activities you need to create Parcelable List and send it through intent.

Android Intents does not support custom list.

Custom list can be passed in two ways, Serialization and Parcelable. But Parcelable is more Efficient and Simple to implement.

Refer this link to send custom list between activities through Parcelable

This link will give you much better idea to implement Parcelable.

Updated Code: Change your Model Code like below.

publicclassModelimplementsParcelable{
    privateString name;
    private int selected;

    publicModel(String name){
        this.name = name;
        selected = 0;
    }

    publicStringgetName(){
        return name;
    }

    public int isSelected(){
        return selected;
    }
    publicvoidsetSelected(boolean selected){
        this.selected = selected;
    }

@Overridepublic int describeContents() {
        // TODO Auto-generated method stubreturn0;
    }
 
    /**
    * Storing the Student data to Parcel object
    **/@OverridepublicvoidwriteToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(selected);
        
    }

 privateModel (Parcelin){
        this.name = in.readString();
        this.selected = in.readInt();
    }

publicstatic final Parcelable.Creator<Model> CREATOR = newParcelable.Creator<Model>() {
 
        @OverridepublicStudentcreateFromParcel(Parcel source) {
            returnnewStudent(source);
        }
 
        @OverridepublicModel[] newArray(int size) {
            returnnewModel[size];
        }
    };
}

Then in the MainActivity do this..

Intent next = new Intent(MainActivity , ResultActivity.class);
next.putParcelableArrayListExtra("model_data", (ArrayList<? extends Parcelable>) selectedItems);
startActivity(next);

In the ResultActivity do this.

ArrayList<Model> his = getIntent().getParcelableArrayListExtra("model_data");

Try the above code.. Good Luck..!!

Solution 2:

i solve by saving checked items from listview to sqlite on button click. another button to open new activity and call selected items sqlite this way... oncheckchange add and remove items in an arraylist and call this in onbutton click like this way...

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolderview=null;
    Supportsupport= (Support) this.getItem(position);
    if (convertView == null){
        LayoutInflaterinflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.view_items, null);
        view = newViewHolder();
        view.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
        view.cb = (CheckBox) convertView.findViewById(R.id.cb);
        convertView.setTag(view);

        view.cb.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
            @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                CheckBoxcb= (CheckBox) buttonView;
                Supportsupport= (Support) cb.getTag();
                support.setSelected(cb.isChecked());
                if (isChecked){
                    selList.add(support.status);
                    selID.add(support.id);
                    selType.add(support.type);
                  //  Toast.makeText(CustomAdapter.this, "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
                }else {
                    selList.remove(support.status);
                    selID.remove(support.id);
                    selType.remove(support.type);
                }
            }
        });
    }else{
        view = (ViewHolder) convertView.getTag();
        view.cb = view.getCb();
        view.tvInfo = view.getTvInfo();
    }
    view.cb.setTag(support);
    support = list.get(position);
    Stringid= support.getId();
    Stringstatus= support.getStatus();
    Stringtype= support.getType();
    view.cb.setChecked(support.isSelected());
   // view.tvInfo.setText(id + "," + status + "," + type);
    view.tvInfo.setText(status);
    return convertView;
}

this is button coding to add to db

 btest.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
           handler.addSelected(adapter.selList, adapter.selID, adapter.selType);

and this is how to insert to sqlite..

publicvoidaddSelected(ArrayList<String> selList, ArrayList<String> selID, ArrayList<String> selType) {

    int size = selID.size();

    SQLiteDatabase db = getWritableDatabase();
    try{
       for (int i = 0; i < size ; i++){
            ContentValues cv = new ContentValues();
          //  cv.put(KEY_ID, selID.get(i).toString());
           cv.put(KEY_ID, selID.get(i));
             cv.put(KEY_STATUS, selList.get(i));
           cv.put(KEY_TYPE, selType.get(i));
            Log.d("Added ",""+ cv);
            db.insertOrThrow(TABLE_SELECTED, null, cv);
        }
        db.close();
    }catch (Exception e){
        Log.e("Problem", e + " ");
    }
}

and get back from db like this

publicArrayList<String> getSelected() {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<String> result = null;
    try{
        result = newArrayList<String>();
      //  String query = "SELECT * FROM " + TABLE_SELECTED;String query = "SELECT " + KEY_ID + " FROM " + TABLE_SELECTED;

        Cursor c = db.rawQuery(query, null);
        if (!c.isLast()){
            if (c.moveToFirst()){
                do{
                    String sel_name = c.getString(c.getColumnIndex("_id"));
                    result.add(sel_name);
                    Log.d("Added ", sel_name);
                }while (c.moveToNext());
            }
        }
        c.close();
        db.close();
    }catch (Exception e){
        Log.e("Nothing is to show", e + " ");
    }
    return result;
}

Post a Comment for "Transfer Checked Items In List-view To Another List-view On Button Click In Android"