android - 使用两个值从 AutoCompleteTextView 获取 ID

标签 android mysql json android-intent autocompletetextview

我正在使用此代码将值从 MySQL 添加到我的 AutoCompleteTextView。这可行,但我希望在选择一个项目时打开一个传递 id_poblacion 值的新 Activity 。 我已经尝试了很多东西,但我只能得到 poblacion 值,没有 id_poblacion。

Poblacion.java

public class JsonParse {
    double current_latitude,current_longitude;
    public JsonParse(){}
    public JsonParse(double current_latitude,double current_longitude) {
        this.current_latitude=current_latitude;
        this.current_longitude=current_longitude;
    }
    public List<SuggestGetSet> getParseJsonWCF(String sName) {
        List<SuggestGetSet> ListData = new ArrayList<SuggestGetSet>();
        try {
            String temp=sName.replace(" ", "%20");
            URL js = new URL("my url"+temp);
            URLConnection jc = js.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
            String line = reader.readLine();
            JSONObject jsonResponse = new JSONObject(line);
            JSONArray jsonArray = jsonResponse.getJSONArray("results");
            for(int i = 0; i < jsonArray.length(); i++) {
                JSONObject r = jsonArray.getJSONObject(i);
                ListData.add(new SuggestGetSet(r.getString("id_poblacion"),r.getString("poblacion")));
            }
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return ListData;
    }
}

public class SuggestGetSet {

    String id_poblacion, poblacion;
    public SuggestGetSet(String id_poblacion, String poblacion){
        this.setId(id_poblacion);
        this.setName(poblacion);
    }
    public String getId() {
        return id_poblacion;
    }
    public void setId(String id_poblacion) {
        this.id_poblacion = id_poblacion;
    }

    public String getName() {
        return poblacion;
    }
    public void setName(String poblacion) {
        this.poblacion = poblacion;
    }
}

protected static final String TAG = "SuggestionAdapter";
private List<String> suggestions;
public Poblaciones(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>
                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 contraint, FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
    return myFilter;
}

索引.java

    super.onCreate(savedInstanceState);
    setContentView(R.layout.index);
    AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
    acTextView.setAdapter(new Poblaciones(this,acTextView.getText().toString()));
    acTextView.setOnItemClickListener(new OnItemClickListener() { 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getApplicationContext(), NewActivity.class);
            intent.putExtra("id_poblacion", id_poblacion);
            startActivity(intent);
        }
    });

谢谢

最佳答案

Id_poblacion ---> suggestions.add(new_suggestions.get(i).getId());

关于android - 使用两个值从 AutoCompleteTextView 获取 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24053196/

相关文章:

android - 有没有一种方法(或插件)可以快速访问例如。来自 Eclipse 中 R.layout.main 的 layout/main.xml?

c# - 使用存储在变量中的值更新数据库

php - 为不同的用户创建相同的登录页面

Javascript 奇怪的 Fetch() 问题,在特定 JSON 数据/格式上失败

json - 格式 JSON Postgresql

Android:即使将 Runnable 发布到 View ,也会获取 CalledFromWrongThreadException

android - 坚持使用 Android 布局

android,xml按钮布局问题

mysql - mysql 库存数据库中的库存数量 View

java - Android 无法转换为 JSONObject 错误