我有一个listview,它从服务器上取下数据并显示。显示的数据很好。我也可以从服务器收集纬度,经度值。 但是列表适配器在显示列表后没有更新列表中的反向地理定位地址。这是代码。由于代码是出于我的业务目的,我无法直接显示其他类。但我肯定答案是在本课程中请帮助:
public class CustomListViewAdapter extends BaseAdapter
{
LayoutInflater inflater;
List<ListViewItem> items;
double currentLAT, currentLONG;
public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
holder.position = position;
ListViewItem item = items.get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);
ImageView imgThumbnail = (ImageView) vi.findViewById(R.id.imgThumbnail);
TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);
holder.tLocation = (TextView) vi.findViewById(R.id.tukxiLocation);
imgThumbnail.setImageResource(item.ThumbnailResource);
txtTitle.setText(item.Title);
txtSubTitle.setText(item.SubTitle);
currentLAT = item.currentLAT;
currentLONG = item.currentLONG;
ReverseG thetask = new ReverseG(position, holder);
thetask.execute();
return vi;
}
private class ReverseG extends AsyncTask<Void, Void, String> {
private int mPosition;
private ViewHolder mHolder;
String addressString = "";
private TransitList tl = new TransitList();
public ReverseG(int position, ViewHolder holder){
mPosition = position;
mHolder = holder;
}
@Override
protected String doInBackground(Void... arg0){
try {
Geocoder gc = new Geocoder(tl.getApplicationContext(), Locale.getDefault());
List<Address> addressList = gc.getFromLocation(currentLAT, currentLONG, 1);
StringBuilder add = new StringBuilder();
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
int noOfMaxAddressLine = address
.getMaxAddressLineIndex();
if (noOfMaxAddressLine > 0) {
for (int i = 0; i < address
.getMaxAddressLineIndex() - 1; i++) {
add.append(address.getAddressLine(i)).append(
" ");
}
addressString = "Near " + add.toString();
}
}
} catch (Exception e) {
addressString = "";
}
return addressString;
}
@Override
protected void onPostExecute(String result){
if(mHolder.position == mPosition){
mHolder.tLocation.setText(result);
}
}
}
private static class ViewHolder {
public int position;
public TextView tLocation;
}
}
我在这里使用TransitList类对象将其作为上下文传递,因为列表是在该类中生成的。
编辑:我在上下文中进行了必要的更改。现在它显示每个列表项的地址都很好。 但是在加载地址时,它会逐一显示第一个项目上的所有地址,然后更新剩余的项目。这是更新后的代码:
package com.transit.tukxi;
/**
* Created by sachin on 05/09/13.
*/
import java.util.List;
import java.util.Locale;
import com.transit.tukxi.TransitList.ListViewItem;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListViewAdapter extends BaseAdapter
{
LayoutInflater inflater;
List<ListViewItem> items;
double currentLAT, currentLONG;
Context c;
public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.c = context;
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
holder.position = position;
ListViewItem item = items.get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);
ImageView imgThumbnail = (ImageView) vi.findViewById(R.id.imgThumbnail);
TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);
holder.tLocation = (TextView) vi.findViewById(R.id.tukxiLocation);
imgThumbnail.setImageResource(item.ThumbnailResource);
txtTitle.setText(item.Title);
txtSubTitle.setText(item.SubTitle);
holder.currentLAT = item.currentLAT;
holder.currentLONG = item.currentLONG;
ReverseG thetask = new ReverseG(position, holder);
thetask.execute();
return vi;
}
private class ReverseG extends AsyncTask<Void, Void, String> {
private int mPosition;
private ViewHolder mHolder;
String addressString = "";
public ReverseG(int position, ViewHolder holder){
mPosition = position;
mHolder = holder;
}
@Override
protected String doInBackground(Void... arg0){
try {
Geocoder gc = new Geocoder(c, Locale.getDefault());
List<Address> addressList = gc.getFromLocation(mHolder.currentLAT, mHolder.currentLONG, 1);
StringBuilder add = new StringBuilder();
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
int noOfMaxAddressLine = address
.getMaxAddressLineIndex();
if (noOfMaxAddressLine > 0) {
for (int i = 0; i < address
.getMaxAddressLineIndex() - 1; i++) {
add.append(address.getAddressLine(i)).append(
" ");
}
addressString = "Near " + add.toString();
}
}
} catch (Exception e) {
addressString = "";
}
return addressString;
}
@Override
protected void onPostExecute(String result){
if(mHolder.position == mPosition){
mHolder.tLocation.setText(result);
}
}
}
private static class ViewHolder {
public int position;
public double currentLAT, currentLONG;
public TextView tLocation;
}
}