简体   繁体   English

单击其他活动中的按钮时如何向ListView添加新项目

[英]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.

相关问题 单击listView项时创建新活动 - Creating new activity when clicking on listView Item 如何打开新活动点击列表视图中的项目? - How open new activity clicking an item in listview? ListView:如何 go 到单击 ListView 中的按钮的新活动? - ListView: How to go to a new activity on clicking a button in the ListView? 引导用户点击ListView中的项目进行新活动 - Leading user to new activity clicking on item in ListView 单击菜单项按钮时无法打开新的活动 - Opening a new Activity when clicking a menu item button is not working 通过在另一个活动中单击按钮将项目添加到ListView中的一个片段中 - Add items to ListView in a fragment in one Activity by clicking a button in another activity 如何在创建新活动时记住单个ListView项的颜色 - How to remember a color of single ListView item when new activity is created 当我们单击列表视图项时如何启动新活动 - How to start a new activity when we click on listview item 应用程序在单击 listView 项目而不是打开新活动时崩溃 - App crashes on clicking listView item rather than opening a new activity 如何在Android Studio中单击列表视图中的项目来打开新活动? - How can i open new activity clicking an item in listview in Android Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM