I have an adapter that returns an hashmap with 2 strings in it. What I want people to see is the first string and when people click on it I want to have the second string but I'm kinda stuck on how to do it.
This is my adapter:
public class PlacesAutoCompleteAdapter extends ArrayAdapter<HashMap<String, String>> implements Filterable {
private ArrayList<HashMap<String, String>> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public HashMap<String, String> getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "AIzaSyCpBpPYqky54Ktp4svdxv249xw9wOKQDAU";
private static final String LOG_TAG = "ExampleApp";
private ArrayList<HashMap<String, String>> autocomplete(String input) {
ArrayList<HashMap<String, String>> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<HashMap<String, String>>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
HashMap<String, String> place = new HashMap<String, String>();
String description = predsJsonArray.getJSONObject(i).getString("description");
String placeId = predsJsonArray.getJSONObject(i).getString("place_id");
place.put("description", description);
place.put("place_id", placeId);
resultList.add(place);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
return resultList;
}
}
When people click on it I have the second string so that is okay:
//When click on autocomplete suggestion
autoCompView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
Map<String, String> map = (Map<String, String>) listView.getItemAtPosition(position);
String location = map.get("description");
String reference = map.get("place_id");
Toast.makeText(getActivity(), location, Toast.LENGTH_SHORT).show();
}
});
In my autocompletetextview it shows the entire hashmap, how can I just show the first string of it and when people click on it get the second string?
Thanks in advance!
Instead of doing getItem()
, I think you can remove getItem()
and implement getView()
First you need to make a XML file for your item view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/list_item_desc"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Line1"/>
</LinearLayout>
remove getItem()
and implement getView()
@Override
public View getView(int rowIndex, View convertView, ViewGroup parent) {
View row = convertView;
if(null == row) {
LayoutInflater layout = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE
);
row = layout.inflate(R.layout.item_view, null);
}
HashMap<String, String> result = resultList.get(rowIndex);
if(null != result) {
TextView descTextView = (TextView) row.findViewById(R.id.list_item_desc);
if(null != desc) {
descTextView.setText(desc.get("description"));
}
}
return row;
}
for your click listener, you can just simply change
Toast.makeText(getActivity(), location, Toast.LENGTH_SHORT).show();
To
Toast.makeText(getActivity(), reference, Toast.LENGTH_SHORT).show();
You can also read this tutorial about how to implement places API in your android app.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.