1

执行 onPostExecute(Void result) 后调用 onListItemClick() 函数时遇到问题

下面是我的代码片段。

package com.myapp.checkoutnhangout;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CheckoutNearestPlaces extends ListActivity {
    private String[] placeName;
    private String[] imageUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new GetPlaces(this,getListView()).execute();
    }

    class GetPlaces extends AsyncTask<Void, Void, Void> {

        Context context;
        private ListView listView;
        private ProgressDialog progressDialog;

        public GetPlaces(Context context , ListView listView) {
            this.context = context;
            this.listView = listView;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(context);
            progressDialog.setIndeterminate(true);
            progressDialog.setTitle("Loading");
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                findNearLocation();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            } catch (UnrecoverableKeyException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (KeyStoreException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progressDialog.dismiss();
            setListAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,placeName));
//          this.listView.setAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,placeName));
        }

        public void onListItemClick(ListView listView , View v , int position , long id){
            System.out.println("position------------"+placeName[position]);
        }

        public void findNearLocation() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {
            PlacesService placesService = new PlacesService("AIzaSyC3y3fOMTqTkCjNNETQTCKustG7BRk_eVc");

            List<Place> findPlaces = placesService.findPlaces(28.6752, 77.4362, "");

            int findPlacesSize = findPlaces.size();
            placeName = new String[findPlacesSize];
            imageUrl = new String[findPlacesSize];

            for(int index = 0 ; index < findPlacesSize ; index ++) {
                    Place placeDetail = findPlaces.get(index);

//                  System.out.println("name of place : "+placeDetail.getName());
                    placeName[index] = placeDetail.getName();

//                  System.out.println("url of place : "+placeDetail.getIcon());
                    imageUrl[index] = placeDetail.getIcon();
            }
        }

    }
}

上面的代码使用 google palce api 并根据用户的兴趣显示位置列表。之后,我想调用 onListItemClick() 函数,这样我就可以在每次单击项目时调用另一个活动。

任何帮助都将是可观的。

4

1 回答 1

1

消除

public void onListItemClick(ListView listView , View v , int position , long id){
            System.out.println("position------------"+placeName[position]);
        }

从班级GetPlaces

并放入CheckoutNearestPlaces

于 2012-11-28T17:33:22.427 回答