使用列表适配器更新列表片段

时间:2015-07-03 16:27:48

标签: android listadapter dialogfragment

目前遇到的问题是尝试使用对话框片段输入(或虚拟输入)更新列表片段,但所有内容都会编译,但看不到对列表的任何更改。 请让我知道你在想什么。感谢。



public class NewEventDialogFragment extends DialogFragment {
    private List<GlobalClass> mItems;
    EditText editText;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //editText = (EditText) findViewById(R.id.editText);
        builder.setMessage("What Would You Like to Name the Event?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        mItems = new ArrayList<GlobalClass>();
                        mItems.add(new GlobalClass("Whoiszzzzzzzzzzzzzzthis", "Adsdfdsdomg"));
                        mItems.add(new GlobalClass("Whodsfzzzzzzzzzzzzzsdfsisthis", "Addsdfdfomg"));
                        mItems.add(new GlobalClass("Whoisthzzzzzzzzzzzzzzsdfdsfis", "Addosdfsdfmg"));
                        // Create the adapter to convert the array to views
                        MainTabsPagerAdapter adapter = new MainTabsPagerAdapter(getActivity(), mItems);
                        // Attach the adapter to a ListView

                        adapter.addAll(mItems);

                        adapter.notifyDataSetChanged();
                        //setListAdapter(new MainTabsPagerAdapter(getActivity(), mItems));
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        View view = getActivity().getLayoutInflater().inflate(R.layout.neweventdialog_fragment, null);
        builder.setView(view);

        return builder.create();

    }

}
&#13;
&#13;
&#13;

&#13;
&#13;
 private List<GlobalClass> mItems;        // ListView items list

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // initialize the items list
    mItems = new ArrayList<GlobalClass>();
    Resources resources = getResources();

    mItems.add(new GlobalClass("Whoisthis", "Adsdfdsdomg"));
    mItems.add(new GlobalClass("Whodsfsdfsisthis", "Addsdfdfomg"));
    mItems.add(new GlobalClass("Whoisthsdfdsfis", "Addosdfsdfmg"));

    // initialize and set the list adapter
    setListAdapter(new MainTabsPagerAdapter(getActivity(), mItems));
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

  1. 您需要在布局ListView中使用R.layout.neweventdialog_fragment并将此窗口小部件绑定到代码中的视图,然后在其上设置setAdapter。

    View view = getActivity().getLayoutInflater().inflate(R.layout.neweventdialog_fragment, null);
    ListView listView = (ListView) view.findViewById(R.id.listView); // you bind to listView your widget
    listView.setAdapter(adapter); // you set adapter
    builder.setView(view);
    
    return builder.create();
    
  2. 如果要显示DialogFragment,请使用NewEventDialogFragment.newInstance().show(getSupportFragmentManager(),CURRENT_FRAGMENT_TAG);