2014-04-22 4 views
0

SplashActivity.java{} Mise à jourTravailler avec des objets curseur dans Android

public class SplashActivity extends Activity { 
    /** Called when the activity is first created. */ 

    JSONObject jsonobject; 
    JSONArray jsonarray; 
    ArrayList<HashMap<String, String>> arraylist; 
    private String Content; 
    DatabaseAdapter db; 
    TextView txtSplashTitle,txtSplashDesc; 
    DatabaseAdapter databaseHelper; 
    Cursor cursor; 


    //@InjectView(R.id.txtSplashDesc) TextView txtSplashDesc=null; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 

     //ButterKnife.inject(this);//using ButterKnife library for viewInjection 
     txtSplashDesc=(TextView) findViewById(R.id.txtSplashDesc); 

     String serverURL = ""; 
     db = new DatabaseAdapter(this); 
     new LongOperation().execute(serverURL); 


     freeMemory(); 
    } 

    @Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 

     //Setting fonts for textviews 
     setCustomFontForTextViews(); 

    } 



    private void setCustomFontForTextViews() { 
     Typeface typeFace = Typeface.createFromAsset(getAssets(), "royalacid.ttf"); 
     txtSplashDesc.setTypeface(typeFace); 
    } 



    // Class with extends AsyncTask class 
    private class LongOperation extends AsyncTask<String, Void, Void> { 

     private final HttpClient Client = new DefaultHttpClient(); 
     private String Error = null; 
     private ProgressDialog Dialog = new ProgressDialog(SplashActivity.this); 


     protected void onPreExecute() { 
      // NOTE: You can call UI Element here. 
      Dialog.setMessage("Downloading source.."); 
      Dialog.show(); 
     } 

     // Call after onPreExecute method 
     protected Void doInBackground(String... urls) { 
      try { 
       // NOTE: Don't call UI Element here. 
       HttpGet httpget = new HttpGet("http://10.0.2.2:3009/findmybuffet/?storedproc=get_app_tables&flag=sudhakar"); 
       ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
       Content = Client.execute(httpget, responseHandler); 
       jsonobject = new JSONObject(Content); 
       jsonobject = jsonobject.getJSONObject("findmybuffet"); 
       jsonarray = jsonobject.getJSONArray("buffets"); 

       for (int i = 0; i < jsonarray.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 
        jsonobject = jsonarray.getJSONObject(i); 
        // Retrive JSON Objects 
        map.put("buf_off_id", jsonobject.getString("buf_off_id")); 
        map.put("from_time", jsonobject.getString("from_time")); 
        map.put("to_time", jsonobject.getString("to_time")); 
        map.put("online_price", jsonobject.getString("online_price")); 
        map.put("reserved_price", jsonobject.getString("reserved_price")); 
        map.put("buf_image", jsonobject.getString("buf_image")); 
        map.put("res_name", jsonobject.getString("res_name")); 
        map.put("rating", jsonobject.getString("rating")); 
        map.put("latitude", jsonobject.getString("latitude")); 
        map.put("longitude", jsonobject.getString("longitude")); 
        map.put("buf_type_name", jsonobject.getString("buf_type_name")); 
        map.put("from_date", jsonobject.getString("from_date")); 
        map.put("to_date", jsonobject.getString("to_date")); 
        map.put("city_id", jsonobject.getString("city_id")); 
        map.put("city_name", jsonobject.getString("city_name")); 
        map.put("meal_type_id", jsonobject.getString("meal_type_id")); 
        map.put("meal_type_name", jsonobject.getString("meal_type_name")); 
        map.put("buf_desc", jsonobject.getString("buf_desc")); 
        map.put("distance", jsonobject.getString("distance")); 

        Log.d("----$$$----", map.toString()); 

        //Calling database 
        db.addContact(map); 

        try { 
         Cursor cursor = (Cursor) databaseHelper.getAllContacts(); 
         cursor.moveToFirst(); 
         if(cursor.moveToFirst()){ 
          do{ 
           String refDestLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(7))); 
           Log.d("[email protected]*@*@*@*@*@----", refDestLatitude+""); 
          }while(cursor.moveToNext()); 
         } 

        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         Log.d("ThrownException", e.toString()); 
         e.printStackTrace(); 
        } 

        //cursor.close(); 
       } 
       // Reading all contacts 
       Log.d("Reading: ", "Reading all contacts.."); 
      } catch (IOException|JSONException e) { 
       Error = e.getMessage(); 
       cancel(true); 
      } 
      return null; 
     } 

     protected void onPostExecute(Void unused) {  
      // Close progress dialog 
      Dialog.dismiss(); 
      Intent intent=new Intent(SplashActivity.this,MainActivitySherlock.class); 
      startActivity(intent); 
     } 
    } 

    private void freeMemory() { 
     jsonobject=null; 
     jsonarray=null; 
     arraylist=null; 
     Content=null; 
    } 

} 

Quand je débogués l'application je l'ai trouvé comme ci-dessous J'ai problème dans la ligne ::

String refDestLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(7))); 

Le curseur est en mesure d'obtenir la valeur

cursor.getColumnIndex(cursor.getColumnName(7)) 

Mais exception Popps quand

cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4))); 

est évalué

Remarque :: Cette ligne fonctionnait quand je menais adaptateur ..... mais ça ne marche pas ici. Ai-je besoin de jeter une référence ou quelque chose?

+0

exception ce que vous obtenez ..? –

+0

entoure cette ligne avec try catch block puis vérifie l'exception –

+0

ajoute cursor.moveToFirst() avant d'appeler cursor.getString() –

Répondre

1

essayer comme ceci:

if(c.moveToFirst()){ 
do{ 
String refDestLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(7))); 
}while(c.moveToNext()) 
} 
+0

@wqrahd ... S'il vous plaît regardez la question mise à jour ... j'ai mis à jour SplashActivity.java & LOg – Devrath

+0

tout semble juste..mais pas obtenir où est le problème. : p ... vérifiez votre code si vous fermez la base de données avant de lire le curseur dans getAllContacts(). –

+0

J'ai posté même le code complet de DatabaseAdapter.java ..... je n'ai pas fermé la connexion DB dans cette classe ... alors pourquoi cette exception se produit – Devrath

1

cursor.getString (cursor.getColumnIndex (cursor.getColumnName (7)));

Vous obtenez une erreur parce qu'il n'y a pas de colonne 7.

Je dois demander pourquoi tout le drame quand vous pourriez obtenir les données de la colonne?

if (getColumnCount() > 11) { // 4+7 = 11 fail 
     cursor.getString(7); 
} 
+0

s'il vous plaît regardez la mise à jour ...... Il ya colonne n ° 7 j'ai posté une nouvelle classe – Devrath

+0

trop de drame pourquoi ne pas obtenir les données de la colonne? cursor.getString (columnIndex); – danny117

+0

[+1] pour le pointage corrigeant mon standard de codage :) – Devrath

Questions connexes