0

我试图用来自 AlertDialog 的输入填充 ListView,该输入在其中打开一个 EditText。我的问题是每次我用 EditText 在 AlertDialog 中给出一些东西并单击 AlertDialog 中的添加按钮时,它会用相同的 String 填充我的 Array 的所有 20 组。但我希望他只从 EditText 填充 String 1时间,我可以进一步处理这 1 个项目。

这是我的代码

MainActivity.java

public class MainActivity extends Activity {

    private static final int DIALOG_ALERT = 10;

    ListView list;
    Button addBtn;
    EditText input;

    String[] items;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView) findViewById(R.id.list);
        addBtn = (Button) findViewById(R.id.addPlanBtn);

        input = new EditText(this);

        items = new String[20];

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
        list.setAdapter(adapter);



        addBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
                alert.setTitle("Train plan name");
                alert.setMessage("enter a name:");
                alert.setView(input);




                alert.setButton("add", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                            String value = input.getText().toString();

                            for(int i=0; i < items.length; i++){     
                                items[i] = value;

                            }

                     }
                  });

                    alert.show();
            }


        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

activity_main.xml

<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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/addPlanBtn"
         >

    </ListView>

    <Button
        android:id="@+id/addPlanBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="add a new Plan" />

</RelativeLayout>

感谢您的任何帮助。

4

2 回答 2

1

点击监听器中的这个 for 循环正在为列表中的所有数组成员设置字符串:

for(int i=0; i < items.length; i++){     
    items[i] = value;
}

要仅更改有问题的字符串,您需要索引数组的特定成员,例如:

items[0] = value;

这只会改变第一项。在您的情况下,您拉出的对话框与特定的列表项无关,因此您必须想出一种方法来跟踪正在编辑的列表项。

于 2012-12-20T20:50:26.330 回答
0

知道了,

这是工作代码。

MainActivity.java

public class MainActivity extends Activity {

    private static final int DIALOG_ALERT = 10;

    ListView list;
    Button addBtn;
    EditText input;

    String[] items;
    MyListAdapter adapter;
    AlertDialog alert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView) findViewById(R.id.list);
        addBtn = (Button) findViewById(R.id.addPlanBtn);

        input = new EditText(this);

        items = new String[1];





        alert = new AlertDialog.Builder(MainActivity.this).create();
        alert.setTitle("Train plan name");
        alert.setMessage("enter a name:");
        alert.setView(input);


        alert.setButton("add", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                    String value = input.getText().toString();

                    items[items.length - 1] = value;

                    adapter = new MyListAdapter(MainActivity.this, items);
                    list.setAdapter(adapter);

                    String[] temp = new String[items.length +1];

                    for(int i = 0; i< items.length; i++)
                        temp[i] = items[i];

                    items = temp;

                    alert.dismiss();
                    adapter.notifyDataSetChanged();
             }

          });




        addBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                    alert.show();
            }


        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

我的列表适配器

public class MyListAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final String[] values;

    static class ViewHolder {
        public TextView text;
    }

    public MyListAdapter(Context context, String[] values) {
        super(context, R.layout.row_item, values);
        this.context = context;
        this.values = values;
    }

    @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row_item, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.tvItem);
        textView.setText(values[position]);

        return rowView;
      }


}

row_item.xml

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

    <TextView
        android:id="@+id/tvItem"
        android:layout_width="fill_parent"
        android:layout_height="50px"
        android:textSize="40px" >

    </TextView>


</RelativeLayout>

activity_main.xml

<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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/addPlanBtn"
         >

    </ListView>

    <Button
        android:id="@+id/addPlanBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="add a new Plan" />

</RelativeLayout>

谢谢大家,谁有时间帮助我。

于 2012-12-21T12:39:51.013 回答