我的MainActivity
有一个ListView
,当我点击ListView
时,它会为该项打开一个新活动。
我希望能够更改该项目中的信息,然后当我点击它时会更改ListView
。
以下是我的一些代码:
MainActivity:
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);
//
}
});
我在课堂上有这个,我认为可以从其他活动中获取信息,但没有任何反应:
@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();
}
}
}
在其他活动类中:
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();
}
};
答案 0 :(得分:2)
根据我上面的评论,如果您按后退键,那么您没有正确完成活动。你想要做的是当你准备结束Activity时,无论是在Button还是其他动作中,然后执行以下操作(看起来你已经大部分已经弄明白了)
...
// 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()
的人,这将允许您在活动中按下后退键并截断后退键,并决定如何处理它。但是,请注意这样做的含义:如果这是针对一般公众的,大多数用户会希望后退键能够带你某种形式的“返回”,但这与完成或通过正常流程的正常流程不同。你的应用程序(你想做的选择,然后继续你离开的地方继续)。因此,虽然这可能会实现所需的行为,但这对您来说是否是正确的解决方案仍存在争议。
答案 1 :(得分:0)
如果您有两个以上的活动,我会使用Singleton。如果只有两个则可能使用intent.put。谢谢,