Reputation: 283
I am having some trouble adding values entered in a dialog box into a listview. What I have is a home screen with two listviews and two buttons, each list view has a button that when pressed will open a dialog box. The dialog box will ask the user for information and when closed this information will be added into the listview.
I am having trouble adding the items to the ListView and cant seem to see where I have gone wrong. I can open the dialog boxes, enter the data and close the box but the text isnt being added into the 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>
Upvotes: 2
Views: 2730
Reputation: 227
Here you are closing the dialog on clicking on ok button then the views will be no longer live. So get the data first from the view and dismiss the dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
arrayList.add(ingredient.getText().toString());
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
Upvotes: 3