1

I'm using CheckBox to insert data from MainActivity to LikeActivity. I already use this code in 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;
}

Next, my question is how to put coding for add and remove function in if statement. So, when user tick in checkbox in MainActivity the selected item will add to LikeActivity and when untick the selected item will remove from LikeActivity.

Thanks.

  • read about intent.putextra() – Shreyans Mar 22 '16 at 07:01
  • you can manage using your local list which you can keep as static in your application and when you land to like activity you will load data from that list, if your like activity is open in background then you can use broadcast and handle broadcast in the like activity and achieve your goal. – Silvans Solanki Mar 22 '16 at 07:02
  • you can simply use a arraylist to add the data in if(ischecked) condition and remove it in else condition if it is already available in arraylist. –  Mar 22 '16 at 07:06
  • create a global class with array list and put and remove your data from/to it...check [this](http://stackoverflow.com/a/35183420/2553431) answer... – Iamat8 Mar 22 '16 at 07:12

4 Answers4

0

You can send A Data Using 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)

if Checked a Checkbox add Item in

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

getting Intent extras on LikeActivity

Intent extras = getIntent();
ArrayList<String> arraylist  = extras.getParcelableArrayList("arraylist");  
Iterator<String> iterator = arraylist.iterator();
    while (iterator.hasNext()) {
        // add Item on ListActivity  //iterator.next()
    }
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

You can have this changes in checkbox code in adapter class

 ArrayList<String> checkedSelection = new ArrayList<String>();
    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            // add into arraylist
                               checkedSelection.add(holder.checkBox.getText().toString());
                        }else{
                            // remove from arraylist
                             checkedSelection.remove(position);
                        }
                    }
                });

after that send arraylist from mainactivity by using following intent

Intent i=new Intent(this,Route.class);
i.putStringArrayListExtra("list", ar);
startActivity(i);

and get that arraylist in 2nd activity like,

ArrayList<String> ar1=getIntent().getExtras().getStringArrayList("list");   
0

1.If you dont want immediate jump to MainActivity after the selection then Intent cant be a good choice.

Use a static boolean variable inside your main activity and set the value of that variable inside Adapter using MainActivity.isChecked = true/false ;

Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37
Saurabh Padwekar
  • 3,888
  • 1
  • 31
  • 37
0

Thanks for the tips and sharing. I already get the solution. Here my code and step:

  1. Add this code into if statement to add details into database

@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) {

                Name name = new Name(nameArrayList.get(position).getName(),
                        nameArrayList.get(position).getGender(),
                        nameArrayList.get(position).getEng(),
                        nameArrayList.get(position).getMy());
                name.save();

            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;}
  1. Create LikeAdapter.java to view the selected row in LikeActivity.class

Keep moving!

In progress...