[英]How to add a new item to ListView when clicking a button in a different Activity
I have two activities and I would like to know how can I add a String which I'm getting from the second activity after clicking a button to a ListView in the first activity. 我有两个活动,我想知道如何在第二个活动中单击第一个活动中的ListView按钮之后添加从第二个活动中获取的String。 I have done it this way but for some reason it keeps crashing.
我已经这样做了,但是由于某种原因,它一直崩溃。
This is the first Activity. 这是第一个活动。 It has a button which goes to the second activity and asks for the String.
它具有一个转到第二个活动并要求输入String的按钮。
public class MenuActivity extends Activity implements View.OnClickListener {
ArrayList<String> moduleList = new ArrayList<String>();
String module;
ListView listView1 = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(
this,
R.layout.simple_list_item_1,
moduleList);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(
this,
R.layout.simple_list_item_1,
moduleList);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent i = new Intent(this, ModuleActivity.class);
startActivityForResult(i, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK && requestCode==1){
Bundle bundle = data.getExtras();
module = bundle.getString("module");
moduleList.add(module);
listAdapter.notifyDataSetChanged();
listView1.setAdapter(listAdapter);
}
}
}
And the second Activity from which I get the String 还有第二个我从中获得字符串的活动
public class ModuleActivity extends Activity {
boolean check = false; //checks if a module has been added by clicking the add button
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_modules);
Button addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
EditText moduleName = (EditText) findViewById(R.id.moduleName);
if (moduleName.getText().toString() != null) {
check = true;
Intent intent = new Intent();
intent.putExtra("module",moduleName.getText().toString());
setResult(RESULT_OK,intent);
}
}
});
}
public void onBackPressed(){
if (check == false) {
Intent intent = new Intent();
setResult(RESULT_CANCELED,intent);
}
super.onBackPressed();
}
}
And the XML files 和XML文件
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:background="@drawable/banu_logo">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New Module"
android:id="@+id/btn1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Module List:"
android:id="@+id/textView"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:layout_marginBottom="20dp"
android:layout_below="@+id/btn1" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp" />
</RelativeLayout>
</FrameLayout>
And for the second activity 对于第二项活动
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Module Name:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/moduleName"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/addButton"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignRight="@+id/moduleName" />
</RelativeLayout>
Always first set the new data for your adapter and then refresh 始终首先为适配器设置新数据,然后刷新
listView1.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
notifyDataSetChanged will tell the view that data has been changed and it should refresh its view notifyDataSetChanged将告知视图数据已更改,并且应刷新其视图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.