我有两个适配器 -
1.谷歌放置适配器&
2.我的本地数据库放置适配器。
我想在单个适配器中组合这两个适配器,以便在单个列表视图中显示建议。使用两个不同的列表视图实现两个单独的适配器工作正常。但顶级列表视图只能成为可滚动的一行。但我希望两个列表视图的总结果在一个滚动中。我已将两个列表视图放在scrollview中,但它有同样的问题。两个适配器的代码如下。
public class PlacesAdapter extends ArrayAdapter implements Filterable {
private static final String LOG_TAG = "Google Places Autocomplete";
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 = "XYZ";
private ArrayList<String> resultList;
private ArrayList<String> descriptionList = null;
private ArrayList<String> descriptionList1 = null;
private ArrayList<String> descriptionList2 = null;
private Context context = null;
public ArrayList<String> placeid = null;;
public PlacesAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.context = context;
}
@Override
public int getCount() {
if (resultList != null)
return resultList.size();
else
return 0;
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
public String getPlaceId(int index) {
return placeid.get(index);
}
public ArrayList<String> autocomplete(String input) {
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("&components=country:in");
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", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
Log.d("yo", jsonResults.toString());
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
placeid = new ArrayList(predsJsonArray.length());
resultList = new ArrayList(predsJsonArray.length());
descriptionList = new ArrayList(predsJsonArray.length());
descriptionList1 = new ArrayList(predsJsonArray.length());
descriptionList2 = new ArrayList(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).toString());
String place = predsJsonArray.getJSONObject(i).getString("description");
placeid.add(predsJsonArray.getJSONObject(i).getString("place_id"));
String[] placeindex = place.split(",");
descriptionList.add(placeindex[0]);
descriptionList1.add(placeindex[1]);
descriptionList2.add(placeindex[2]);
}
// saveArray(resultList.toArray(new String[resultList.size()]), "predictionsArray", getContext());
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}catch (IllegalStateException exp){
Log.e(LOG_TAG, "Cannot process JSON results", exp);
}
return descriptionList;
}
@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) {
// setImageVisibility();
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
public class ViewHolder {
TextView tLocation;
TextView tLocationInfo;
}
@Override
@SuppressWarnings("null")
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View vix = convertView;
if (convertView == null) {
if (vix == null) {
LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_stopinfo, null);
}
holder = new ViewHolder();
holder.tLocation = (TextView) convertView.findViewById(R.id.location_name);
holder.tLocationInfo = (TextView) convertView.findViewById(R.id.location_info);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try{
String locationName = descriptionList.get(position).replace(","," ");
String placeName = (descriptionList1.get(position).replace(" ", "")+", "+
descriptionList2.get(position).replace(" ", ""));
if (locationName != null) {
holder.tLocation.setText(locationName);
if (placeName != null) {
holder.tLocationInfo.setText(placeName);
} else {
holder.tLocationInfo.setVisibility(View.GONE);
}
}
}catch (IndexOutOfBoundsException e){
e.printStackTrace();
}
return convertView;
}
}
我的本地数据库适配器 -
public class BusStopAdapter extends ArrayAdapter<BusStops>
{
Context context;
private ArrayList<BusStops> items;
public BusStopAdapter(Context context,ArrayList<BusStops> items, int layout) {
super(context, layout, items);
this.context = context;
this.items = items;
}
public class ViewHolder {
TextView tLocation;
TextView tLocationInfo;
}
@Override
@SuppressWarnings("null")
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View vix = convertView;
if (convertView == null) {
if (vix == null) {
LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_stopinfo, null);
}
holder = new ViewHolder();
holder.tLocation = (TextView) convertView.findViewById(R.id.location_name);
holder.tLocationInfo = (TextView) convertView.findViewById(R.id.location_info);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BusStops stopObject = items.get(position);
if (stopObject != null)
{
holder.tLocation.setText(stopObject.getStopName());
if (stopObject.getStopInfo() != null && stopObject.getStopInfo().length() > 0) {
holder.tLocationInfo.setText(stopObject.getStopInfo());
} else {
holder.tLocationInfo.setVisibility(View.GONE);
}
}
return convertView;
}
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public BusStops getItem(int position) {
return items.get(position);
}
}
这就是我实现文本更改侦听器的方法 -
myAdapter = new PlacesAdapter(LocationListActivity.this,R.layout.row_stopinfo);
listView.setAdapter(myAdapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(mAutocompleteClickListener);
location.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
myAdapter.notifyDataSetChanged();
myAdapter.getFilter().filter(s.toString());
Utils.setDynamicHeight(listView);
busStopList = newBusList(s);
setAdapter();
busStopAdapter.notifyDataSetChanged();
// Utils.setDynamicHeight(mListView);
// Utils.setDynamicHeight(mListView);
// mListView.setAdapter(busStopAdapter);
listView.requestLayout();
}
@Override
public void afterTextChanged(Editable s) {
}
});