我在我的微调器中添加类别,我的最后一个类别是Add New

时间:2017-01-11 07:14:50

标签: android android-spinner

我想要的是,如果用户选择最后一个类别,弹出窗口应该显示有编辑文本,用户必须添加他的新类别,然后它应该反映到微调器。

我正在给我的代码。请告诉我代码有什么问题或我遵循错误的方式。

spinner_category_item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/spinnercatName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

HomeFragment.java

private static final String CATEGORYNAME = "catname";
private static final String ADD_NEW_ITEM = "Add New Item";

private SimpleAdapter categoryAdapter;
private List<HashMap<String, String>> mapCatNames;
private int counter=1;
private AdapterView.OnItemSelectedListener itemSelectedListener = new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                               long arg3) {
       final HashMap<String, String> map = mapCatNames.get(arg2);

        String selectedCategory = map.get(CATEGORYNAME);

        if (selectedCategory.equalsIgnoreCase(ADD_NEW_ITEM)) {
            LayoutInflater layoutInflater=LayoutInflater.from(getActivity().getApplicationContext());
            View prompt=layoutInflater.inflate(R.layout.prompt_dialog,null);
            final AlertDialog.Builder alertdialogBuilder= new AlertDialog.Builder(getActivity().getApplicationContext());
            alertdialogBuilder.setView(prompt);
            final TextView tv= (TextView) root.findViewById(R.id.textView12);
            final EditText addUserCategory= (EditText) root.findViewById(R.id.etDialogAddCat);
            alertdialogBuilder.setCancelable(false)
                    .setPositiveButton("ADD", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String newCat =addUserCategory.getText().toString();


                            mapCatNames.remove(map);

                            counter++;
                            addNewName(String.valueOf(counter));
                            addNewName(newCat);
                            addNewName(ADD_NEW_ITEM);
                            categoryAdapter.notifyDataSetChanged();

                        }
                    })
                    .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
            AlertDialog alertDialog= alertdialogBuilder.create();
            alertDialog.show();



        }
    }

private void populateList() {
    mapCatNames = new ArrayList<HashMap<String, String>>();

    addNewName("Food");
    addNewName("Clothes");
    addNewName("Eating Out");
    addNewName("Entertainment");
    addNewName("Gifts");
    addNewName("Genral");
    addNewName("Holidays");
    addNewName("Kids");
    addNewName("Shopping");
    addNewName("Sports");
    addNewName("Travel");
    addNewName(ADD_NEW_ITEM);
}

private void addNewName(String name) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(CATEGORYNAME, name);
    mapCatNames.add(map);
}

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

    root=inflater.inflate(R.layout.fragment_home1,container,false);

    spinner= (Spinner) root.findViewById(R.id.spinner1);
    populateList();
    categoryAdapter=new SimpleAdapter(getActivity(),mapCatNames,R.layout.spinner_category_item,new String[] { CATEGORYNAME },new int[]{R.id.spinnercatName});
    spinner.setAdapter(categoryAdapter);
    spinner.setOnItemSelectedListener(itemSelectedListener);

promp_dialog.xml

TextView
    android:id="@+id/textView12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add your Category : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/etDialogAddCat"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />

1 个答案:

答案 0 :(得分:0)

添加新选项后,

在微调器的适配器上调用notifyDataSetChanged()。即 adapter.addItem(项目); adapter.notifyDataSetChanged();