android - 将对话框中的项目添加到 ListView

标签 android xml listview android-listview

我在将对话框中输入的值添加到 ListView 时遇到了一些问题。我有一个带有两个 ListView 和两个按钮的主屏幕,每个 ListView 都有一个按钮,按下该按钮将打开一个对话框。该对话框将询问用户信息,关闭时该信息将添加到 ListView 中。

我在将项目添加到 ListView 时遇到问题,而且似乎看不出哪里出了问题。我可以打开对话框,输入数据并关闭对话框,但文本没有被添加到 ListView 中。

主 Activity

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    final Context context = this;
    private Button ingredient;
    private Button direction;
    private ArrayAdapter<String> adapter;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView ingredientList = (ListView) findViewById(R.id.ingredientsList);
        final ArrayList<String> arrayList = new ArrayList<String>();

        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);

        ingredientList.setAdapter(adapter);

        ingredient = (Button) findViewById(R.id.showIngredientDialog);
        ingredient.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // custom ingredient_dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.ingredient_dialog);
                dialog.setTitle("Add Ingredient");

                // set the custom ingredient_dialog components
                final EditText ingredient = (EditText) dialog.findViewById(R.id.name);
                //ingredient.getText().toString();

                Spinner measurement = (Spinner) dialog.findViewById(R.id.measurement);
                String measurementValue = measurement.getSelectedItem().toString();

                Spinner unit = (Spinner) dialog.findViewById(R.id.unit);
                String unitValue = unit.getSelectedItem().toString();

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom ingredient_dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        arrayList.add(ingredient.getText().toString());
                        adapter.notifyDataSetChanged();
                    }
                });

                dialog.show();
            }
        });

        direction = (Button) findViewById(R.id.showDirectionDialog);
        direction.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // custom ingredient_dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.direction_dialog);
                dialog.setTitle("Add Step");

                // set the custom ingredient_dialog components
                TextView description = (TextView) dialog.findViewById(R.id.description);
                EditText ingredient = (EditText) dialog.findViewById(R.id.direction);

                Button dialogButton2 = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom ingredient_dialog
                dialogButton2.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        });


    }
}

Activity_main.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:id="@+id/recipe"
        android:text="New Recipe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/ingredientsList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/recipe"
        android:layout_above="@+id/showIngredientDialog">
    </ListView>

    <Button
        android:id="@+id/showIngredientDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Ingredient"
        android:layout_above="@+id/showDirectionDialog"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="145dp" />

    <ListView
        android:id="@+id/directionList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/showDirectionDialog"
        android:layout_below="@+id/showIngredientDialog"
        android:layout_alignTop="@+id/showIngredientDialog">
    </ListView>

    <Button
        android:id="@+id/showDirectionDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Direction"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

ingredient_dialog.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="e.g Onions"/>

    <Spinner
        android:id="@+id/measurement"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_alignParentStart="true"
        android:entries="@array/measurements"/>

    <Spinner
        android:id="@+id/unit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/measurement"
        android:layout_alignParentStart="true"
        android:entries="@array/units"/>

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginRight="5dp"
        android:layout_below="@+id/unit"
        android:layout_toEndOf="@+id/name"
        android:gravity="center"/>

</RelativeLayout>

最佳答案

在这里,您将通过单击确定按钮关闭对话框,然后 View 将不再有效。所以首先从 View 中获取数据并关闭对话框

dialogButton.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {

                        arrayList.add(ingredient.getText().toString());
                        adapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                });

关于android - 将对话框中的项目添加到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34021836/

相关文章:

css - 我想将每个食谱分成自己的盒子,我正在寻找其他想法

java - 如何从 ListView 中删除选定的项目?

android - SwipeListView by 47degree : swipe first item programmatically

android - Adobe Flex 获取用户电话号码

Android:帮助创建一个按钮,它产生与按下方向键上的向下键相同的结果? (第2部分)

带有关键帧的 Android 矢量可绘制动画

android - 将 imageview 对齐到 listview 的末尾,当 listview 加载数据时 android

android - 如何在两个视频 View 中并行播放一个视频文件?

java - 在 Android 上使用保存的 Dropbox 身份验证详细信息

c# - 在 System.Xml.XPath 中启用 XPath2 查询(XPathException : invalid token)