2012-12-07 55 views
1

我正在創建字典英語 - 越南語應用程序。我想將搜索功能添加到我的listView。我想用Word(英文)搜索。但我不知道如何放置數據庫才能顯示列表視圖,所以我放了一個數組來顯示。有沒有人幫助我如何使數據庫顯示搜索功能ListView?如何使數據庫顯示搜索功能ListView

它的數據庫處理器類

public class DatabaseHandler extends SQLiteOpenHelper { 

// All Static variables 
// Database Version 
private static final int DATABASE_VERSION = 1; 

// Database Name 
private static final String DATABASE_NAME = "dictionaryManager"; 

// Contacts table name 
private static final String TABLE_DICTIONARY = "Dictionary"; 

// Contacts Table Columns names 
private static final String KEY_ID = "id"; 
private static final String KEY_WORD = "word"; 
private static final String KEY_DATA = "data"; 


public DatabaseHandler(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_DICTIONARY_TABLE = "CREATE TABLE " + TABLE_DICTIONARY + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," + KEY_WORD + " TEXT," 
      + KEY_DATA + " TEXT" + ")"; 
    db.execSQL(CREATE_DICTIONARY_TABLE); 
} 

// Upgrading database 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // Drop older table if existed 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DICTIONARY); 

    // Create tables again 
    onCreate(db); 
} 

/** 
* All CRUD(Create, Read, Update, Delete) Operations 
*/ 

// Adding new 
void addDictionary(Dictionary dictionary) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_WORD, dictionary.getWord()); // Word: English 
    values.put(KEY_DATA, dictionary.getData()); // Data: Vietnam 

    // Inserting Row 
    db.insert(TABLE_DICTIONARY, null, values); 
    db.close(); // Closing database connection 
} 

// Getting single 
Dictionary getDictionary(int id) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query(TABLE_DICTIONARY, new String[] { KEY_ID, 
      KEY_WORD, KEY_DATA }, KEY_ID + "=?", 
      new String[] { String.valueOf(id) }, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 

    Dictionary dictionary = new Dictionary(Integer.parseInt(cursor.getString(0)), 
      cursor.getString(1), cursor.getString(2)); 
    // return 
    return dictionary; 
} 

// Getting All Dictionary 
public List<Dictionary> getAllDictionary() { 
    List<Dictionary> dictionaryList = new ArrayList<Dictionary>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_DICTIONARY; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Dictionary dictionary = new Dictionary(); 
      dictionary.setID(Integer.parseInt(cursor.getString(0))); 
      dictionary.setWord(cursor.getString(1)); 
      dictionary.setData(cursor.getString(2)); 
      // Adding contact to list 
      dictionaryList.add(dictionary); 
     } while (cursor.moveToNext()); 
    } 

    // return list 
    return dictionaryList; 
} 

// Updating single 
public int updateDictionary(Dictionary dictionary) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_WORD, dictionary.getWord()); 
    values.put(KEY_DATA, dictionary.getData()); 

    // updating row 
    return db.update(TABLE_DICTIONARY, values, KEY_ID + " = ?", 
      new String[] { String.valueOf(dictionary.getID()) }); 
} 

// Deleting single 
public void deleteDictionary(Dictionary dictionary) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_DICTIONARY, KEY_ID + " = ?", 
      new String[] { String.valueOf(dictionary.getID()) }); 
    db.close(); 
} 

// Getting Count 
public int getDictionaryCount() { 
    String countQuery = "SELECT * FROM " + TABLE_DICTIONARY; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(countQuery, null); 
    cursor.close(); 

    // return count 
    return cursor.getCount(); 
} 

}

SQLiteActivity類

public class SQLiteActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    DatabaseHandler db = new DatabaseHandler(this); 

    /** 
    * CRUD Operations 
    * */ 
    // Inserting 
    Log.d("Insert: ", "Inserting .."); 
    db.addDictionary(new Dictionary("Apple", "Trai tao")); 
    db.addDictionary(new Dictionary("Bear", "Con gau")); 

    // Reading all dictionary 
    Log.d("Reading: ", "Reading all dictionary.."); 
    List<Dictionary> dictionary = db.getAllDictionary();  

    for (Dictionary dt : dictionary) { 
     String log = "Id: "+dt.getID()+" ,Word: " + dt.getWord() + " ,Data: " + dt.getData(); 
      // Writing Dictionary to log 
    Log.d("Word: ", log); 

    //OnClick to change Activity 
    Button btn = (Button)findViewById(R.id.btntim); 
    btn.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(SQLiteActivity.this, LookupActivity.class); 
      startActivity(i); 

     } 
    }); 
} 
} 

}

字典類:

public class Dictionary { 
//private variables 
int _id; 
String _word; 
String _data; 

// Empty constructor 
public Dictionary(){ 

} 
// constructor 
public Dictionary(int id, String word, String data){ 
    this._id = id; 
    this._word = word; 
    this._data = data; 
} 

// constructor 
public Dictionary(String word, String data){ 
    this._word = word; 
    this._data = data; 
} 
// getting ID 
public int getID(){ 
    return this._id; 
} 

// setting id 
public void setID(int id){ 
    this._id = id; 
} 

// getting word 
public String getWord(){ 
    return this._word; 
} 

// setting word 
public void setWord(String word){ 
    this._word = word; 
} 

// getting data 
public String getData(){ 
    return this._data; 
} 

// setting data 
public void setData(String data){ 
    this._data = data; 
} 

}

最後,這是LookupActivity。在下面的代碼中,我將所有列表數據存儲在名爲words []的數組中,並使用簡單的ArrayAdapter附加到listview。因此,如何使數據庫顯示搜索功能的ListView:

package com.dictionary.tudienandroid; 

import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 

public class LookupActivity extends Activity { 

// List view 
private ListView lv; 

// Listview Adapter 
ArrayAdapter<String> adapter; 

// Search EditText 
EditText inputSearch; 

// ArrayList for Listview 
ArrayList<HashMap<String, String>> productList; 
DatabaseHandler mDB; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.maintim); 

    // Listview Data 
    String words[] ={"Kungfu","Long"}; 

    lv = (ListView) findViewById(R.id.list_view); 
    inputSearch = (EditText) findViewById(R.id.inputSearch); 

    // Adding items to listview 
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.word, words); 
    lv.setAdapter(adapter); 

    /** 
    * Enabling Search Filter 
    * */ 
    inputSearch.addTextChangedListener(new TextWatcher() { 

     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      // When user changed the Text 
      LookupActivity.this.adapter.getFilter().filter(cs); 
     } 

     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 

     } 

     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
     } 
    }); 
} 

}

回答

0

嘗試:

lv = (ListView) findViewById(R.id.listDB); 
    List<Dictionary> listdb = new ArrayList<Dictionary>(); 
    dbHelper = new DbHelper(getBaseContext()); 
    listdb = dbHelper.selectdictionary(); //create method in dbhelper 
    CourseAdapter adapter = new CourseAdapter(getBaseContext(), listLessons); 
    lv.setAdapter(adapter); 

創建適配器

public class CourseAdapter extends BaseAdapter { 
     /create adapter here to set string to listview 

}