2012-08-27 93 views
0

我會直接指向這一點。我有一個班,我有6個按鈕。每個按鈕保存到SQLite數據庫一些參數然後它啓動一個活動。ListView,遊標和空集

新活動取參數並查詢數據庫以相應地提取數據。當活動啓動時,我清除參數爲了再次保存它們,如果我按另一個或相同的按鈕。

如果1個表(鏈接到按鈕)爲空,它會返回我想要的消息。問題是如果1是空的,那麼即使他們有數據,所有的表也會返回消息!

我一等

public class HRecords extends Activity { 
SQLiteDatabase myDB=null; 
Button tes,con,al,me,pr,va; 

protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.records); 
    tes=(Button) findViewById(R.id.testbut); 
    con=(Button) findViewById(R.id.condbut); 
    al=(Button) findViewById(R.id.albut); 
    me=(Button) findViewById(R.id.medbut); 
    pr=(Button) findViewById(R.id.procbut); 
    va=(Button) findViewById(R.id.vacbut); 

    Database openHelper = new Database(this);//create new Database to take advantage of the SQLiteOpenHelper class 
    myDB = openHelper.getWritableDatabase(); // or getWritableDatabase(); 
    myDB=SQLiteDatabase.openDatabase("data/data/com.example.login2/databases/aeglea", null, SQLiteDatabase.OPEN_READWRITE);//set myDB to aeglea 

    doclicks(); 

} 

private void doclicks(){ 
    tes.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       ContentValues values = new ContentValues(); 
       values.put("ton", "Tests"); 
       values.put("tazle","user_test"); 
       myDB.insert("history_go",null, values); 
       //create new intent 
       Intent record = new Intent(getApplicationContext(), Record.class); 
       // Close all views before launching logged 
       record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(record); 
       // Close Login Screen 
       onPause(); 
      }}); 
    con.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       ContentValues values = new ContentValues(); 
       values.put("ton", "Medical Conditions"); 
       values.put("tazle","user_cond"); 
       myDB.insert("history_go",null, values); 
       //create new intent 
       Intent record = new Intent(getApplicationContext(), Record.class); 
       // Close all views before launching logged 
       record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(record); 
       // Close Login Screen 
       onPause(); 
      }}); 
    al.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       ContentValues values = new ContentValues(); 
       values.put("ton", "Allergies"); 
       values.put("tazle","user_all"); 

       myDB.insert("history_go",null, values); 
       //create new intent 
       Intent record = new Intent(getApplicationContext(), Record.class); 
       // Close all views before launching logged 
       record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(record); 
       // Close Login Screen 
       onPause(); 
      }}); 
    me.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       ContentValues values = new ContentValues(); 
       values.put("ton", "Medication"); 
       values.put("tazle","user_med"); 

       myDB.insert("history_go",null, values); 
       //create new intent 
       Intent record = new Intent(getApplicationContext(), Record.class); 
       // Close all views before launching logged 
       record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(record); 
       // Close Login Screen 
       onPause(); 
      }}); 
    pr.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       ContentValues values = new ContentValues(); 
       values.put("ton", "Medical Procedures"); 
       values.put("tazle","user_proc"); 

       myDB.insert("history_go",null, values); 
       //create new intent 
       Intent record = new Intent(getApplicationContext(), Record.class); 
       // Close all views before launching logged 
       record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(record); 
       // Close Login Screen 
       onPause(); 
      }}); 
    va.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       ContentValues values = new ContentValues(); 
       values.put("ton", "Vaccinations"); 
       values.put("tazle","user_vacc"); 

       myDB.insert("history_go",null, values); 
       //create new intent 
       Intent record = new Intent(getApplicationContext(), Record.class); 
       // Close all views before launching logged 
       record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(record); 
       // Close Login Screen 
       onPause(); 
      }}); 
} 

} 

正如你可以看到它就會從SQLite數據庫的數據(你也可以看到空的結果的消息第二類「這裏沒有什麼增加。進入網站添加更多。「)

public class Record extends Activity{ 
SQLiteDatabase myDB=null; 
TextView title=null; 
Cursor cur,cur2=null; 
ListView list=null; 
private ArrayAdapter<String> listAdapter ; 

protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.record); 
    title = (TextView) findViewById(R.id.recordTitle); 
    list = (ListView) findViewById(R.id.listView1); 

    Database openHelper = new Database(this); 
    myDB = openHelper.getReadableDatabase(); 
    myDB=SQLiteDatabase.openDatabase("data/data/com.example.login2/databases/aeglea", null, SQLiteDatabase.OPEN_READONLY); 

    Database db = new Database(getApplicationContext()); 
    cur = fetchOption("SELECT * FROM history_go"); 
    title.setText(cur.getString(cur.getColumnIndex("ton"))); 


    ArrayList<String> itemlist = new ArrayList<String>(); 
    String[] names=null; 
    //do query 
    cur2=fetchOption("SELECT * FROM "+cur.getString(cur.getColumnIndex("tazle"))); 
    //check for results 
    if (cur2.getCount()==0) { 
     names = new String[] { "Nothing Added here. Go to the site to add more."}; 
    }else{ 
     names = new String[] {cur2.getString(cur2.getColumnIndex("name"))}; 

    } 
    //add the array as list to the ArrayList 
    itemlist.addAll(Arrays.asList(names)); 
    listAdapter = new ArrayAdapter<String>(this, R.layout.item, itemlist); 
    //if results add the rest 
    if(cur2.getCount()!=0){ 
     for(int i=0;i<(cur2.getCount()-1);i++){ 
      cur2.moveToNext(); 
      listAdapter.add(cur2.getString(cur2.getColumnIndex("name"))); 
     } 
    } 
    // Set the ArrayAdapter as the ListView's adapter. 
    list.setAdapter(listAdapter); 
    //remove the navigation history 
    db.resetHistoryNavigation(); 
    cur.close(); 
    cur2.close(); 
} 



