2013-03-09 71 views
0

晚上好,好心的人。我希望將的所有數據從我的數據庫中取出並顯示在TextView中。我怎樣才能做到這一點?此外,我想將數據(全部)從數據庫中提取出來並通過soap消息發送給遠處的WS。我沒有任何關於WSDL設置的問題,但是我確實需要關於如何從數據庫中提取數據並反覆使用的信息。從SQLite數據庫中提取數據並在文本視圖中顯示

下面是代碼:

package com.example.prototype2; 



import android.os.Bundle; 
import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.support.v4.app.NavUtils; 

public class AddGlucose extends Activity implements OnClickListener { 

final String LOG_TAG = "myLogs"; 

Button btnAdd, btnRead, btnClear; 
    EditText etGlucose, etTime, etDate; 

    DBHelper dbHelper; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_add_glucose); 
// Show the Up button in the action bar. 
setupActionBar(); 

btnAdd = (Button) findViewById(R.id.btnAdd); 
btnAdd.setOnClickListener(this); 

btnRead = (Button) findViewById(R.id.btnRead); 
btnRead.setOnClickListener(this); 

btnClear = (Button) findViewById(R.id.btnClear); 
btnClear.setOnClickListener(this); 

etGlucose = (EditText) findViewById(R.id.etGlucose); 
etTime = (EditText) findViewById(R.id.etTime); 
etDate = (EditText) findViewById(R.id.etDate); 
dbHelper = new DBHelper(this); 
} 

/** 
* Set up the {@link android.app.ActionBar}. 
*/ 
private void setupActionBar() { 

getActionBar().setDisplayHomeAsUpEnabled(true); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.add_glucose, menu); 
return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
switch (item.getItemId()) { 
case android.R.id.home: 
    // This ID represents the Home or Up button. In the case of this 
    // activity, the Up button is shown. Use NavUtils to allow users 
    // to navigate up one level in the application structure. For 
    // more details, see the Navigation pattern on Android Design: 
    // 
    // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
    // 
    NavUtils.navigateUpFromSameTask(this); 
    return true; 
} 
return super.onOptionsItemSelected(item); 
} 

@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
ContentValues cv = new ContentValues(); 
String glucose = etGlucose.getText().toString(); 
String time = etTime.getText().toString(); 
String date = etDate.getText().toString(); 

SQLiteDatabase db = dbHelper.getWritableDatabase(); 

switch (v.getId()) { 
case R.id.btnAdd: 
    Log.d(LOG_TAG, "--- Insert in mytable: ---"); 

    cv.put("glucose", glucose); 
    cv.put("time", time); 
    cv.put("date", date); 
long rowID = db.insert("mytable", null, cv); 
    Log.d(LOG_TAG, "row inserted, ID = " + rowID); 
    break; 

case R.id.btnRead: 
    Log.d(LOG_TAG, "--- Rows in mytable: ---"); 
    Cursor c = db.query("mytable", null, null, null, null, null, null, null); 
    if (c.moveToFirst()) { 
     int idColIndex = c.getColumnIndex("id"); 
     int glucoseColIndex = c.getColumnIndex("glucose"); 
     int timeColIndex = c.getColumnIndex("time"); 
     int dateColIndex = c.getColumnIndex("date"); 

     do {  

      Log.d(LOG_TAG, 
        "ID = " + c.getInt(idColIndex) + 
        ", glucose = " + c.getString(glucoseColIndex) + 
        ", time = " + c.getString(timeColIndex) + ", date = " +  c.getString(dateColIndex)); 
     } while (c.moveToNext()); 
    } else 
     Log.d(LOG_TAG, "0 rows"); 
    c.close(); 
    break; 

case R.id.btnClear: 
    Log.d(LOG_TAG, "--- Clear mytable: ---"); 

    int clearCount = db.delete("mytable", null, null); 
    Log.d(LOG_TAG, "deleted rows count = " + clearCount); 
    break; 
} 
dbHelper.close(); 
} 

class DBHelper extends SQLiteOpenHelper { 

    public DBHelper(Context context) { 

     super(context, "mytable", null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     Log.d(LOG_TAG, "--- onCreate database ---"); 
     // создаем таблицу с полями 
     db.execSQL("create table mytable (" 
      + "id integer primary key autoincrement," 
      + "glucose text," 
      + "time text," + "date text" + ");"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 
    } 

} 
+0

textview.setText( 「ID =」 + c.getInt(idColIndex)+ 「葡萄糖= 」+ c.getString(glucoseColIndex)+ 「,時間=」 + c.getString(timeColIndex )) – DjHacktorReborn 2013-03-09 21:20:05

+0

我真的不知道你的問題,因爲你已經發布了檢索所有需要的數據的代碼。 – Griddo 2013-03-09 22:10:46

+0

它進入日誌,而不是textview。我很困惑,並且由於缺乏經驗,對幫助 – 2013-03-09 22:36:18

回答

0

你剛剛在一個類中插入了這麼多的代碼,似乎沒有紅線。 按照本教程和以下教程,只需將解釋的代碼更改爲您的需要即可。一切正常,你將被教導如何從數據庫中獲取信息。

數據庫是非常重要的,你應該佔用一點點。

Tut 1

相關問題