0

現在、Google マップ アプリを作成しており、ユーザーがリストビューの項目をクリックした後にアプリを更新しようとしています。ただし、listviewadapter がコードの別の部分にあるため、これは計画どおりには進んでいません。コードの特定の部分を変更するなど、複数の方法を試しましたが、staticこれはGoogleマップのコードを壊すだけです。

マップがある私の主な活動からのコードは次のとおりです。

public class MainActivity extends ActionBarActivity {

GoogleMap googleMap;

// products JSONArray
JSONArray locations = null;

ListView townList;
ListViewAdapter townAdapter;
String[] townID;    
String[] townName;
ArrayList<TownSelector> arraylist = new ArrayList<TownSelector>();
public static String currentLocationID;

public String getLocationID()
{
    return this.currentLocationID;
}
public static String setLocationID(String l)
{
    return currentLocationID = l;
}

public static boolean refreshMap = false;

public boolean getRefreshMap()
{
    return this.refreshMap;
}
public static boolean setRefreshMap(Boolean m)
{
    return refreshMap = m;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //setContentView(R.layout.search);  

    // Locate the ListView in listview_main.xml
    townList = (ListView) findViewById(R.id.listview);
}

private void setUpMap() {
    // enable MyLocation Layer of the Map
    googleMap.setMyLocationEnabled(true);

    // get LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // Get current Location
    Location myLocation = locationManager.getLastKnownLocation(provider);

    //set map type
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    // Get lattitude of the current location
    double latitude = myLocation.getLatitude();

    //get longitude of the current location
    double longitude = myLocation.getLongitude();

    //Create a LatLong object for the current location
    LatLng latLng = new LatLng(latitude,longitude);

    // show the current location in google map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
    //googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title("Location").snippet("You're here"));
}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllInfo extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading Bars. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters            
        List<NameValuePair> paramsLocations = new ArrayList<NameValuePair>();
        // getting JSON string from URL            
        JSONObject jsonLocations = jParser.makeHttpRequest(url_all_locations, "GET", paramsLocations);

        //Get all Locations
        if(jsonLocations != null)
        {
            // Check your log cat for JSON reponse
            Log.d("All Locations: ", jsonLocations.toString());
            try {

                // Checking for SUCCESS TAG
                int success = jsonLocations.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Locations
                    locations = jsonLocations.getJSONArray(TAG_LOCATIONS);

                    // looping through All Offers
                    for (int i = 0; i < locations.length(); i++) {
                        JSONObject c = locations.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_LID);
                        String locationName = c.getString(TAG_LNAME);

                        // creating new HashMap
                        HashMap<String, String> locationsListMap = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        locationsListMap.put(TAG_LID, id);
                        locationsListMap.put(TAG_LNAME, locationName);

                        // adding HashList to ArrayList
                        locationList.add(locationsListMap);

                    }
                }                   
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return null;            
    } 

    }

}

}

ListViewAdapter アクティビティのコードは次のとおりです。

 view.setOnClickListener(new OnClickListener() { 
        @Override
        public void onClick(View arg0) {            

            MainActivity.setLocationID(locationlist.get(position).getID());
            MainActivity.setRefreshMap(true);
        }
    });

    return view;
}

助言がありますか?ユーザーがリスト項目をクリックしたときに静的ブール値がfalseからtrueに変わることを確認する更新機能を追加したいと思いますが、これよりも簡単な方法があるはずです

編集:setOnItemClickListenerメイン アクティビティに を追加し、Log.d 文字列を入力して、それが機能するかどうかを確認しました。リストにある 4 つの町の名前のいずれかにヒットした後、何も受信しません。

    townList.setOnItemClickListener(new OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
           Log.d("Hello","Why?");            
       } 
    });
4

1 に答える 1