【问题标题】:How to get autocompletetextview id?如何获取自动完成文本视图 ID?
【发布时间】:2015-12-07 18:58:04
【问题描述】:

我按照this 示例在我的项目中使用 autocmpletetextview,我想在用户选择任何项目时获取 id,谁能告诉如何获取 id..

下面是json响应..所以如果点击ab然后我想得到1,如果我点击abc我想得到2..

主活动

public class MainActivity extends Activity {
private AutoCompleteTextView acTextView;
private String idtest;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    acTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);

    final SuggestionAdapter adapter=new SuggestionAdapter(this, acTextView.getText().toString());
    acTextView.setAdapter(adapter);


    acTextView.setOnItemClickListener(new OnItemClickListener() {



        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            JsonParse jps=new JsonParse();

            /* List<SuggestGetSet> list =jps.getParseJsonWCF(acTextView.getText().toString());

                for(int i = 0;i<list.size();i++)
                {
                  if(list.get(i).getName().equals(acTextView.getText().toString()))

                  idtest=list.get(position).getId();



                }
                   */




            SuggestGetSet  selectedSuggestGetSet = 
                     adapter.getAllUpdatedSuggestion().get(position);

            Toast.makeText(getApplicationContext(), selectedSuggestGetSet+acTextView.getText().toString(), Toast.LENGTH_SHORT).show();

        }
    });
}

适配器

public class SuggestionAdapter extends ArrayAdapter<String> {

protected static final String TAG = "SuggestionAdapter";
public List<String> suggestions;
private List<SuggestGetSet> new_suggestions;

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

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

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

@Override
public Filter getFilter() {
    Filter myFilter = 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>
                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 contraint,
                FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
    return myFilter;
}

public List<SuggestGetSet>  getAllUpdatedSuggestion(){
      return this.new_suggestions;
    }

} 回应

{"results":[{"id":"1","name":"ab"},{"id":"2","name":"abc"},{"id":"3","name":"bc"},{"id":"4","name":"bcd"},{"id":"5","name":"cd"},{"id":"6","name":"cde"},{"id":"7","name":"ef"},{"id":"8","name":"efg"},{"id":"9","name":"hi"},{"id":"10","name":"hig"},{"id":"11","name":"jk"},{"id":"12","name":"jkl"},{"id":"13","name":"mn"},{"id":"14","name":"mno"},{"id":"15","name":"pq"},{"id":"16","name":"pqr"},{"id":"17","name":"st"},{"id":"18","name":"stu"},{"id":"19","name":"vw"},{"id":"20","name":"vwx"},{"id":"21","name":"yz"},{"id":"22","name":"yza"}]}

【问题讨论】:

  • 你要哪个id?
  • 我在回复中的 id..
  • the id which i have in my response: 在哪个响应中?
  • 在该示例中,他们从服务器获取名称和 ID,请参见此处webheavens.com/suggestion.php?name=
  • 仔细看方法签名:onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id)id吗?

标签: android json autocompletetextview


【解决方案1】:

检查一下

List<SuggestGetSet> new_suggestions = jp.getParseJsonWCF(constraint.toString());

并在您的适配器中使用private List&lt;SuggestGetSet&gt; new_suggestions; 制作一个

【讨论】:

    【解决方案2】:

    在适配器中放入一个方法

    public Int getItemId(int index) {
        return suggestions.get(index).getId();
    }
    然后在 onItemClick 中访问它。你会从那里得到id 适配器.getItemId(位置);

    【讨论】:

      【解决方案3】:

      如何获取 autocompletetextview 项的 id?

      new_suggestions 包含所有想要在 ListView 项目上单击的项目。所以在 getFilter 方法之外声明它以便从其他类访问:

      private List<String> suggestions;
      private List<SuggestGetSet> new_suggestions ;
      ....
      

      new_suggestionsgetFilter 方法中初始化它:

      ...
      new_suggestions =jp.getParseJsonWCF(constraint.toString());
      suggestions.clear();
      ...
      

      现在在SuggestionAdapter 中创建一个方法:

      public List<SuggestGetSet>  getAllUpdatedSuggestion(){
        return this.new_suggestions;
      }
      

      最后在onItemClick内部调用getAllUpdatedSuggestion方法:

      final SuggestionAdapter adapter=new SuggestionAdapter(this,
                                      acTextView.getText().toString())
      acTextView.setAdapter(adapter);
      

      onItemClick 方法中:

         @Override
          public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
              SuggestGetSet  selectedSuggestGetSet = 
                           adapter.getAllUpdatedSuggestion().get(position);
          }
      

      selectedSuggestGetSet 将包含所选项目 nameid

      【讨论】:

      • @Lakhan 请告诉我为什么要使用如此复杂的代码?一次又一次地崩溃?我给了你WORKING 30行代码,那为什么呢?有什么难理解的?
      • @pskink 您的代码运行良好..但我的问题是在单击项目时获取 id..但我也不能从您的代码中获取它..所以我再次处理这个复杂的代码跨度>
      • @Lakhan 我放弃了,我已经说了三遍:使用onItemClick的最后一个参数:long id
      • @ρяσѕρєяK 是的,我试过了,但是在我的吐司中它没有显示 id 和 name
      • @ρяσѕρєяK 查看问题
      【解决方案4】:

      将您的建议列表更改为公开:

      public List<String> suggestions;
      

      然后在您的 itemClick 方法中获取所需的 id:

       @Override
              public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {
      
                  your id = YourAdapter.suggestions[position].id;
      
              }
      

      【讨论】:

      • 如何设置这种方式..your id = YourAdapter.suggestions[position].id;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      相关资源
      最近更新 更多