从方法获取返回值并传递给另一个活动

时间:2015-09-01 17:26:03

标签: java android android-intent arraylist

我有一个从OnClick回调方法调用的方法。

public enum Foo {

    BAR_1("Bar 1"),
    BAR_2("Bar 2");

    private String friendlyValue;

    Foo(String friendlyValue){
        this.friendlyValue = friendlyValue;
    }

    public String getFriendlyValue() {
        return friendlyValue;
    }

    public static Foo fromFriendlyValue1(String friendlyValue){
        return Stream.of(Foo.values()).filter(r -> r.getFriendlyValue().equals(friendlyValue)).findFirst().get();
    }

    public static Foo fromFriendlyValue2(String friendlyValue) {
        return Arrays.stream(Foo.values()).filter(r -> r.getFriendlyValue().equals(friendlyValue)).findFirst().get();
    }
}

public void onClick(View v) { switch (v.getId()){ case R.id.irating: ilist(); break; 方法看起来像

ilist()

现在,如何在按钮的另一个OnClick方法中获取选择的arraylist,我需要将选择列表传递给另一个活动。

家长活动:

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; 
}

儿童活动:

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;
}

0 个答案:

没有答案