2013-09-21 44 views
4

我正在處理android上的sqlite數據庫項目。這是我第一個sqlite項目。我讀了很多文章並創建了我的應用程序,但存在一個問題,我無法找到解決方法。我希望你能爲我展示一條道路。 問題是,當我通過單擊按鈕調用活動時,設備啓動了一條消息(「不幸的應用程序已停止」)。Android從sqlite數據庫獲取字符串到自動完成文本視圖

我想從sqlite數據庫的字符串到autocompletetextview。

SQLite的databasehelper類的代碼

package com.example.matik; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 

public class Veritabani extends SQLiteOpenHelper { 

     private static final String VERITABANI="KAYISGDATA.db"; 
     private static final int SURUM=1; 

     public static final String TABLE_TODO = "nace"; 
     public static final String COLUMN_ID = "_id"; 
     public static final String COLUMN_CATEGORY = "KOD"; 
     public static final String COLUMN_SUMMARY = "ACK"; 
     public static final String COLUMN_DESCRIPTION = "TEH"; 

     private static final String DB_DROP = "DROP TABLE IF EXISTS nace"; 
     private static final String DATABASE_CREATE = "create table " 
        + TABLE_TODO 
        + "(" 
        + COLUMN_ID + " integer primary key autoincrement, " 
        + COLUMN_CATEGORY + " text not null, " 
        + COLUMN_SUMMARY + " text not null," 
        + COLUMN_DESCRIPTION 
        + " text not null" 
        + ");"; 

    public Veritabani(Context con, String name, CursorFactory factory, 
      int version) { 
     super(con, VERITABANI, null, SURUM); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(DATABASE_CREATE); 
     db.execSQL("INSERT INTO nace (KOD,ACK,TEH) VALUES('01.11.07','Baklagillerin yetiştirilmesi','Tehlikeli')");} 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL(DB_DROP); 
     onCreate(db); 
    } 


} 

活動類的代碼

package com.example.matik; 
import java.util.ArrayList; 

import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.EditText; 

public class Matik extends Activity { 
    AutoCompleteTextView auto; 
    EditText nc; 
    EditText th; 
    Veritabani VB; 
    private ArrayAdapter<String> arrayAdapter; 
    ArrayList<String> Liste = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_matik); 
     Liste = new ArrayList<String>(); 
     doldur(); 
     nc = (EditText) findViewById(R.id.editText2); 
     th = (EditText) findViewById(R.id.editText3); 
     auto = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 
     arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item, Liste); 
     auto.setThreshold(1); 
     auto.setAdapter(arrayAdapter); 
    } 

    private void doldur() { 
     SQLiteDatabase Db = VB.getReadableDatabase(); 
     Cursor c = Db.rawQuery("Select ACT From nace", null); 
     Db.isOpen(); 
     while(c.moveToNext()){ 
      Liste.add(c.getString(c.getColumnIndex("ACT"))); 
     }; 


    } 

} 
+0

使用LogCat檢查在出現「應用程序已停止」對話框時生成的Java堆棧跟蹤。 – CommonsWare

+0

LogCat上的致命異常主錯誤 –

+0

我建議您閱讀如何解釋Java堆棧跟蹤,因爲這對於您在Java開發中很重要:http://stackoverflow.com/questions/3988788/what-is- a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors http://stackoverflow.com/questions/12688068/how-to-read-and-understand-the -java-stack-trace如果您很難理解錯誤消息的含義,請將* entire *堆棧跟蹤複製並粘貼到您的StackOverflow問題中。 – CommonsWare

回答

4

使用SimpleCursorAdapter,如在this article所示。

_descriptionText = (AutoCompleteTextView) findViewById(R.id.description); 
final int[] to = new int[]{android.R.id.text1}; 
final String[] from = new String[]{VehicleDescriptionsTable.DESCRIPTION}; 
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
     android.R.layout.simple_dropdown_item_1line, 
     null, 
     from, 
     to); 

// This will provide the labels for the choices to be displayed in the AutoCompleteTextView 
adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { 
    @Override 
    public CharSequence convertToString(Cursor cursor) { 
     final int colIndex = cursor.getColumnIndexOrThrow(VehicleDescriptionsTable.DESCRIPTION); 
     return cursor.getString(colIndex); 
    } 
}); 

// This will run a query to find the descriptions for a given vehicle. 
adapter.setFilterQueryProvider(new FilterQueryProvider() { 
    @Override 
    public Cursor runQuery(CharSequence description) { 
     String vehicle = getSelectedVehicle(); 
     Cursor managedCursor = _helper.getDescriptionsFor(vehicle, description.toString()); 
     Log.d(TAG, "Query has " + managedCursor.getCount() + " rows of description for " + vehicle); 
     return managedCursor; 
    } 
}); 

_descriptionText.setAdapter(adapter); 

從這裏,你只需要添加一個函數返回一個指針與您的數據庫,適配器添加到您的AutoTextCompleteTextView。您需要從日誌查詢中獲取_id,實際上,您可能會考慮獲取整行(SELECT * FROM table)。如果您沒有列_id,請嘗試SELECT data, rowid AS _id FROM table

相關問題