我正在尝试从SQLite数据库中获取所有行并将它们显示在ListView中。而且我无法使用所有值返回列表。数据库已预先创建,现在位于/ raw文件夹中。
表(FTSgesla_content):
ID Value1 Value3
1 Some text1 Some text1
2 Some text2 Some text2
这是我的代码:
public class GeslaPodatkovnaBaza
{
private static final String TAG = "GeslaPodatkovnaBaza";
public static final String KEY_WORD = SearchManager.SUGGEST_COLUMN_TEXT_1;
public static final String KEY_DEFINITION = SearchManager.SUGGEST_COLUMN_TEXT_2;
private static final String DATABASE_NAME = "gesla";
private static final String FTS_VIRTUAL_TABLE = "FTSgesla";
private static final int DATABASE_VERSION = 1;
private final DictionaryOpenHelper mDatabaseOpenHelper;
private static final HashMap<String,String> mColumnMap = buildColumnMap();
/**
* Constructor
* @param context The Context within which to work, used to create the DB
*/
public GeslaPodatkovnaBaza(Context context) {
mDatabaseOpenHelper = new DictionaryOpenHelper(context);
}
public GeslaPodatkovnaBaza createDatabase() throws SQLException
{
try
{
mDatabaseOpenHelper.createDataBase();
}
catch (IOException mIOException)
{
Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}
public GeslaPodatkovnaBaza open() throws SQLException
{
try
{
mDatabaseOpenHelper.openDataBase();
mDatabaseOpenHelper.close();
mDatabaseOpenHelper.getReadableDatabase();
}
catch (SQLException mSQLException)
{
Log.e(TAG, "open >>"+ mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close()
{
mDatabaseOpenHelper.close();
}
public ArrayList<HashMap<String,String>> getAllWords()
{
return mDatabaseOpenHelper.getAll();
}
private static HashMap<String,String> buildColumnMap()
{
HashMap<String,String> map = new HashMap<String,String>();
map.put(KEY_WORD, KEY_WORD);
map.put(KEY_DEFINITION, KEY_DEFINITION);
map.put(BaseColumns._ID, "rowid AS " +
BaseColumns._ID);
map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " +
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " +
SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
return map;
}
public Cursor getWord(String rowId, String[] columns)
{
String selection = "rowid = ?";
String[] selectionArgs = new String[] {rowId};
return query(selection, selectionArgs, columns);
/* This builds a query that looks like:
* SELECT <columns> FROM <table> WHERE rowid = <rowId>
*/
}
public Cursor getWordMatches(String query, String[] columns)
{
//String selection = KEY_WORD + " MATCH ?";
String selection = FTS_VIRTUAL_TABLE + " MATCH ?"; //išče tudi po opisih
String[] selectionArgs = new String[] {query+"*"};
return query(selection, selectionArgs, columns);
}
private Cursor query(String selection, String[] selectionArgs, String[] columns) {
/* The SQLiteBuilder provides a map for all possible columns requested to
* actual columns in the database, creating a simple column alias mechanism
* by which the ContentProvider does not need to know the real column names
*/
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(FTS_VIRTUAL_TABLE);
builder.setProjectionMap(mColumnMap);
Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
columns, selection, selectionArgs, null, null, null);
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
public static class DictionaryOpenHelper extends SQLiteOpenHelper
{
private final Context mHelperContext;
private SQLiteDatabase mDatabase;
private static String DB_PATH="/data/data/com.example.enigmar/databases/";
private static String DB_NAME="gesla";
DictionaryOpenHelper(Context context)
{
super(context, DB_NAME, null, 1);
this.mHelperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db)
{
}
//THIS IS THE FUNCTION
public ArrayList<HashMap<String,String>> getAll()
{
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String, String> item;// = new HashMap<String, String>();
String stavekSQL = "SELECT c0suggest_text_1, c1suggest_text_2 FROM FTSgesla_content ORDER BY c0suggest_text_1 ASC";
//String selectQuery = "SELECT * FROM " + DATABASE_NAME;
Cursor cursor = mDatabase.rawQuery(stavekSQL, null);
if(cursor.moveToFirst())
{
do
{
item = new HashMap<String, String>();
item.put("line1", cursor.getString(0));
Log.e("ListIzBaze", "dodalBesedo1");
item.put("line2", cursor.getString(1));
Log.e("ListIzBaze", "dodalBesedo2");
list.add(item);
}
while(cursor.moveToNext());
}
Log.e("ListIzBaze", "VracamList");
return list;
//return mDatabase.execSQL(stavekSQL);
}
public void createDataBase() throws IOException
{
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException
{
//InputStream mInput = mHelperContext.getAssets().open(DB_NAME);
final Resources resources = mHelperContext.getResources();
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
InputStream mInput = resources.openRawResource(R.raw.gesla);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
mDatabase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return mDatabase != null;
}
@Override
public synchronized void close() {
if(mDatabase != null)
mDatabase.close();
super.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
答案 0 :(得分:0)
使用ANDROID进行LISTVIEW :
•首先,我们必须在列表视图中存储所有这些值。为此,我们使用List函数。
•然后,我们必须在一个List数组适配器中设置这些列表值。
•您应该将活动扩展到列表活动中。
•确保在main.xml中有一个带有id @android:id / list的ListView元素。
<强>功能:强>
List<String[].
•setListAdapter。
Cursor c=db.rawQuery("select * from sv_tb_fieldnotes where sv_fieldnote_created_by_fk='" + get_userid + "'",null);
List<String[]> list = new ArrayList<String[]>();
c.moveToFirst();
while(!c.isAfterLast())
{
title=new String[]{c.getString(3),c.getString(0)};
list.add(title);
c.moveToNext();
}
c.close();
db.close();
fetch_fieldnote_title=new String[list.size()];
fetch_fieldnote_id=new String[list.size()];
int x=0;
String assigning_fetch_fieldnote_title;
String assigning_fetch_fieldnote_id;
for (String[] name : list) {
assigning_fetch_fieldnote_title =name[0];
assigning_fetch_fieldnote_id=name[1];
fetch_fieldnote_title[x]=assigning_fetch_fieldnote_title;
fetch_fieldnote_id[x]=assigning_fetch_fieldnote_id;
x++;
}
//To generate the list view.
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,fetch_fieldnote_title));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//Toast.makeText(this, stg1[position], 2000).show();
fetch_title=fetch_fieldnote_title[position];
fetch_id=fetch_fieldnote_id[position];
//To register the list view with context menu.
registerForContextMenu(getListView());
super.onListItemClick(l, v, position, id);
}