2015-12-01 26 views
2

我在將對話框中輸入的值添加到列表視圖時遇到了一些麻煩。我擁有的是一個包含兩個列表視圖和兩個按鈕的主屏幕,每個列表視圖都有一個按鈕,當按下該按鈕時會打開一個對話框。該對話框將詢問用戶的信息,關閉時這些信息將被添加到列表視圖中。將對話框中的項目添加到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> 

回答

3

他如果您點擊確定按鈕關閉對話框,則視圖將不再存在。所以從視圖首先得到的數據,並關閉該對話框

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

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

如果你覺得它有用接受的答案 – Don

+0

感謝您的幫助,但認爲沒有似乎修復它,清單仍然顯示爲空白 – Hayes121

+0

我剛纔注意到我在列表中使用了錯誤的佈局,使用了簡單的微調器而不是simple_list_item_1。再次感謝您的幫助,我不會看到這個錯誤 – Hayes121

相關問題