无法获得搜索功能以列出搜索结果

时间:2016-01-16 10:10:16

标签: android

这是我的数据库,我将所有信息存储在上面    名单。

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 = "imagedb";
private static final String TAG = "DataBaseHandler";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_PRICE = "price";
public static final String KEY_IMAGE = "image";
public static final String KEY_SEARCH = "searchData";

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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"+  
KEY_PRICE + " TEXT," + KEY_SEARCH + " TEXT,"
            +" UNIQUE (" + KEY_NAME + "));" + KEY_IMAGE + " BLOB" + ")";
    Log.w(TAG, CREATE_CONTACTS_TABLE);
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@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");
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

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

public// Adding new contact
long addContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    String searchValue = contact._name + " ";
    values.put(KEY_NAME, contact._name); // Contact Name
    values.put(KEY_PRICE, contact._price); // Contact Name
    values.put(KEY_IMAGE, contact._image); // Contact Phone
    values.put(KEY_SEARCH, searchValue); // Contact Phone

    // Inserting Row
    return db.insert(TABLE_CONTACTS, null, values);

}



// Getting All Contacts
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM contacts ORDER BY name";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(2));
            contact.setPrice(cursor.getString(1));
            contact.setImage(cursor.getBlob(4));
            contact.setSearch(cursor.getString(3));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }
    // close inserting data from database
    db.close();
    // return contact list
    return contactList;

}


public Cursor searchCustomer(String inputText) throws SQLException {
    Log.w(TAG, inputText);
    String query = " SELECT id as _id," + KEY_NAME + "," + KEY_PRICE +
            " from " + TABLE_CONTACTS +
            " where " +  KEY_SEARCH + " LIKE '" + inputText + "';";
    Log.w(TAG, query);
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor mCursor = db.rawQuery(query,null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

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

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

这是我的搜索功能,当我搜索单词时,列表视图    应该显示与单词匹配的搜索结果。但是,清单    视图无法显示搜索结果。是因为数据没有得到    从数据库?

public class Search extends Activity implements   
SearchView.OnQueryTextListener,
    SearchView.OnCloseListener{
private ListView mListView;
private SearchView searchView;
DataBaseHandler db;

private TextView nameText;
private TextView priceText;
private SimpleAdapter customers;

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

    searchView = (SearchView) findViewById(R.id.searchView);
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextListener(this);
    searchView.setOnCloseListener(this);

    mListView = (ListView) findViewById(R.id.list1);

    db = new DataBaseHandler(this);

}

public boolean onQueryTextChange(String newText) {
    showResults(newText + "*");
    return false;
}

public boolean onQueryTextSubmit(String query) {
    showResults(query + "*");
    return false;
}

public boolean onClose() {
    showResults("");
    return false;
}

private void showResults(String query) {

    Cursor cursor = db.searchCustomer((query != null ? query.toString() : 
"@@@@"));

    if (cursor == null) {
        //
    } else {
        // Specify the columns we want to display in the result
        String[] from = new String[] {
                DataBaseHandler.KEY_NAME,
                DataBaseHandler.KEY_PRICE};

        // Specify the Corresponding layout elements where we want the 
  columns to go
        int[] to = new int[] {     R.id.txtTitle,
                R.id.txtPrice};
        System.out.println("hello1");
        // Create a simple cursor adapter for the definitions and apply them 
  to the ListView
        SimpleCursorAdapter customers = new 
  SimpleCursorAdapter(this,R.layout.result, cursor, from, to);
        mListView.setAdapter(customers);
        System.out.println("hell1");
        // Define the on-click listener for the list items
        mListView.setOnItemClickListener(new 
  AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int 
  position, long id) {
                // Get the cursor, positioned to the corresponding row in
  the result set
                Cursor cursor = (Cursor) 
  mListView.getItemAtPosition(position);
                System.out.println("hel1");
                // Get the state's capital from this row in the database.
                String name = 
  cursor.getString(cursor.getColumnIndexOrThrow("name"));
                String price = 
  cursor.getString(cursor.getColumnIndexOrThrow("price"));
                System.out.println("hello2");

                //Get References to the TextViews
                nameText = (TextView) findViewById(R.id.txtTitles);
                priceText = (TextView) findViewById(R.id.txtPrices);
                System.out.println("hello3");
                // Update the parent class's TextView
                nameText.setText(name);
                priceText.setText(price);
                System.out.println("hello4");
                searchView.setQuery("", true);
            }
        });

    }
   }

  }

0 个答案:

没有答案