android listview asynctask,如何从asyncTask onPostExecute()android填充listView

美好的一天,我试图通过异步任务显示列表视图,并且在列表视图中显示的成功有限.情况就是这样,我有一个已经显示的活动,当用户按下按钮时,它开始执行gps活动,通过异步任务获取用户位置.现在我想显示一个包含地理编码器结果的列表视图,以便用户可以选择他想要的位置,然后更新回活动中的文本视图.我无法显示此列表视图.它根本没有显示.

这是相关代码:

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.view_location);

}

异步任务类:

class locationTask extends AsyncTask > {

List

addresses;

ProgressDialog progressDialog;

Activity location_activity;

public locationTask(Activity activity){

location_activity = activity;

}

protected void onPreExecute(){

progressDialog = ProgressDialog.show(LocationViewer.this, "", "Getting GPS information Location here");

}

protected List

doInBackground(Object... params){

Log.d(TAG, "am in doinbackground now");

addresses = doGeoCode(globalLocation);

int count = addresses.size();

Log.d(TAG, "count in doinbackground = " + count);

Log.d(TAG, "am out of doinbackground now");

return null;

}

@Override

protected void onPostExecute(List

address){

progressDialog.dismiss();

int count = addresses.size();

Log.d(TAG, "count = " + count);

Log.d(TAG, "am in PostExecute now");

locationAdapter adapter = new locationAdapter(LocationViewer.this, android.R.layout.simple_list_item_1, addresses);

adapter.notifyDataSetChanged();

list.setAdapter(adapter);

}

}

doGeocode方法:

public List

doGeoCode(Location location){

globalLocation = location;

double newlatitude = globalLocation.getLatitude();

double newlongitude =globalLocation.getLongitude();

Geocoder geocode = new Geocoder(this, Locale.getDefault());

try{

addresses = geocode.getFromLocation(newlatitude, newlongitude, 1);

} catch(IOException e){}

return addresses;

//return addresses;

}

适配器类:

class locationAdapter extends ArrayAdapter

{

Context mycontext;

public locationAdapter(Context context,int textViewResourceId, List

addresses) {

super(context,textViewResourceId, addresses);

mycontext = context;

}

@Override

public View getView(int position, View convertView, ViewGroup parent){

/*View row = convertView;

if(row == null){

LayoutInflater inflater = getLayoutInflater();

row = inflater.inflate(R.layout.view_list, parent, false);

}*/

int maxAddressLineIndex = getItem(position).getMaxAddressLineIndex();

String addressline = "";

for(int j=0; j <= maxAddressLineIndex; j++){

addressline += getItem(position).getAddressLine(j) + ",";

}

TextView rowAddress = new TextView(mycontext);

rowAddress.setText(addressline);

return rowAddress;

}

}

public void onItemClick(AdapterView> parent, View view, int position, long id) {

StringBuilder sb = new StringBuilder();

sb.append(((Address)parent.getItemAtPosition(position)).getAddressLine(position)).append(",");

sb.append(((Address)parent.getItemAtPosition(position)).getAdminArea()).append(";");

sb.append(((Address)parent.getItemAtPosition(position)).getPostalCode()).append(";");

sb.append(((Address)parent.getItemAtPosition(position)).getCountryName());

address = sb.toString();

}

logcat的:

11-18 11:47:18.538: VERBOSE/LocationManagerService(1350): requestLocationUpdates

11-18 11:47:18.548: DEBUG/WifiService(1350): enable and start wifi due to updateWifiState

11-18 11:47:18.588: INFO/System.out(1459): [INFO:467227473]: LogSource: Running flush

11-18 11:47:18.588: INFO/System.out(1459): [INFO:467227475]: LogSource: Sending payload [bytes=247]

11-18 11:47:18.648: INFO/System.out(1459): [INFO:467227537]: LogSource: Response [http=200,length=219]

11-18 11:47:18.648: INFO/System.out(1459): [INFO:467227538]: LogSource: Read id 119, status code 200

11-18 11:47:18.688: DEBUG/InputManagerService(1350): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@408a9630

11-18 11:47:18.858: DEBUG/SurfaceFlinger(1350): layer=0xb8fdb8 is not in the purgatory list

11-1811:47:19.529: INFO/wpa_supplicant(1548): got scan complete

11-18 11:47:19.529: INFO/wpa_supplicant(1548): wpa_supplicant_get_scan_results:return scan results2

11-18 11:47:19.529: INFO/wpa_supplicant(1548): AP:ssid[ICYSPICY],rssi[-53],BSSID=00:1c:df:73:b2:6c

11-18 11:47:19.529: INFO/wpa_supplicant(1548): AP:ssid[virginmedia2196134],rssi[-91],BSSID=c4:3d:c7:41:12:f3

11-18 11:47:19.529: INFO/wpa_supplicant(1548): AP:ssid[ICYSPICY],rssi[-85],BSSID=00:24:b2:b4:8b:f8

11-18 11:47:19.529: INFO/wpa_supplicant(1548): Received 950 bytes of scan results (3 BSSes)

11-18 11:47:19.529: INFO/wpa_supplicant(1548): wpa_driver_wext_get_scan_results---

11-18 11:47:19.549: DEBUG/GpsLocationProvider(1350): GetGpsInterface+

11-18 11:47:19.549: DEBUG/GpsLocationProvider(1350): GetGpsInterface-

11-18 11:47:19.549: DEBUG/lib_locapi(1350): loc_eng_inject_location, accuracy = 56.0

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - mLocationListener: onLocationChanged() location = Location[mProvider=network,mTime=1321616839556,mInfo=-22.2383714333463.40099234999997,mAccuracy=56.0

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - handleMessage() incoming message, what:1

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - mLocationListener: onLocationChanged() location = Location[mProvider=network,mTime=1321616839556,mInfo=-22.2383714333463.40099234999997,mAccuracy=56.0

11-18 11:47:19.569: DEBUG/AutoSetting(2277): Util - isSetupWizardCompleted(): true

11-18 11:47:19.569: DEBUG/AutoSetting(2277): Util - wifi connected

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - processLocationBundle() distance to current is less than 1000.0m, bypass update

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - handleMessage() within range1

解决方法:

我刚刚遇到同样的问题并解决了它,实际上你只需要这个:

adapter.notifyDataSetChanged();

并且不需要这个:

runOnUIThread(new Runable(){

@Override

public void run(){

adapter.notifyDataSetChanged();

}

};

AsyncTask可以与主UI线程一起使用.

标签:android

来源: https://codeday.me/bug/20190530/1185531.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值