samedi 19 septembre 2015

Android Google Map adding Markers using stored Sqlite database

My app get stopped When I type "Fair" in Autocompletetextview?

  1. When I type nothing i.e Empty in Autocompletetextview it shows all the location in Map Which I stored in sqlite.

  2. When I type "Excellent" in Autocompletetextview. It Shows only the "Excellent" location in Map.

  3. But, when I type "Fair" in Autocompletetextview. Which is not stored any location in sqlite. It stop my app. I want it to show "Data not available" instead of stopping my app. How can I change in my below code.

Error is :

Process: com.example.bharat.plantnow, PID: 15807
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:86)
        at com.example.bharat.plantnow.MapsActivity$1.onClick(MapsActivity.java:149)

My SQLITE database columns are:

    //Table Columns names
private static final String KEY_TID = "id";
public static final String KEY_TreeID = "treeid";
public static final String KEY_TCONDITION = "condition";
public static final String KEY_TLATITUDE = "latitude";
public static final String KEY_TLONGITUDE = "longitude";

My sqlite Database: I stored the values for "Excellent" & "Good" . Not stores any values for "Fair"

ID  Condition   Latitude    Longitude
1   Excellent   12.897485   15.425974
2   Good        12.897485   15.425974
3   Excellent   12.897485   15.425974
4   Excellent   12.897485   15.425974
5   Excellent   12.897485   15.425974
6   Excellent   12.897485   15.425974
7   Good        12.897485   15.425974
8   Fair        
9   Fair        
10  Fair        


  private AutoCompleteTextView inputSearch;
String[] conditions = {"Excellent",
        "Good",
        "Fair",

};

My MapActivity:

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

            String search = inputSearch.getText().toString();

            if(search.equals("")) {
                double latitude = 0;
                double longitude = 0;
                String mark;

                // SQLite database handler
                db = new TreeData(getApplicationContext());



                Cursor locationCursor = db.getLocations();

                locationCursor.moveToFirst();

                do {
                    latitude = locationCursor.getDouble(locationCursor
                            .getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
                    longitude = locationCursor.getDouble(locationCursor
                            .getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));

                    mark = locationCursor.getString(locationCursor
                            .getColumnIndex(SQLiteHandler.KEY_TreeID));

                    LatLng location = new LatLng(latitude, longitude);

                    mMap.addMarker(new MarkerOptions().position(location).title(mark));


                } while (locationCursor.moveToNext());

            }


            if(search.length()>0){

                //float zoom = 0;
                mMap.clear();

                // SQLite database handler
                db = new TreeData(getApplicationContext());


                Cursor c = db.getSelectedLocations(search);


                c.moveToFirst();

                do {

                   double latitude1 = c.getDouble(c
                            .getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
                    double longitude1 = c.getDouble(c
                            .getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));

                    String marker = c.getString(c
                            .getColumnIndex(SQLiteHandler.KEY_TreeID));

                    LatLng location = new LatLng(latitude1, longitude1);

                    mMap.addMarker(new MarkerOptions().position(location).title(marker));

                    // Moving CameraPosition to last clicked position
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(location));

                    // Setting the zoom level in the map on last position is clicked
                     mMap.animateCamera(CameraUpdateFactory.newLatLng(location));


                } while (c.moveToNext());
                Toast.makeText(getApplicationContext(), "All is Well", Toast.LENGTH_LONG).show();
        }
            else{

                Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGTH_LONG).show();
            }
    }
});}

Aucun commentaire:

Enregistrer un commentaire