0

我会直奔主题。我有 1 个班级,其中有 6 个按钮。每个按钮都会将一些参数保存到 SQLiteDatabase ,然后它会启动一个 Activity。

新的 Activity 接受参数并查询数据库以相应地提取数据。当活动启动时,如果我按下另一个或相同的按钮,我会清除参数以便再次保存它们。

如果 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 循环触发)

4

1 回答 1

0

我发现出了什么问题。空集触发了 catch 异常,之后没有收到任何其他内容。所以每个表都是空的。

我必须实现多个 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();
            }
        });

}
于 2012-08-27T10:36:30.950 回答