Alertdialog选择的项目当我来自儿童活动时,Arraylist正在增长

时间:2015-09-03 07:18:06

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

我有2个活动A(用户输入)和B(基于用户输入显示)。在活动A中,我从警报对话框中选择多个值并将其放入arraylist然后将其传递给活动B.因此,当我想要更改值时,我需要回到活动A并且我可以重新选择。但是当我重新选择值或删除任何值时,旧的选定值也存在,以便我的Arraylist继续增长。请帮忙。

活动A:

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

活动B:

public class sharedcollect扩展了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);
    } }}

尝试在活动B中的onOptionsItemSelected(MenuItem项)中使用OnBackPressed(),但这并没有帮助。如何控制数组列表,以便我只发送所选项目。

1 个答案:

答案 0 :(得分:0)

在活动A中每次重新选择之前,您需要清除数组。

ilist.clear();

在您的情况下,我的猜测是在您的活动A中添加以下内容。

编辑:

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