0

I have a method getting called from inside OnClick callback method.

public void onClick(View v) {
    switch (v.getId()){
        case R.id.irating:
            ilist();
            break;

The ilist() method looks like

public Arraylist ilist() {
    final ArrayList selectedratings = new ArrayList();
    // Have alert dialog with multichoice options and 
    // Using Shared preference to store selected values and remind in dialog     
    return selectedratings; 
}

Now, how can I get the selectedrating arraylist inside another OnClick method of button and i need to pass the selectedrating list to another activity.

Parent Activity:

public class AllMovieRating extends ActionBarActivity implements View.OnClickListener {

private Views mViews;
final ArrayList ilist = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_movie_rating);

    mViews=new Views();
    mViews.irating.setOnClickListener(this);


}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.imdb_rating:
            ilist();
            break;

    }

}

private void filtervalues() {
    Intent intent = new Intent(AllRating.this,sharedcollect.class);
    intent.putExtra("im",ilist);
    startActivity(intent);

}


public ArrayList ilist() {

    final CharSequence[] ratings = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    final boolean[] ratingschecked = {false, false, false, false, false, false, false, false, false, false};
    SharedPreferences sharedPreferences = this.getSharedPreferences("checkedrate_i", Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sharedPreferences.edit();

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    int size = sharedPreferences.getInt("size", 0);
    for(int j=0;j<size;j++)
    {
        ilist.add(sharedPreferences.getString("selectedratings" + j, null));
       //Log.e("Kumar", "" + selectedratings);
    }

    for(int j=0;j<=ratingschecked.length;j++){
        if(ilist.contains((String.valueOf(j)))) {
            ratingschecked[j-1] = true;
        }
    }

    builder.setTitle("Select Ratings");
    builder.setMultiChoiceItems(ratings, ratingschecked, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            if (isChecked) {
                if(!ilist.contains((String)String.valueOf(ratings[which]))){
                    ilist.add(String.valueOf(ratings[which]));

                    ratingschecked[which]=true;
                }
            } else if ((ilist.contains((String)String.valueOf(ratings[which])))) {
                ilist.remove(String.valueOf(ratings[which]));
               // Log.e("Kumar", String.valueOf(ratings[which]));
                ratingschecked[which]=false;

            }
        }
    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            // editor.putString("checked", String.valueOf(selectedratings));
            for (int i = 0; i < ilist.size(); i++) {
                editor.putString("selectedratings" + i, String.valueOf(ilist.get(i)));
            }
            editor.putInt("size", ilist.size());
            editor.apply();
            //Log.e("Shiva", String.valueOf(selectedratings));


        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    AlertDialog dialog = builder.create();
    builder.show();
  return ilist;
}

child Activity:

public class sharedcollect extends ActionBarActivity {

ListView movielist;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sharedcollect);

    Intent i = getIntent();
    ArrayList<String> list = i.getStringArrayListExtra("im");




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_sharedcollect, menu);
    return true;
}

@Override
public void onBackPressed() {
    //do whatever to save the settings
    moveTaskToBack(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    switch (item.getItemId()){
        case R.id.action_settings:
           Intent intent = new Intent(sharedcollect.this,AllMovieRating.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    } }}
  • create callback from dialog – N J Sep 01 '15 at 17:35
  • If you are `// Using Shared preference to store selected values` why not use the same preferences in your other activity? – Sascha Kolberg Sep 01 '15 at 17:41
  • I don't know if its a good practice, but you can try declaring the selectedratings as a static variable. you can then access it anywhere – Jerry Sep 01 '15 at 17:42
  • Why don't you just store it in a run-time data structure of a class which provide gettersetter for this ArrayList. Or you can explore the option of storing the data in Sqlite in your app. Look here for more details on this option http://developer.android.com/training/basics/data-storage/databases.html – Saurabh Sep 01 '15 at 17:44
  • possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Tim Sep 01 '15 at 17:46
  • @Jerry Sangma solution is working good. – user29051986 Sep 02 '15 at 07:53
  • @user29051986 its good that it worked for you but keep the use of static variables to the minimum...its not a good practice .reason [link](http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil) – Jerry Sep 02 '15 at 09:18
  • @JerrySangma One issue. When i coming back to parent activity and going to child it is taking the empty list as arraylist is declared globally. how can i get the selected values? its is sending values when i open the alert dialog else it sending empty list. – user29051986 Sep 02 '15 at 16:59
  • can you provide me your updated code for your parent activity and the child activity? – Jerry Sep 02 '15 at 19:42

0 Answers0