无法将项目添加到ListView

时间:2014-12-03 06:58:29

标签: android listview android-edittext

我有一个dialog,我需要添加姓名,如果我点击Ok按钮,edittext中输入的名称应该添加到我的ListViewfragment

这是我的家庭片段:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;

这是我的输入对话框:

<LinearLayout 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:padding="@dimen/activity_vertical_margin"
android:orientation="vertical" >

<TextView
    android:id="@+id/list_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter name for shopping list" />

<EditText
    android:id="@+id/list_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge" />

这是我的fragment_home:

<?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" >

 <ListView android:id="@+id/itemslistView"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:layout_marginTop="75dp"/>
</RelativeLayout>

这是我的对话框代码:

protected void showInputDialog() {

    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);
    alertDialogBuilder.setView(promptView);
    final EditText editText = (EditText) promptView
            .findViewById(R.id.list_text);
    // setup a dialog window
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    LayoutInflater layoutInflater = LayoutInflater
                            .from(MainActivity.this);
                    View lstview = layoutInflater.inflate(
                            R.layout.fragment_home, null);
                    lv = (ListView) lstview
                            .findViewById(R.id.itemslistView);
                    List<String> MyList = new ArrayList<String>();
                    String NewListname=editText.getText().toString();
                    MyList.add(NewListname);
                     Toast.makeText(MainActivity.this, NewListname, Toast.LENGTH_LONG).show();
                    ArrayAdapter<String> adp = new ArrayAdapter<String>(
                            MainActivity.this, R.layout.list, MyList);
                    lv.setAdapter(adp);
                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();

}

这是我的HomeFragment:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class HomeFragment extends Fragment {
    public HomeFragment() {
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container,
                false);


        return rootView;

    }
}

我的问题是我能够从吐司所示的edittext中获取文本,但无法将其添加到listview。

谁能说我哪里出错?

3 个答案:

答案 0 :(得分:1)

在您刚刚调用MyList

adapter.notifyDataSetChanged();中添加文字后

无需再次setAdapter(adp);

答案 1 :(得分:0)

请尝试使用以下代码(我从未在设备中尝试过)

protected void showInputDialog() {

    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);
    alertDialogBuilder.setView(promptView);
    final EditText editText = (EditText) promptView
            .findViewById(R.id.list_text);
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View lstview = layoutInflater.inflate(R.layout.fragment_home, null);
    lv = (ListView) lstview.findViewById(R.id.itemslistView);
    List<String> MyList = new ArrayList<String>();

    final ArrayAdapter<String> adp = new ArrayAdapter<String>(MainActivity.this,
            R.layout.list, MyList);
    lv.setAdapter(adp);
    // setup a dialog window
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    String NewListname = editText.getText().toString();
                    MyList.add(NewListname);
                    Toast.makeText(MainActivity.this, NewListname,
                            Toast.LENGTH_LONG).show();
                    adp.notifyDataSetChanged();

                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();

} 

答案 2 :(得分:0)

试试这个:

在你的HomeFragment中:

List<String> MyList = new ArrayList<String>();
ListView lv=(ListView)findViewById(R.id.itemslistView);

protected void showInputDialog() {

// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        MainActivity.this);
alertDialogBuilder.setView(promptView);
final EditText editText = (EditText) promptView
        .findViewById(R.id.list_text);
// setup a dialog window
alertDialogBuilder
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                String NewListname=editText.getText().toString();
                MyList.add(NewListname);
                 Toast.makeText(MainActivity.this, NewListname, Toast.LENGTH_LONG).show();
                ArrayAdapter<String> adp = new ArrayAdapter<String>(
                        MainActivity.this, R.layout.list, MyList);
                lv.setAdapter(adp);
            }
        })
        .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();

}