我有两个活动,我想知道如何在第一个活动中点击一个按钮到ListView后,从第二个活动中添加一个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);
}
}
}
我从中得到String的第二个Activity
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();
}
}
和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>
第二项活动
<?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>
答案 0 :(得分:0)
始终先为适配器设置新数据,然后刷新
listView1.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
notifyDataSetChanged将告诉视图数据已被更改并且应该刷新其视图