我的工作在Android上的SQLite数据库项目。而且这是我第一次源码的项目。我阅读了大量文章,并建立了我的应用程序,但还有我能不能找到一种方法来解决它的问题。我希望你能证明的方式对我来说。 问题是,当我打电话活性通过点击一个按钮,设备启动一个消息(不幸的是应用程序停止)。
我试图从SQLite数据库字符串autocompletetextview。
sqlite的databasehelper类的codeS
包com.example.matik;
进口android.content.Context;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteDatabase.CursorFactory;
进口android.database.sqlite.SQLiteOpenHelper;
公共类Veritabani扩展SQLiteOpenHelper {
私有静态最后弦乐VERITABANI =KAYISGDATA.db;
私有静态最终诠释SURUM = 1;
公共静态最后弦乐TABLE_TODO =NACE;
公共静态最后弦乐COLUMN_ID =_id;
公共静态最后弦乐COLUMN_CATEGORY =KOD;
公共静态最后弦乐COLUMN_SUMMARY =确认;
公共静态最后弦乐COLUMN_DESCRIPTION =TEH;
私有静态最后弦乐DB_DROP =DROP TABLE IF EXISTS NACE;
私有静态最后弦乐DATABASE_CREATE =创建表
+ TABLE_TODO
+(
+ COLUMN_ID +整数主键自动增量,
+ COLUMN_CATEGORY +文字不为空,
+ COLUMN_SUMMARY +文字不为空,
+ COLUMN_DESCRIPTION
+文本不为空
+);;
公共Veritabani(上下文CON,字符串名称,CursorFactory厂,
INT版){
超(CON,VERITABANI,空,SURUM);
// TODO自动生成构造函数存根
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
// TODO自动生成方法存根
db.execSQL(DATABASE_CREATE);
db.execSQL(INSERT INTO NACE(KOD,ACK,TEH)VALUES('01 .11.07','Baklagillerinyetiştirilmesi','Tehlikeli'));}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
db.execSQL(DB_DROP);
的onCreate(DB);
}
}
活动课的codeS
包com.example.matik;
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.os.Bundle;
进口android.widget.ArrayAdapter;
进口android.widget.AutoCompleteTextView;
进口android.widget.EditText;
公共类Matik延伸活动{
AutoCompleteTextView汽车;
NC的EditText;
的EditText日;
Veritabani VB;
私人ArrayAdapter<字符串> arrayAdapter;
ArrayList的<字符串>清单当然=新的ArrayList<字符串>();
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_matik);
清单当然=新的ArrayList<字符串>();
doldur();
NC =(EditText上)findViewById(R.id.editText2);
TH =(EditText上)findViewById(R.id.editText3);
自动=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
arrayAdapter =新的ArrayAdapter<字符串>(这一点,android.R.layout.select_dialog_item,清单当然);
auto.setThreshold(1);
auto.setAdapter(arrayAdapter);
}
私人无效doldur(){
SQLiteDatabase分贝= VB.getReadableDatabase();
光标C = Db.rawQuery(选择ACT从NACE,NULL);
Db.isOpen();
而(c.moveToNext()){
Liste.add(c.getString(c.getColumnIndex(ACT)));
};
}
}
解决方案
使用一个SimpleCursorAdapter,如图的这篇文章。
_descriptionText =(AutoCompleteTextView)findViewById(R.id.description);
最终诠释[]到=新INT [] {android.R.id.text1};
最终字符串从=新的String [] {} VehicleDescriptionsTable.DESCRIPTION [];
SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,
android.R.layout.simple_dropdown_item_1line,
空值,
从,
至);
//这将提供标签的选择,以显示在AutoCompleteTextView
adapter.setCursorToStringConverter(新SimpleCursorAdapter.CursorToStringConverter(){
@覆盖
公众的CharSequence convertToString(光标指针){
最终诠释colIndex = cursor.getColumnIndexOrThrow(VehicleDescriptionsTable.DESCRIPTION);
返回cursor.getString(colIndex);
}
});
//这将运行查询以找到对于给定的车辆的说明。
adapter.setFilterQueryProvider(新FilterQueryProvider(){
@覆盖
公共光标runQuery(CharSequence的描述){
串车辆= getSelectedVehicle();
光标managedCursor = _helper.getDescriptionsFor(车辆,description.toString());
Log.d(TAG,查询+ managedCursor.getCount()+行描述为+车辆);
返回managedCursor;
}
});
_descriptionText.setAdapter(适配器);
在这里,你只需要添加一个函数返回一个指针与您的数据库,然后将适配器添加到您的AutoTextCompleteTextView。你需要从你的日志查询中获取_id,事实上,你可能只是考虑让整行( SELECT * FROM表
)。如果你没有一列_id,尝试 SELECT数据的rowid AS _id FROM表
。
I am working on a sqlite database project on android. And it's my first sqlite project. I read lots of article and created my app but there's a problem that I can't find a way to solve it. I hope you can show a way for me. The problem is when I call activity by clicking a button, the device fire up a message ("unfortunately app stopped").
I am trying to get strings from sqlite database to autocompletetextview.
Sqlite databasehelper class's codes
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);
}
}
Activity Class's codes
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")));
};
}
}
解决方案
Use a SimpleCursorAdapter, as shown in 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);
From here, you just need to add a function to return a cursor with your database, and add the adapter to your AutoTextCompleteTextView. You will need to get _id from your logbook query, in fact, you might just consider getting the entire row (SELECT * FROM table
). If you don't have a column _id, try SELECT data, rowid AS _id FROM table
.