aditya kumar singh
aditya kumar singh

Reputation: 33

Unable to show checked List View Item in the next Activity

I Have an Activity with a ListView. ListView row consist of 3 TextView and a Checkbox. I want to show the checked list item to another activity. I am using Custom Adapter that extends Cursor Adapter. here's my code for Custom Adapter

class MyCursorAdapter extends CursorAdapter /*implements Serializable*/{
private LayoutInflater inflater;
ArrayList<String> selectedStrings = new ArrayList<>();
private String str;
TextView tv1,tv2,tv3;

public MyCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    View view = inflater.inflate(R.layout.simplecursorrowlayout, viewGroup, false);
    return view;
}

@Override
public void bindView(View view, Context context,final Cursor cursor) {
    tv1 =  view.findViewById(R.id.textView1);
    tv2 =  view.findViewById(R.id.textView2);
    tv3 =  view.findViewById(R.id.textView3);
    tv1.setText(String.valueOf(cursor.getInt(0)));
    tv2.setText(cursor.getString(1));
    tv3.setText(cursor.getString(2));
    final CheckBox checkBox = view.findViewById(R.id.cBox);
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(((CheckBox)view).isChecked()){
                tv1.setText(String.valueOf(cursor.getInt(0)));
                tv2.setText(cursor.getString(1));
            }
        }
    });
    /*checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b){
                //str=tv1.getText().toString()+"    "+tv2.getText().toString();
                //selectedStrings.add(str);
                tv1.setText(String.valueOf(cursor.getInt(0)));
                tv2.setText(cursor.getString(1));
            }
        }
    });*/
 /*   Intent intent = new Intent(view.getContext(),DisplayRecord.class);
    intent.putStringArrayListExtra("Selected_Students",selectedStrings);
    context.startActivity(intent);*/
    }

@Override
public boolean isEnabled(int position) {
    return true;
}

}

codes in comments are someother way that I've tried When using Intent to pass all those data, my app hangs with a Black screen.

Upvotes: 0

Views: 49

Answers (1)

pradithya aria
pradithya aria

Reputation: 687

I think you could expose the selectedStrings from the adapter.

class MyCursorAdapter extends CursorAdapter Serializable*/{

    public ArrayList<String> getSelectedStrings() {
        return selectedStrings;
    }
    // other existing codes
}

And then in the current activity, retrieve it when it's time to move to the next activity.

class MyActivity extends AppCompatActivity {

    public void displaySelectedItems() {
        Intent intent = new Intent(this, DisplayRecord.class);
        intent.putStringArrayListExtra("Selected_Students", adapter.getSelectedStrings());
        context.startActivity(intent);
    }

    // other MyActivity's code
} 

Upvotes: 1

Related Questions