public Cursor fetchOption(String query) throws SQLException { 
    Cursor mCursor = myDB.rawQuery(query, null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

} 

編輯

另外,我忘了提,如果我設置的驗證條件CUR2 == null由於光標超出邊界(for循環觸發),應用程序會崩潰

回答

0

我發現錯誤。空集觸發了捕捉異常,之後沒有收到任何其他內容。所以每個表都是空的。

我不得不實施多的try/catch

private void doclicks(){ 
    hr.setOnClickListener(new View.OnClickListener() { 
     private Database db = new Database(getApplicationContext()); 
     JSONArray allergy,condition,medication,procedure,test,vaccine; 
      public void onClick(View v) { 
       ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
       postParameters.add(new BasicNameValuePair("uid", cursor.getString(cursor.getColumnIndex("uid")))); 
       JSONObject response = null; 
       try { 
        CustomHttpTask asdf = new CustomHttpTask(); 
        response = asdf.execute("http://192.168.1.4/aeglea/android/history.php", postParameters).get(); 

        if(response.getString("success").equals("1")){ 
         //get JSON Arrays 
         try{test = response.getJSONArray("test");}catch(Exception e){} 
         try{allergy = response.getJSONArray("allergy");}catch(Exception e){} 
         try{condition = response.getJSONArray("condition");}catch(Exception e){} 
         try{medication = response.getJSONArray("medication");}catch(Exception e){} 
         try{procedure = response.getJSONArray("procedure");}catch(Exception e){} 
         try{vaccine = response.getJSONArray("vaccine");}catch(Exception e){} 

         } 
       }catch (Exception e) { 
         Log.e("HHHERPT","YO MAMAA "+e); 
        } 
         //temp JSONOnject 
         JSONObject buffer=null; 

         //reset older values 
         db.resetHistory(); 

         //Store values 
         try{ 
         for(int i=0;i<allergy.length();i++){ 
          buffer=allergy.getJSONObject(i); 
          db.addRecords("allergy", 
            Integer.parseInt(buffer.getString("id")), 
            Integer.parseInt(buffer.getString("uid")), 
            buffer.getString("name"), 
            1, 
            "", 
            (float) 0.1, 
            1, 
            1); 
         } 
         }catch(Exception e){ 

         } 
         try{ 
         for(int i=0;i<condition.length();i++){ 
          buffer=condition.getJSONObject(i); 
          db.addRecords("condition", 
            Integer.parseInt(buffer.getString("id")), 
            Integer.parseInt(buffer.getString("uid")), 
            buffer.getString("name"), 
            Integer.parseInt(buffer.getString("year")) , 
            "",(float) 0.1, 
            Integer.parseInt(buffer.getString("current")), 
            1); 
         } 
         }catch(Exception e){ 

         } 
         try{ 
         for(int i=0;i<medication.length();i++){ 
          buffer=medication.getJSONObject(i); 
          db.addRecords("medication", 
            Integer.parseInt(buffer.getString("id")), 
            Integer.parseInt(buffer.getString("uid")), 
            buffer.getString("name"), 
            1, 
            "", 
            (float) 0.1, 
            Integer.parseInt(buffer.getString("current")), 
            1); 
         } 
         }catch(Exception e){ 

         } 
         try{ 
         for(int i=0;i<procedure.length();i++){ 
          buffer=procedure.getJSONObject(i); 
          db.addRecords("procedure", 
            Integer.parseInt(buffer.getString("id")), 
            Integer.parseInt(buffer.getString("uid")), 
            buffer.getString("name"), 
            Integer.parseInt(buffer.getString("year")), 
            buffer.getString("comments"), 
            (float) 0.1, 
            0, 
            1); 
         } 
         }catch(Exception e){ 

         } 

         try{ 
         for(int i=0;i<vaccine.length();i++){ 
          buffer=vaccine.getJSONObject(i); 
          db.addRecords("vaccine", 
            Integer.parseInt(buffer.getString("id")), 
            Integer.parseInt(buffer.getString("uid")), 
            buffer.getString("name"), 
            Integer.parseInt(buffer.getString("year")), 
            "", 
            (float) 0.1, 
            0, 
            1); 
         } 
         }catch(Exception e){ 

         } 
         try{ 
          for(int i=0;i<test.length();i++){ 
           buffer=test.getJSONObject(i); 
           db.addRecords("test", 
             Integer.parseInt(buffer.getString("id")), 
             Integer.parseInt(buffer.getString("uid")), 
             buffer.getString("name"), 
             Integer.parseInt(buffer.getString("year")), 
             buffer.getString("comments"), 
             (float) Float.parseFloat(buffer.getString("value")), 
             1, 
             1); 
          } 
          }catch(Exception e){ 

          } 

       //create new intent 
       Intent records = new Intent(getApplicationContext(), HRecords.class); 
       // Close all views before launching logged 
       records.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(records); 
       // Close Login Screen 
       onPause(); 
      } 
     }); 

}