执行onPostExecute(Void result)android后调用onListItemClick()

时间:2012-11-28 17:28:52

标签: android android-listview android-asynctask

我在执行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()函数,这样我就可以在每个项目上点击另一个活动。

任何帮助都会很明显。

1 个答案:

答案 0 :(得分:1)

删除

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

来自班级GetPlaces

并输入CheckoutNearestPlaces