gpt4 book ai didi

java - 在 arrayadapter 中显示 arraylist 的值以进行自动完成

转载 作者:太空宇宙 更新时间:2023-11-04 14:16:48 25 4
gpt4 key购买 nike

我有一个适配器,它返回一个包含 2 个字符串的 HashMap 。我希望人们看到的是第一个字符串,当人们单击它时,我想要第二个字符串,但我有点不知道如何做到这一点。

这是我的适配器:

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 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();
}
});

在我的自动完成 TextView 中,它显示整个 HashMap ,我如何只显示它的第一个字符串,当人们单击它时获取第二个字符串?

提前致谢!

最佳答案

我认为您可以删除 getItem() 并实现 getView(),而不是执行 getItem()

首先,您需要为项目 View 创建一个 XML 文件:

<?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>

删除getItem()并实现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;
}

对于您的点击监听器,您只需更改即可

Toast.makeText(getActivity(), location, Toast.LENGTH_SHORT).show();

Toast.makeText(getActivity(), reference, Toast.LENGTH_SHORT).show();

您还可以阅读this tutorial关于如何在您的 Android 应用程序中实现地方 API。

关于java - 在 arrayadapter 中显示 arraylist<hashmap> 的值以进行自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27623679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com