我正在使用CheckBox将数据从MainActivity插入到LikeActivity。
我已经在myAdapter.java中使用此代码:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.row_name, null);


    // Get TextView
    TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
    TextView tvEng = (TextView) convertView.findViewById(R.id.tvEng);
    TextView tvMy = (TextView) convertView.findViewById(R.id.tvMy);

    CheckBox cbFavorite = (CheckBox) convertView.findViewById(R.id.cbFavorite);
    cbFavorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        // Check if it is favorite or not
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked) {


                Toast.makeText(context.getApplicationContext(), "Add to Favorite", Toast.LENGTH_LONG).show();
                Log.e("set", "true");
            } else {


                Toast.makeText(context.getApplicationContext(), "Remove from Favorite", Toast.LENGTH_LONG).show();
                Log.e("set", "false");
            }
        }
    });

    return convertView;
}


接下来,我的问题是如何在if语句中添加用于添加和删除功能的代码。因此,当用户在MainActivity中的复选框中打勾时,所选项目将添加到LikeActivity,而在取消选中时,所选项目将从LikeActivity中删除。

谢谢。

最佳答案

您可以使用Intent发送数据

ArrayList<String> arraylist = new Arraylist<String>();
Intent intent1 = new Intent(MainActivity.this,
                    LikeActivity.class);
Bundle b = new Bundle();
bundle.putParcelableArrayList("arraylist", arraylist);
intent1.putExtras(b)


如果选中了Checkbox添加项

arraylist.add("Checked 1");
arraylist.add("Checked 2");
..so on


在LikeActivity上获得Intent Extras

Intent extras = getIntent();
ArrayList<String> arraylist  = extras.getParcelableArrayList("arraylist");
Iterator<String> iterator = arraylist.iterator();
    while (iterator.hasNext()) {
        // add Item on ListActivity  //iterator.next()
    }

09-25 18:16