我在我的项目中使用了SQLite数据库,并且在我的主要活动(BancoDictionary
)上有一个ListView。此ListView应显示KEY_WORD
(列)中的所有单词,而不键入任何内容(如词典)。我在我的数据库活动(DictionaryDatabase
)中创建了一个数组列表来获取所有单词。我的应用程序运行正常,但没有显示ListView中的任何项目。为什么?
代码如下:
DictionaryDatabase.java
public class DictionaryDatabase {
private static final String TAG = "DictionaryDatabase";
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 = "dictionary";
private static final String FTS_VIRTUAL_TABLE = "FTSdictionary";
private static final int DATABASE_VERSION = 2;
private final DictionaryOpenHelper mDatabaseOpenHelper;
private static final HashMap<String,String> mColumnMap = buildColumnMap();
public DictionaryDatabase(Context context) {
mDatabaseOpenHelper = new DictionaryOpenHelper(context);
}
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);
}
public Cursor getWordMatches(String query, String[] columns) {
String selection = KEY_WORD + " MATCH ?";
String[] selectionArgs = new String[] {query+"*"};
return query(selection, selectionArgs, columns);
}
private Cursor query(String selection, String[] selectionArgs, String[] columns) {
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;
}
private static class DictionaryOpenHelper extends SQLiteOpenHelper {
private final Context mHelperContext;
private SQLiteDatabase mDatabase;
private static final String FTS_TABLE_CREATE = "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + " USING fts3 (" + KEY_WORD + ", " + KEY_DEFINITION + ");";
DictionaryOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mHelperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
mDatabase = db;
mDatabase.execSQL(FTS_TABLE_CREATE);
loadDictionary();
}
private void loadDictionary() {
new Thread(new Runnable() {
public void run() {
try {
loadWords();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void loadWords() throws IOException {
Log.d(TAG, "Loading words...");
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "//");
if (strings.length < 2)
continue;
long id = addWord(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
Log.d(TAG, "DONE loading words.");
}
public long addWord(String word, String definition) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_WORD, word);
initialValues.put(KEY_DEFINITION, definition);
return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
onCreate(db);
}
}
我认为问题从这里开始。
public ArrayList<WordDefinition> getAllWords() {
ArrayList<WordDefinition> arrayList=new ArrayList<WordDefinition>();
SQLiteDatabase database=mDatabaseOpenHelper.getReadableDatabase();
String selectAllQueryString="SELECT * FROM "+FTS_VIRTUAL_TABLE;
Cursor cursor=database.rawQuery(selectAllQueryString, null);
if (cursor.moveToFirst()) {
do {
WordDefinition wordDefinition=new WordDefinition(cursor.getString(cursor.getColumnIndex(KEY_WORD)), cursor.getString(cursor.getColumnIndex(KEY_DEFINITION)));
arrayList.add(wordDefinition);
} while (cursor.moveToNext());
}
return arrayList;
}
public WordDefinition getWordDefinition(String word) {
SQLiteDatabase database=mDatabaseOpenHelper.getReadableDatabase();
WordDefinition wordDefinition=null;
String selectQueryString="SELECT * FROM "+FTS_VIRTUAL_TABLE+ " WHERE "+KEY_WORD+" = '"+word+ "'";
Cursor cursor=database.rawQuery(selectQueryString, null);
if (cursor.moveToFirst()) {
wordDefinition=new WordDefinition(cursor.getString(cursor.getColumnIndex(KEY_WORD)), cursor.getString(cursor.getColumnIndex(KEY_DEFINITION)));
}
return wordDefinition;
}
public WordDefinition getWordDefinition(long id) {
SQLiteDatabase database=mDatabaseOpenHelper.getReadableDatabase();
WordDefinition wordDefinition=null;
String selectQueryString="SELECT * FROM "+FTS_VIRTUAL_TABLE+ " WHERE "+BaseColumns._ID+" = '"+id+ "'";
Cursor cursor=database.rawQuery(selectQueryString, null);
if (cursor.moveToFirst()) {
wordDefinition=new WordDefinition(cursor.getString(cursor.getColumnIndex(KEY_WORD)), cursor.getString(cursor.getColumnIndex(KEY_DEFINITION)));
}
return wordDefinition;
}
public void initializeDatabaseFortheFirstTime(ArrayList<WordDefinition> wordDefinitions) {
SQLiteDatabase database=mDatabaseOpenHelper.getWritableDatabase();
database.execSQL("BEGIN");
ContentValues contentValues=new ContentValues();
for (WordDefinition wordDefinition : wordDefinitions) {
contentValues.put(KEY_WORD, wordDefinition.KEY_WORD);
contentValues.put(KEY_DEFINITION, wordDefinition.KEY_DEFINITION);
database.insert(FTS_VIRTUAL_TABLE, null, contentValues);
}
database.execSQL("COMMIT");
}
}
WordDefinition.java
public class WordDefinition {
String KEY_WORD,KEY_DEFINITION;
public WordDefinition(String KEY_WORD,ArrayList<String> alldefinition) {
this.KEY_WORD=KEY_WORD;
StringBuilder stringBuilder=new StringBuilder();
for (String string : alldefinition) {
stringBuilder.append(string);
}
this.KEY_WORD=stringBuilder.toString();
}
public WordDefinition(String KEY_WORD,String alldefinition) {
this.KEY_WORD=KEY_WORD;
this.KEY_DEFINITION=alldefinition;
}
}
BancoDictionary.java
public class BancoDictionary extends Activity {
private TextView mTextView;
private ListView mListView;
ListView dictionaryListView;
String logTagString="DICTIONARY";
ArrayList<WordDefinition> allWordDefinitions=new ArrayList<WordDefinition>();
DictionaryDatabase DictionaryDatabase;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
Log.d("DICTIONARY", "second activity started");
dictionaryListView=(ListView) findViewById(R.id.dictionaryListView);
mTextView = (TextView) findViewById(R.id.text);
mListView = (ListView) findViewById(R.id.list);
DictionaryDatabase=new DictionaryDatabase(this);
allWordDefinitions=DictionaryDatabase.getAllWords();
dictionaryListView.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View view, ViewGroup arg2) {
if (view==null) {
view=getLayoutInflater().inflate(R.layout.list_item, null);
}
TextView textView=(TextView) view.findViewById(R.id.listItemTextView);
textView.setText(allWordDefinitions.get(position).KEY_WORD);
return view;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return allWordDefinitions.size();
}
});
dictionaryListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
Intent intent =new Intent(BancoDictionary.this, WordActivity.class);
intent.putExtra("KEY_WORD", allWordDefinitions.get(position).KEY_WORD);
intent.putExtra("KEY_DEFINITION", allWordDefinitions.get(position).KEY_DEFINITION);
startActivity(intent);
}
});
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Intent wordIntent = new Intent(this, WordActivity.class);
this.finish();
wordIntent.setData(intent.getData());
startActivity(wordIntent);
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null, new String[] {query}, null);
if (cursor == null) {
mTextView.setText(getString(R.string.no_results, new Object[] {query}));
} else {
int count = cursor.getCount();
String countString = getResources().getQuantityString(R.plurals.search_results, count, new Object[] {count, query});
mTextView.setText(countString);
String[] from = new String[] { DictionaryDatabase.KEY_WORD, DictionaryDatabase.KEY_DEFINITION };
int[] to = new int[] { R.id.word, R.id.definition };
SimpleCursorAdapter words = new SimpleCursorAdapter(this,
R.layout.result, cursor, from, to);
mListView.setAdapter(words);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI, String.valueOf(id));
wordIntent.setData(data);
startActivity(wordIntent);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
答案 0 :(得分:1)
在inflate调用中,您将父级设置为null,这不是正确的用法。使用3参数inflate,并将attachToRoot参数设置为false:getLayoutInflater().inflate(R.layout.list_item, arg2, false);
这个网站有一个很好的解释,为什么这是必要的:http://www.possiblemobile.com/2013/05/layout-inflation-as-intended