我有Activity
ListView
。 ListView
行由3 TextView
和Checkbox
组成。我想将选中的列表项显示给另一个activity
。我使用Custom Adapter
扩展Cursor Adapter
。这是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;
}
}
评论中的代码是我尝试过的另一种方式
使用Intent
传递所有这些数据时,我的应用会以黑屏挂起。
答案 0 :(得分:1)
我认为您可以从适配器公开selectedStrings
。
class MyCursorAdapter extends CursorAdapter Serializable*/{
public ArrayList<String> getSelectedStrings() {
return selectedStrings;
}
// other existing codes
}
然后在当前活动中,在它移动到下一个活动时检索它。
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
}