我是Android的新手,我搜索了很多,但无法找到合适的答案。有人可以帮帮我吗? 我有两个活动,活动1包含一个列表视图,活动2是一个编辑视图,我想要做的是点击列表视图上的项目后,它将转到活动2,操作数据后点击提交按钮(提交方法),它将返回列表视图,列表视图将显示更新的结果。 这是我的avtivity 1代码
public class I4ComponentActivity extends AppCompatActivity {
private String quantity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_component);
final ArrayList<ComponentDetail> details = new ArrayList<ComponentDetail>();
details.add(new ComponentDetail("Screen", "1"));
details.add(new ComponentDetail("Camera", "2"));
details.add(new ComponentDetail("Battery", "5"));
details.add(new ComponentDetail("Change Port", "10"));
details.add(new ComponentDetail("Speaker", "5"));
details.add(new ComponentDetail("Power Key", "14"));
details.add(new ComponentDetail("Screen Cable", "24"));
ListView listView = (ListView) findViewById(R.id.component_list);
DetailAdapter detailAdapter = new DetailAdapter(I4ComponentActivity.this, details);
listView.setAdapter(detailAdapter);
// Set a click listener to edit quantity when the list item is clicked on
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
ComponentDetail cd = details.get(position);
quantity = cd.getQuantity();
Intent i = new Intent(I4ComponentActivity.this, Edit.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("Item_quantity", quantity);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
}
});
}
}
这是活动2代码
public class Edit extends AppCompatActivity {
private int quantity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String Item_quantity = bundle.getString("Item_quantity");
TextView tv = (TextView) findViewById(R.id.quantity_edit);
tv.setText(Item_quantity);
quantity = Integer.parseInt(Item_quantity);
}
public void decrease(View v){
quantity --;
displayQuantity();
}
public void increase(View v){
quantity ++;
displayQuantity();
}
public void submit(View v){
Intent i = new Intent(Edit.this, I4ComponentActivity.class);
Bundle bundle = new Bundle();
bundle.putString("quantity_back", ""+quantity);
i.putExtras(bundle);
startActivity(i);
}
public void displayQuantity(){
TextView tv = (TextView) findViewById(R.id.quantity_edit);
tv.setText(""+quantity);
}
}
答案 0 :(得分:0)
使用startActivityForResult(Intent,1)
在Aty2 setResult(1,Intent)
并重写onActivityResult
你会得到后面的意图。
将项目添加到您的arraylist后。使用adapter.notifyDataSetChanged();
刷新listview
如果你不清楚。 Google中必须包含大量信息