我有一个 MainActivity
,它有一个 ListView
,当我单击 ListView
时,它会为该项目打开一个新 Activity 。
我希望能够更改该项目中的信息,然后当我单击返回时,它会更改 ListView
。
这是我的一些代码:
主要 Activity :
String[] people;
private ListView mListView;
public static ArrayAdapter<String> adapter;
在 onCreate() 中{
people = new String[] {"", "", "", "", "", "", "", ""};
mListView = (ListView) findViewById(R.id.personListView);
adapter = (new ArrayAdapter<String>(this, R.layout.list_item, people);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Current item
String person = ((TextView) view).getText().toString();
// Launch new activity based on item
Intent intent = new Intent(getApplicationContext(), SinglePerson.class);
//
intent.putExtra("person", person);
//intent.putExtra("peopleList", people);
intent.putExtra("position", position);
startActivityForResult(intent, 1);
//
}
});
我在类里面有这个,我认为可以从其他 Activity 中获取信息,但没有任何反应:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1) {
// Make sure the request was successful
if(resultCode == RESULT_OK){
int listPos = data.getIntExtra("listPosition", 1);
//edit listview value at position
people[listPos] = data.getStringExtra("edittextvalue");
adapter.notifyDataSetChanged();
}
}
}
在其他 Activity 类中:
public class SinglePerson extends Activity{
String[] people;
int position;
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_person_item_view);
EditText txtPerson = (EditText) findViewById(R.id.person_name);
intent = getIntent();
String person = intent.getStringExtra("person");
//people = intent.getStringArrayExtra("peopleList");
position = intent.getIntExtra("position", 0);
txtPerson.setText(person);
}
private TextWatcher peopleNumberListener = new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//people[position] = s.toString();
//BillSplit.adapter.notifyDataSetChanged();
intent.putExtra("edittextvalue",s.toString());
intent.putExtra("listPosition", position);
setResult(RESULT_OK, intent);
//finish();
}
};
最佳答案
根据我上面的评论,如果您按后退键,那么您就没有正确完成 Activity 。您想要做的是,当您准备好结束 Activity 时,无论是在按钮中还是在其他操作中,然后执行以下操作(看起来您已经基本上弄清楚了)
...
// you can create a new Intent for the result
Intent newIntent = new Intent();
newintent.putExtra("edittextvalue",s.toString());
newintent.putExtra("listPosition", position);
setResult(RESULT_OK, newintent);
finish();
...
编辑:为了回应那些发布重写onBackPressed()
的人,这将允许您在 Activity 中按下后退键时拦截它,并决定如何处理它。但是,请注意这样做的含义:如果这是针对公众的,大多数用户会期望后退键带您某种形式的“后退”,但这与完成或通过正常流程进行进展不同。您的应用程序(您希望通过进行选择然后从上次中断的地方继续执行)。因此,虽然这可能会实现所需的行为,但这是否是适合您的正确解决方案是有争议的。
关于java - 在 Activity 之间来回传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618628/