嗨,我是android和amp;的新手做一些简单的例子。确切地说,我正在使用两项活动&它包含两者的列表视图。在第一个列表视图(Activity1)为空时,第二个列表视图(Activity2)有一些带有复选框的列表项。现在从第二个活动中获取所选项目&列在第一个活动中。这很好,但我想要的是再次点击第一个活动的“添加”按钮,它启动第二个活动,然后选择一些项目&保存。此新选择的项目不会添加到第一个活动中使用的现有列表值。任何人都可以帮助我。
SecondActivity.java
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkedContactList = adapter.getSelectedItemArrayList();
}
Intent returnIntent = new Intent(v.getContext(),MainActivity.class);
returnIntent.putParcelableArrayListExtra("result", checkedContactList);
startActivity(returnIntent);
}
});
在MainActivity.java中
private ArrayList<SelectedListModel> selectedArrayList = new ArrayList<SelectedListModel>();
List<Map<String, String>> dataList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataList = new ArrayList<Map<String, String>>();
Bundle data = getIntent().getExtras();
selectedArrayList = data.getParcelableArrayList("result");
Iterator<ArrayList<SelectedListModel>> it = selectedArrayList.iterator();
while (it.hasNext()){SelectedListModel selectedModel = (SelectedListModel)it.next();
SelectedListModel selectedModel = (SelectedListModel)it1.next();
String name1 = selectedModel.getName();
String number1 = selectedModel.getNumber();
Map<String, String> dict = new HashMap<String, String>(2);
dict.put("name", name1);
dict.put("number", number1);
dataList.add(dict);
//listdata.add(dataList);
}
SimpleAdapter adpt = new SimpleAdapter(this, dataList, android.R.layout.two_line_list_item, new String[] {"name", "number"}, new int[] {android.R.id.text1, android.R.id.text2});
selectedList.setAdapter(adpt);
}
答案 0 :(得分:0)
你可以使用startActivityForResult() Api Document
致电活动1
startActivityForResult( Activity2, Activity2RequestCode)
在活动2中
int result = "Some message here";
output.putExtra(name , result);
setResult(RESULT_OK, output);
finish();
在活动1中获取返回值
onActivityResult (int requestCode, int resultCode, Intent data)
if (requestCode == ActivityTwoRequestCode && resultCode == RESULT_OK && data != null)
num1 = data.getIntExtra(Number1Code);
num2 = data.getIntExtra(Number2Code);