gpt4 book ai didi

java - 在 Activity 之间来回传递数据

转载 作者:行者123 更新时间:2023-11-30 09:12:20 24 4
gpt4 key购买 nike

我有一个 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 时,无论是在 Button 还是其他一些 Action 中,然后执行以下操作(看起来你已经基本弄清楚了)

...
// 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com