android - 如何从 asyncTask onPostExecute() android 填充 listView

标签 android

美好的一天,我正在尝试通过异步任务显示 ListView ,但在显示的 ListView 中取得的成功有限。情况是这样的,我有一个已经显示的 Activity ,当用户按下按钮时,它开始执行 gps Activity 以通过异步任务获取用户位置。现在我想显示一个包含地理编码器结果的 ListView ,以便用户可以选择他想要的位置,然后它会更新回 Activity 中的 TextView 。无法显示此 ListView 。它根本不显示。

相关代码如下:

public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
    setContentView(R.layout.view_location);

}

异步任务类:

class locationTask extends AsyncTask<Object, Void, List<Address> > {
        List<Address> 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<Address> 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> 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<Address> 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<Address> {
        Context mycontext;

        public locationAdapter(Context context,int textViewResourceId, List<Address> 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();
    }

日志:

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 - 如何从 asyncTask onPostExecute() android 填充 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8181789/

相关文章:

android - scrollView 可以滚动多少?

声明可浏览类别后,Android 应用程序不再可用

javascript - 从 android 连接到 node.js 服务器时遇到问题

android - 工具栏中的色调导航图标

java - 2020如何将media目录下的文件保存到外存?

android - 通过模块与注入(inject)构造函数进行 Dagger 2 配置

android - 通过线性布局移动图像

android - 我的 Android 应用程序占用太多内存?

android - Android 中保存静态变量的 "Globals"类是否安全?

android - ExpandableListView 中的方向更改折叠子项