简体   繁体   English

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

[英]Populating listview on main activity from another activity

I managed to pass string to main activity from another by intent and showing it as textView. 我设法通过意图将字符串从另一个传递给主要活动,并将其显示为textView。 Is there a similar logic to populate the listview on main? 是否有类似的逻辑可填充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);

The second class: 第二类:

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

It worked for a single editText. 它适用于单个editText。

If you want to populate the ListView from main, you should send an Intent 如果要从main填充ListView ,则应发送一个Intent
But this time you need to send as extra the ArrayList<String> . 但是这一次,您需要额外发送ArrayList<String>
So you need to implement the following code to send the Intent: 因此,您需要实现以下代码来发送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);

And in order to receive the intent you need to implement the following code: 并且为了接收意图,您需要实现以下代码:

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

Instead of calling startActivity(i); 而不是调用startActivity(i); from your first Activity call startActivityForResult(i,REQUEST_CODE); 从您的第一个Activity调用startActivityForResult(i,REQUEST_CODE); . This will help you listen to whatever the Second Activity has to say/return as a result. 这将帮助您聆听Second Activity的结果。

The result will be received in the form of an Intent . 结果将以Intent的形式接收。 You need to @Override onActivityResult() in your First Activity . 您需要在First Activity @Override onActivityResult()

@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) 
    } 
} 
} 

Check out Android Documentation to know more about how to get result. 查看Android文档,以了解有关如何获得结果的更多信息。

Now to set the Result, In the Second Activity you need to call setResult() . 现在设置结果,在第二个活动中,您需要调用setResult()

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

calling finish() after setResult() send the data to the Activity from where it was called. setResult() finish()之后,调用finish()将数据从调用位置发送到Activity

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM