从SQLite通过搜索获取​​所有数据

时间:2014-09-04 15:15:31

标签: java android sqlite

我正在尝试使用SQLite LIKE语句从Query获取所有数据。但我不知道如何得到它。

我尝试修改我用来获取所有数据而不进行过滤的Query,并且其工作正常。 我已经尝试搜索这个问题,但我仍然没有得到它。

这是我的 Search.java

    Intent search = getIntent();
    String searchResult = search.getStringExtra("TAG_SEARCH");
    DatabaseHandler db = new DatabaseHandler(this);

    /**
     * CRUD Operations
     * */
    Log.d("Reading: ", "Reading all contacts.."); 
    List <AllItem> allItems = new ArrayList<AllItem>();

    allItems = db.getAllSearchResult();

    ArrayList <String> allItems2 = new ArrayList<String>();

    for (AllItem cn : allItems) {
        allItems2.add(cn.getItem_name());
        allItems2.add(cn.getAreaNAme());
        allItems2.add(cn.getPriceCategory());
        allItems2.add(cn.getImageCount());
        allItems2.add(cn.getID());
        allItems2.add(cn.getCategory_name());
        allItems2.add(cn.getKids());
        allItems2.add(cn.getFreeTextForKids());
        allItems2.add(cn.getDescription());
        allItems2.add(cn.getPromotion());
        allItems2.add(cn.getPromotionFreeText());
        allItems2.add(cn.getPromotionStartDate() + " - ");
        allItems2.add(cn.getPromotionEndDate());
        allItems2.add(cn.getYoutube());
        allItems2.add(cn.getYoutubeLink());
        allItems2.add(cn.getPhone());
        allItems2.add(cn.getMobilePhone());
        allItems2.add(cn.getReview());
        allItems2.add(cn.getAddress());
        allItems2.add(cn.getLatitude());
        allItems2.add(cn.getLongitude());
        allItems2.add(cn.getOpenDetail());
        allItems2.add(cn.getFacebook());
        allItems2.add(cn.getTwitter());
        allItems2.add(cn.getInstagram());
        allItems2.add(cn.getWebsite());
    }


    CustomAdapterAccommodation adapter = new CustomAdapterAccommodation(Search.this, allItems2);
    listview.setAdapter(adapter); 


}

我想使用Intent中的String来搜索DATABASE上的数据。

这是我的 DatabaseHandler.java

