【发布时间】:2014-07-23 23:19:36
【问题描述】:
我是 android 新手,我正在制作基于列表视图的应用程序。在我的应用程序中,我有从 Acitity_2 到 Activity_1 的项目。 Activity_1 具有列表视图。当用户单击activity1的添加按钮时,将调用Activity_2。 Activity_2 有 EditText。 EditText 的值应该更新 ListView。但它只需要 Edittext 的最后一个值。
这里是Activity_2的代码:
Intent intent = new Intent(ItemDetails.this, MainActivity.class);
intent.putExtra("values", values);
startActivity(intent);
Activity_1 的代码
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
protected Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Setting a custom layout for the list activity */
setContentView(R.layout.activity_main);
/** Reference to the add button of the layout main.xml */
Button btn = (Button) findViewById(R.id.btnAdd);
/** Reference to the delete button of the layout main.xml */
Button btnDel = (Button) findViewById(R.id.btnDel);
/** Defining the ArrayAdapter to set items to ListView */
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
list.clear();
list.add("Aluminium foil");
/** Setting the adapter to the ListView */
setListAdapter(adapter);
String val="";
if(getIntent().getStringExtra("values")!=null && getIntent().getStringExtra("values").length() > 0){
val = getIntent().getStringExtra("values");
list.add(val);
}
adapter.notifyDataSetChanged();
现在它覆盖了 edittext 的最后一个值。 欢迎提出建议。
【问题讨论】:
标签: android android-layout android-intent android-activity android-listview