从autocompletetextview动态添加项目到列表视图

时间:2017-11-21 21:28:21

标签: android android-studio

public void AutocmpleteMeth() {
    // Hieronder is het code voor Autocomplete [BEGIN]
    final AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_dropdown_item_1line, lstProduct);
    ACTV.setAdapter(adapter);
    ACTV.setThreshold(1);

    ACTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            String selectedItem = (String) arg0.getItemAtPosition(position);
            Boodschappenlst.add(selectedItem);
            ACTV.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                        Log.e("TAG", "Done pressed");
                        ACTV.setText("");

                    }
                    return false;
                }
            });
        }
        //Autocmplete [END]
    });

}

这是我的自动完成方法,通过按下完成键完美地将项目添加到列表视图,按下键后键盘消失,自动完成文本设置为''。我想让它变得更有活力。就像现在发生的那样,当用户点击建议清单中的项目时。它来到Autocompletetextview字段,然后用户必须在键盘上按完成并显示到列表视图。但我想要的是它直接添加到listview。在用户点击建议列表中的任何项目时,它不会转到文本字段,用户也不必按完成。它只是将该项添加到listview上的click和textfield get reset。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

将项目添加到notifyDataSetChanged()后,您需要在相关的ListView适配器上致电Boodschappenlst。这种方法可以满足你的需要:

public void AutocmpleteMeth() {
 // Hieronder is het code voor Autocomplete [BEGIN]
 final AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_dropdown_item_1line, lstProduct);
 ACTV.setAdapter(adapter);
 ACTV.setThreshold(1);

 ACTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView < ? > arg0, View arg1, int position, long arg3) {

    String selectedItem = (String) arg0.getItemAtPosition(position);
    Boodschappenlst.add(selectedItem);
    // Call notifyDatasetChanged() here on the related ListView's adapter here to recognise new item change
    ACTV.setText("");
   }
   //Autocmplete [END]
 });
}