I managed to pass string to main activity from another by intent and showing it as textView. Is there a similar logic to populate the listview on main?
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.
If you want to populate the ListView
from main, you should send an Intent
But this time you need to send as extra the ArrayList<String>
.
So you need to implement the following code to send the 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);
from your first Activity
call startActivityForResult(i,REQUEST_CODE);
. This will help you listen to whatever the Second Activity
has to say/return as a result.
The result will be received in the form of an Intent
. You need to @Override
onActivityResult()
in your 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)
}
}
}
Check out Android Documentation to know more about how to get result.
Now to set the Result, In the Second Activity you need to call 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.
Hope it helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.