// Getting All search result
public List<AllItem> getAllSearchResult() {
    List<AllItem> allsearchResultList = new ArrayList<AllItem>();
    // Select All Query
    String selectQuery = "SELECT * FROM " + TABLE_ALLITEM;

    // If i try change like this its show nothing
    // String selectQuery = "SELECT * FROM " + TABLE_ALLITEM + " WHERE " + KEY_ITEM_NAME_ALLITEM + " = '%a%'";


    Log.d("Query search", selectQuery);
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            AllItem allitem = new AllItem();
            allitem.setTableID(cursor.getInt(0));
            allitem.setID(cursor.getString(1));
            allitem.setCategory_name(cursor.getString(2));
            allitem.setItem_Name(cursor.getString(3));

            allitem.setDescription(cursor.getString(4));
            allitem.setAddress(cursor.getString(5));
            allitem.setArea (cursor.getString(6));
            allitem.setAreaName (cursor.getString(7));

            allitem.setLongitude (cursor.getString(8));
            allitem.setLatitude (cursor.getString(9));

            allitem.setOpenDetail (cursor.getString(10));

            allitem.setKids (cursor.getString(11));
            allitem.setFreeTextForKids (cursor.getString(12));

            allitem.setCuisineID (cursor.getString(13));
            allitem.setCuisineName (cursor.getString(14));
            allitem.setCuisineUniqueCode (cursor.getString(15));

            allitem.setBarTypeID (cursor.getString(16));
            allitem.setBarTypeName (cursor.getString(17));
            allitem.setBarTypeCode (cursor.getString(18));

            allitem.setRoomTypeID (cursor.getString(19));
            allitem.setRoomTypeName (cursor.getString(20));
            allitem.setRoomTypeCode (cursor.getString(21));

            allitem.setWellnessTypeID (cursor.getString(22));
            allitem.setWellnessTypeName (cursor.getString(23));
            allitem.setWellnessTypeCode (cursor.getString(24));

            allitem.setAttractionsTypeID (cursor.getString(25));
            allitem.setAttractionsTypeName (cursor.getString(26));
            allitem.setAttractionsTypeCode (cursor.getString(27));

            allitem.setShopsTypeID (cursor.getString(28));
            allitem.setShopsTypeName (cursor.getString(29));
            allitem.setShopsTypeCode (cursor.getString(30));

            allitem.setAdventuresTypeID (cursor.getString(31));
            allitem.setAdventuresTypeName (cursor.getString(32));
            allitem.setAdventuresTypeCode (cursor.getString(33));

            allitem.setRomanceTypeID (cursor.getString(34));
            allitem.setRomanceTypeName (cursor.getString(35));
            allitem.setRomanceTypeCode (cursor.getString(36));

            allitem.setGalleriesTypeID (cursor.getString(37));
            allitem.setGalleriesTypeName (cursor.getString(38));
            allitem.setGalleriesTypeCode (cursor.getString(39));

            allitem.setFurnitureTypeID (cursor.getString(40));
            allitem.setFurnitureTypeName (cursor.getString(41));
            allitem.setFurnitureTypeCode (cursor.getString(42));

            allitem.setEventTypeID (cursor.getString(43));
            allitem.setEventTypeName (cursor.getString(44));
            allitem.setEventTypeCode (cursor.getString(45));

            allitem.setPriceCategory (cursor.getString(46));

            allitem.setHotelOfficialStarRating (cursor.getString(47));

            allitem.setPhone (cursor.getString(48));
            allitem.setMobilePhone (cursor.getString(49));

            allitem.setReview (cursor.getString(50));

            allitem.setPromotion (cursor.getString(51));
            allitem.setPromotionFreeText (cursor.getString(52));
            allitem.setPromotionStartDate (cursor.getString(53));
            allitem.setPromotionEndDate (cursor.getString(54));
            allitem.setActive (cursor.getString(55));

            allitem.setImageCount (cursor.getString(56));

            allitem.setCreatedBy (cursor.getString(57));
            allitem.setCreatedDate (cursor.getString(58));
            allitem.setUpdatedBy (cursor.getString(59));
            allitem.setUpdatedDate (cursor.getString(60));

            allitem.setFacebook (cursor.getString(61));
            allitem.setTwitter (cursor.getString(62));
            allitem.setInstagram (cursor.getString(63));
            allitem.setWebsite (cursor.getString(64));
            allitem.setYoutube (cursor.getString(65));
            allitem.setYoutubeLink (cursor.getString(66));

            allitem.setMember (cursor.getString(67));
            allitem.setMemberStartDate (cursor.getString(68));
            allitem.setMemberEndDate (cursor.getString(69));

            allitem.setNew (cursor.getString(70));
            allitem.setEventStartDate (cursor.getString(71));
            allitem.setEventEndDate (cursor.getString(72));


            // Adding food to list
            allsearchResultList.add(allitem);
        } while (cursor.moveToNext());
    }

    // return food list
    return allsearchResultList;
}

有没有人可以帮我解决这个问题? 当用户按下按钮搜索时,文本发送到Search类,文本是从SQLite获取所有数据的关键。然后在ListView中显示。

之前感谢:D

1 个答案:

答案 0 :(得分:0)

使用LIKE not =

LIKE允许您使用通配符:%表示多个字符_表示一个字符

String filter = "a";    
String selectQuery = "SELECT * FROM " + TABLE_ALLITEM + " WHERE " + KEY_ITEM_NAME_ALLITEM + " LIKE '%"+filter+"%' ";