我使用此代码将MySQL中的值添加到我的AutoCompleteTextView中。这是有效的,但我希望选择一个项目打开一个传递id_poblacion值的新活动。 我尝试了很多东西,但我只能获得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;
}
Index.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);
}
});
谢谢
答案 0 :(得分:0)
Id_poblacion ---&gt; suggestions.add(new_suggestions.get(ⅰ).getId());