将项目从对话框添加到 ListView

Posted

技术标签:

【中文标题】将项目从对话框添加到 ListView【英文标题】:Adding Items from Dialog Box to ListView 【发布时间】:2016-03-05 11:02:51 【问题描述】:

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

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

MainActivity

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_
    android:layout_ 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_
        android:layout_ />

    <ListView
        android:id="@+id/ingredientsList"
        android:layout_
        android:layout_
        android:layout_below="@+id/recipe"
        android:layout_above="@+id/showIngredientDialog">
    </ListView>

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

    <ListView
        android:id="@+id/directionList"
        android:layout_
        android:layout_
        android:layout_above="@+id/showDirectionDialog"
        android:layout_below="@+id/showIngredientDialog"
        android:layout_alignTop="@+id/showIngredientDialog">
    </ListView>

    <Button
        android:id="@+id/showDirectionDialog"
        android:layout_
        android:layout_
        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_
    android:layout_ >

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

    <Spinner
        android:id="@+id/measurement"
        android:layout_
        android:layout_
        android:layout_below="@+id/name"
        android:layout_alignParentStart="true"
        android:entries="@array/measurements"/>

    <Spinner
        android:id="@+id/unit"
        android:layout_
        android:layout_
        android:layout_below="@+id/measurement"
        android:layout_alignParentStart="true"
        android:entries="@array/units"/>

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

</RelativeLayout>

【问题讨论】:

【参考方案1】:

在这里,您将关闭单击确定按钮的对话框,然后视图将不再存在。所以首先从视图中获取数据并关闭对话框

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

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

【讨论】:

感谢您的帮助,但似乎没有解决问题,列表仍然显示为空白 我刚刚注意到我使用了错误的列表布局,使用了简单的微调器而不是 simple_list_item_1。再次感谢您的帮助,我不会看到那个错误

以上是关于将项目从对话框添加到 ListView的主要内容,如果未能解决你的问题,请参考以下文章

与 ListView 战斗

从片段类中的对话框添加项目到recyclerview。

将 RecyclerView(RecyclerFragment) 添加到对话框

android从listview中删除任何项目删除最后一个元素

Flutter 中刷新 ListView

从 AlertDialog 输入将项目添加到 RecyclerView