我正在做一个谷歌地图应用程序,我试图添加一个搜索框与自动完成与谷歌地图的地方,我已经有代码,但它有2个错误,我不知道如何解决它,我希望你能帮助我< / p>
错误1:
@Override
public GeoSearchResult getItem(int index) {
return resultList.get(index);
}
错误在返回行中,它表示“不兼容的类型,必需:GeoSearchResult,found:java.lang.object”
错误2:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.geo_search_result_item, parent, false);
}
((TextView) convertView.findViewById(R.id.geo_search_result_text)).setText(getItem(position).getAddress());
return convertView;
}
错误发生在“convertView = inflater.inflate(R.layout.geo_search_result_item,parent,false);”行,它说“无法解析geo_search_result_item”,这就是获取代码的方式
下面的完整代码
GeoAutoCompleteAdapter.class
package com.example.luiggi.myapplication;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by andre_000 on 11/9/2015.
*/
public class GeoAutoCompleteAdapter extends BaseAdapter implements Filterable {
private static final int MAX_RESULTS = 10;
private Context mContext;
private List resultList = new ArrayList();
public GeoAutoCompleteAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public GeoSearchResult getItem(int index) {
return resultList.get(index);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.geo_search_result_item, parent, false);
}
((TextView) convertView.findViewById(R.id.geo_search_result_text)).setText(getItem(position).getAddress());
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
List locations = findLocations(mContext, constraint.toString());
// Assign the data to the FilterResults
filterResults.values = locations;
filterResults.count = locations.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
resultList = (List) results.values;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
private List<GeoSearchResult> findLocations(Context context, String query_text) {
List<GeoSearchResult> geo_search_results = new ArrayList<GeoSearchResult>();
Geocoder geocoder = new Geocoder(context, context.getResources().getConfiguration().locale);
List<Address> addresses = null;
try {
// Getting a maximum of 15 Address that matches the input text
addresses = geocoder.getFromLocationName(query_text, 15);
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
if(address.getMaxAddressLineIndex() != -1)
{
geo_search_results.add(new GeoSearchResult(address));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return geo_search_results;
}
}
GeoSearchResult.class
import android.location.Address;
/**
* Created by andre_000 on 11/9/2015.
*/
public class GeoSearchResult {
private Address address;
public GeoSearchResult(Address address)
{
this.address = address;
}
public String getAddress(){
String display_address = "";
display_address += address.getAddressLine(0) + "\n";
for(int i = 1; i < address.getMaxAddressLineIndex(); i++)
{
display_address += address.getAddressLine(i) + ", ";
}
display_address = display_address.substring(0, display_address.length() - 2);
return display_address;
}
public String toString(){
String display_address = "";
if(address.getFeatureName() != null)
{
display_address += address + ", ";
}
for(int i = 0; i < address.getMaxAddressLineIndex(); i++)
{
display_address += address.getAddressLine(i);
}
return display_address;
}
}
GeoSearchResult.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/geo_search_result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" />
答案 0 :(得分:0)
您应该使用支持库而不是ActionBarSherlock,它现在已被弃用。
答案 1 :(得分:0)
我遇到了同样的问题,但是在重建之后它已经消失了。Build > Rebuild Project
但是同一行上的getAddress()无法解决,但它已经修复但是再次删除并添加它。