从另一个活动填充主要活动的列表视图

时间:2015-12-16 12:35:55

标签: android listview

我设法通过意图将字符串传递给另一个主活动,并将其显示为textView。在main上填充listview是否有类似的逻辑?

private Button startQr;
private ListView validList;
private ArrayList<String> strArray;
private ArrayAdapter<String> adapter;
// private TextView textView;


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

    validList = (ListView)findViewById(R.id.listView);
    startQr = (Button)findViewById(R.id.scanButton);
    strArray = new ArrayList<>();
    adapter = new ArrayAdapter<String>(getApplicationContext(),android.
    R.layout.simple_expandable_list_item_1, strArray);
    validList.setAdapter(adapter);


    startQr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(First.this, Second.class);
            startActivity(i);
        }
    });

    Intent intent = getIntent();
    String message = intent.getStringExtra(SecondActivity.EXTRA_MESSAGE);
    //textView = (TextView)findViewById(R.id.textView1);
    //textView.setText(message);

第二节课:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);

    btn = (Button)findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getCode();
            finish();
        }
    });

}

public void getCode() {
    Intent intent = new Intent( Secondclass.this, First.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String array = editText.getText().toString();
    intent.putExtra(EXTRA_ARRAY, array);
    startActivity(intent);
}

它适用于单个editText。

2 个答案:

答案 0 :(得分:1)

如果您想从main填充ListView,则应发送Intent
但这次你需要额外发送ArrayList<String> 因此,您需要实现以下代码来发送Intent:

static final String EXTRA_ARRAY = "com.package.app.STRARRAY";
private ArrayList<String> strArray = new ArrayList<>();

Intent intent = new Intent(getActivity(), Second.class);
intent.putExtra(EXTRA_ARRAY, strArray);
startActivity(intent);

为了获得实现以下代码所需的意图:

static final String EXTRA_ARRAY = "com.package.app.STRARRAY";
Intent intent = getIntent();
ArrayList<String> strArray = intent.getStringArrayListExtra(EXTRA_ARRAY);

答案 1 :(得分:1)

而不是从您的第一个startActivity(i);电话Activity拨打startActivityForResult(i,REQUEST_CODE);。这将有助于您倾听Second Activity必须说/返回的任何内容。

结果将以Intent的形式收到。您需要@Override中的onActivityResult() First Activity

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to 
if (requestCode == REQUEST_CODE) {
    // Make sure the request was successful 
    if (resultCode == RESULT_OK) {
        // The user picked a contact. 
        // The Intent's data Uri identifies which contact was selected. 

        // Do something with the contact here (bigger example below) 
    } 
} 
} 

查看Android Documentation以了解有关如何获得结果的更多信息。

现在设置结果,在第二个活动中,您需要调用setResult()

Bundle data = new Bundle();
data.putString("data", "here goes you data");
Intent intent = new Intent();
intent.putExtras(data);
setResult(RESULT_OK, intent);
finish();

finish()将数据发送到setResult()之后调用Activity

希望它有所帮助!