使用putStringArrayListExtra在活动之间传递ArrayList

时间:2015-03-04 12:31:26

标签: android android-intent

我正在尝试使用putStringArrayListExtra将ArrayList从ScaleSetActivity传递给MainActivity。但是,ArrayList没有正确返回,我对它所做的任何更改都被原始的defaultKeys String数组覆盖。

MainActivity.java:

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends ActionBarActivity {

    private final String[] defaultKeys = {"C","D","E","F Sharp","B Flat","A Flat"};
    private ArrayList<String> scales = new ArrayList<>();
    private int scaleIdx = 0;
    private final int SCALE_SET_REQUEST = 1;

    private TextView practiceScale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setDefaultScales();

        Button chooseScaleButton = (Button) findViewById(R.id.choose_scales_button);
        Button shuffleButton = (Button) findViewById(R.id.shuffle_button);
        practiceScale = (TextView) findViewById(R.id.practice_scale_text);

        chooseScaleButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if(scaleIdx >= scales.size()) {
                    scaleIdx = 0;
                }
                practiceScale.setText(scales.get(scaleIdx));
                scaleIdx++;
            }
        });

        shuffleButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Collections.shuffle(scales);
                scaleIdx = 0;
            }
        });

        Button setScalesButton = (Button) findViewById(R.id.set_scales_button);
        setScalesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), ScaleSetActivity.class);
                intent.putStringArrayListExtra("Scales", scales);
                startActivityForResult(intent, SCALE_SET_REQUEST);
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK) {
            if(requestCode == SCALE_SET_REQUEST) {
                scales = data.getStringArrayListExtra("Scales");
                scaleIdx = 0;
            }
        }
    }
private void setDefaultScales() {
    for(String defaultKey : defaultKeys) {
        scales.add(defaultKey + " Major");
    }
}

@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_main, 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.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
    }
}

ScaleSetActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class ScaleSetActivity extends Activity {

    private ArrayList<String> scales;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scale_set_layout);

        Intent intent = getIntent();
        scales = intent.getStringArrayListExtra("Scales");

        Button finishButton = (Button) findViewById(R.id.finish_button);
        finishButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent results = new Intent();
                results.putStringArrayListExtra("Scales", scales);
                finish();
            }
        });

        final ArrayAdapter<String> scaleAdapter = new
                ArrayAdapter<>(this, android.R.layout.simple_list_item_1, scales);

        ListView scaleList = (ListView) findViewById(R.id.scale_list_view);
        scaleList.setAdapter(scaleAdapter);

        final EditText enterScaleEdit = (EditText)findViewById(R.id.enter_scale_edit);

        enterScaleEdit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                boolean result = false;
                if(event.getAction() == KeyEvent.ACTION_DOWN) {
                    if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER ||
                            event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                        scales.add(0, enterScaleEdit.getText().toString());
                        scaleAdapter.notifyDataSetChanged();
                        enterScaleEdit.setText("");
                        result = true;
                    }
                }
                return result;
            }
        });

        scaleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                scales.remove(position);
                scaleAdapter.notifyDataSetChanged();
            }
        });
    }

}

1 个答案:

答案 0 :(得分:1)

在完成第二项活动之前添加

  

setResult(Activity.RESULT_OK,results);

然后只有你能从onActivityResult获取这个arraylist。

finishButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent results = new Intent();
                results.putStringArrayListExtra("Scales", scales);
                setResult(Activity.RESULT_OK, results);
                finish();
            }
        });