我在将对话框中输入的值添加到列表视图中时遇到了一些麻烦。我所拥有的是一个带有两个listview和两个按钮的主屏幕,每个列表视图都有一个按钮,按下该按钮将打开一个对话框。该对话框将询问用户信息,关闭时,此信息将添加到列表视图中。
我无法将项目添加到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_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>
答案 0 :(得分:3)
在这里,您点击确定按钮关闭对话框,然后视图将不再显示。因此,首先从视图中获取数据并关闭对话框
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
arrayList.add(ingredient.getText().toString());
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});