【问题标题】:How to show dropdown of suggestions in autcompletetextview如何在自动完成文本视图中显示建议下拉列表
【发布时间】:2017-09-29 10:29:40
【问题描述】:

我想在文本字段为空时向用户显示所有建议,我从 json 响应中获取我的建议,当用户键入它出现在下拉列表中的单词但当文本字段为空时它不显示任何内容甚至尝试过showDropDown() 但Android Monitor 说:尝试完成输入事件但输入事件接收器已被释放。

这是我的代码:

public class MainActivity extends AppCompatActivity {
AutoCompleteTextView acTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    acTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
    acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString()));
    acTextView.setThreshold(0);

    acTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            acTextView.showDropDown();
        }
    });
        }
    });
  }
}

我首先从这些课程中获得我的建议适配器 JSON Parser 从 URL 中解析关键字。 getter setter 类。 Suggestions Adapter 在适配器中设置过滤的项目。 我的 JSON 响应:

 [{"lookupValueId":350,"lookupTypeId":33,"lookupValue":"Java"},{"lookupValueId":351,"lookupTypeId":33,"lookupValue":"C++"},{"lookupValueId":352,"lookupTypeId":33,"lookupValue":"Photoshop"},{"lookupValueId":353,"lookupTypeId":33,"lookupValue":"Java Script"}]

JSON 解析器类:

public class JsonParse {


public List<SuggestGetSet> getParseJsonWCF(String sName)
{
    List<SuggestGetSet> ListData = new ArrayList<SuggestGetSet>();
    try {
        String temp = sName.replace(" ", "%20");
        URL js = new URL("http://SomeUrl" + temp + "%22%7D%7D");
                    URLConnection jc = js.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
        String line = reader.readLine();
        JSONArray jsonResponse = new JSONArray(line);
        System.out.print("DATA: " + line);
        for(int i = 0; i < jsonResponse.length(); i++){
            JSONObject r = jsonResponse.getJSONObject(i);
            ListData.add(new SuggestGetSet(r.getInt("lookupValueId"),r.getString("lookupValue")));
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return ListData;

   }
}

getter setter 类:

public class SuggestGetSet {
String name;
static Integer id;
public SuggestGetSet(int id, String name){
    this.setId(id);
    this.setName(name);
}
public static Integer getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
  }
}

最后是适配器类:

class SuggestionAdapter extends ArrayAdapter<String> {

private List<String> suggestions;

SuggestionAdapter(Activity context,String nameFilter) {
    super(context, android.R.layout.simple_dropdown_item_1line);
    suggestions = new ArrayList<>();
}


@Override
public int getCount() {
    return suggestions.size();
}

@Override
public String getItem(int index) {
    return suggestions.get(index);
}

@NonNull
@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            JsonParse jp = new JsonParse();
            if (constraint != null) {
                // A class that queries a web API, parses the data and
                // returns an ArrayList<GoEuroGetSet>
                List<SuggestGetSet> new_suggestions = jp.getParseJsonWCF(constraint.toString());
                suggestions.clear();
                for (int i = 0; i < new_suggestions.size(); i++) {
                    suggestions.add(new_suggestions.get(i).getName());
                }

                // Now assign the values and count to the FilterResults
                // object
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
  }
}

我关注了这个参考:http://manishkpr.webheavens.com/android-autocompletetextview-example-json/

【问题讨论】:

  • 关注这个话题:Show suggestions when no text
  • 我浏览了该教程以及许多其他教程和 stackoverflow 答案。没有发现任何有用的东西,所以最后发布了一个问题。

标签: android autocomplete


【解决方案1】:

首先获取响应并将 arraylist 从活动传递到您的适配器。 你的代码

acTextView.setAdapter(new SuggestionAdapter(this,yourarraylist));

【讨论】:

  • 目前我正在这样做:acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString())); 这将传递我的文本并获得建议。你到底要我做什么?你能详细说明一下吗?
  • 不,不工作。我想要做的是当用户单击自动完成文本视图时,它应该显示所有建议的下拉列表。 \n 这就是我正在做的 acTextView.setOnClickListener(new View.OnClickListener() { (@) Override public void onClick(View view) { acTextView.getHandler().post(new Runnable() { @Override public void run( ) { acTextView.showDropDown(); } }); } });
  • 只需使用下面的代码 autoTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoTextView.showDropDown(); } });
  • 正如我在问题中已经提到的,我尝试了 showDropDown 但它对我不起作用。
  • 当我点击 autocompletetextview 时,它说:尝试完成输入事件,但输入事件接收器已被释放。
【解决方案2】:

首先,如果 android 为您提供相同的组件,为什么您需要自定义适配器

关注tutorialthis

建议如 geeta 所说,首先从 JSON 创建 ArrayList,而不是将其传递给 adpter,这是最好的方法

希望对你有帮助

【讨论】:

  • 我成功地从我的 json 响应中创建了数组。如何将该数组传递到我的主要活动中?
猜你喜欢
  • 2011-10-03
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多