当来自子活动时,在OnCreate方法中获取alertdialog选择的列表值

时间:2015-09-03 10:54:05

标签: android android-intent android-activity arraylist android-alertdialog

在我的应用程序中,需要在警告对话框中的第一个活动中选择值,并将该值传递给第二个活动。这是工作 。现在,当我回到第一个活动而没有选择任何如果我点击过滤器按钮它发送空列表。如何将警报对话框中显示的值带到onCreate。

第一项活动:

 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.clear();
            ilist();
            break;

    }

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

}  public ArrayList ilist() { final String[] ratings = new String[]{"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++)
    {
        imdblist.add(sharedPreferences.getString("selectedratings" + j, null));
       //Log.e("Kumar", "" + selectedratings);
    }

    for(int j=0;j<=ratingschecked.length;j++){
        if(imdblist.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(!imdblist.contains(ratings[which])){
                       imdblist.add(ratings[which]);

                    }
                //Log.e("Shiva", ""+imdblist);
            } else if (imdblist.remove(ratings[which])) {

               // Log.e("Kumar", String.valueOf(ratings[which]));


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

            // editor.putString("checked", String.valueOf(selectedratings));
            for (int i = 0; i < imdblist.size(); i++) {
                editor.putString("selectedratings" + i, String.valueOf(imdblist.get(i)));
            }
            editor.putInt("size", imdblist.size());
            editor.apply();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

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

第二项活动:

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 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:
          onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    } }}

1 个答案:

答案 0 :(得分:0)

要向第二个Activity发送final ArrayList ilist属性,这是您的类变量。但是你没有在这个清单上写任何东西。

case R.id.imdb_rating:
     ilist.clear();
        ilist();

在这里xou清除你的ilist(属性),然后你调用你的ilist(函数)函数。 ilist()返回ArrayList的类型,你忽略它。

对于您的测试用例,应该从提醒diloag写入类属性final ArrayList ilist

...
    builder.setMultiChoiceItems(ratings, ratingschecked, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            if (isChecked) {
                   if(!ilist.contains(ratings[which])){
                       ilist.add(ratings[which]);
                    }
                //Log.e("Shiva", ""+imdblist);
            } else if (imdblist.remove(ratings[which])) {
               // Log.e("Kumar", String.valueOf(ratings[which]));
                if(ilist.contains(ratings[which])){
                       ilist.remove(ratings[which]);
                    }
            }
        }
    }) 
...

然后点击OK按钮后,不需要将此项目写入共享首选项。开始新活动就足够了

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

...