如何通过对话框中的用户输入将项目添加到Listview

我有一个片段内的列表视图。该片段包含一个按钮,该按钮将显示一个对话框,其中包含一个编辑文本,该文本将允许用户将项目添加到列表视图。我决定在框架类中实现对话框。所以我的问题是:如何将用户文本添加到列表视图中? 我猜想需要在对话框的setPositiveButton()方法内实现的东西。这是我的代码。谢谢

public class CustomFragment extends Fragment {
    private ArrayAdapter<String> adapter;
    private ListView listView;
    private FloatingactionButton addPhraseButton;
    private TextView phraseTitleTextView;
    private TextToSpeech textToSpeech;
    private String[] phrases;
    private static final String PHRASE_LABEL = " Phrases";
    private static final String CATEGORY_KEY = "categories";
    private String dialogUserInput;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_custom,container,false);

        final String categories;
        //if we selecte a category sent from home fragment else we got here through menu
        Bundle arguments = getarguments();
        if (arguments != null && arguments.containsKey(CATEGORY_KEY)) {
            categories = arguments.getString(CATEGORY_KEY).toLowerCase();
        }
        else {
            Resources res = view.getResources();
            categories = res.getStringArray(R.array.categories)[0].toLowerCase();
        }

        final Phrases allPhrases = Phrases.getPhrases();
        allPhrases.fetchAllPhrases(view.getcontext());
        phrases = allPhrases.getallPhrases().get(categories);

        phraseTitleTextView = view.findViewById(R.id.label_phrases_txtview);
        phraseTitleTextView.setText(categories.substring(0,1).toUpperCase() +
                categories.substring(1)+ PHRASE_LABEL);
        addPhraseButton = view.findViewById(R.id.add_phrases_btn);
        // setting local for text to speech
        textToSpeech = new TextToSpeech(getactivity().getapplicationContext(),new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                textToSpeech.setLanguage(Locale.US);
            }
        });

        //setting adapter and listview
        adapter = new ArrayAdapter<String>(getcontext(),R.layout.entry_item,R.id.phrase_textview,phrases);
        listView = (ListView) view.findViewById(R.id.phrases_list);
        listView.setadapter(adapter);
        listView.setItemsCanFocus(true);

        listView.setOnItemClicklistener(new AdapterView.OnItemClicklistener() {
            @Override
            public void onItemClick(AdapterView<?> paren,View view,int position,long id) {
                String text = phrases[position];
                Toast.makeText(getcontext(),text,Toast.LENGTH_LONG).show();
                textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
            }
        });

        addPhraseButton.setOnClicklistener(new View.OnClicklistener() {
            @Override
            public void onClick(View view) {
                addPhraseDialogBox();
                allPhrases.addPhrase(categories,dialogUserInput);
            }
        });
        return view;
    }

    public void addPhraseDialogBox() {
        // Creating alert Dialog with one Button
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getactivity(),R.style.dialogBox);
        alertDialog.setTitle("Add Phrase");
        final EditText input = new EditText(getcontext());
        alertDialog.setView(input);
        alertDialog.setIcon(R.drawable.ic_comment_black_24dp);

        alertDialog.setPositiveButton("OK",new DialogInterface.OnClicklistener() {
                    public void onClick(DialogInterface dialog,int which) {
                        Toast.makeText(getcontext(),input.getText().toString(),Toast.LENGTH_SHORT).show();
                        dialogUserInput = input.getText().toString();
                    }
                }).create();

        alertDialog.setNegativeButton("CANCEL",int which) {
                        dialog.cancel();
                    }
                }).create();
        alertDialog.show();
    }
}

这是Phrases类(单例类)中的addPhrases方法。此类从包含字符串数组的string.xml文件中提取短语。 allPhrases是一个Hashmap键值,其中key是短语的类别,而value是所有短语的字符串数组。

 public void addPhrase(String key,String phrase) {
        tempPhrases = Arrays.asList(allPhrases.get(key));
        tempPhrases.add(phrase);
        allPhrases.put(key,(String[]) tempPhrases.toArray());
    }
change124864 回答:如何通过对话框中的用户输入将项目添加到Listview

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2969355.html

大家都在问