2017-04-10 78 views
-2

我正在從sqllite數據庫填充字符串ArrayList。用戶鍵入他想要查找的值,並將搜索結果顯示在列表視圖中給用戶。現在,當我輸入數據庫中的少量條目時,它始終是所有數據。我想用java來做這個搜索而不是Sql查詢,我的代碼似乎在某種程度上工作,但它並沒有真正做我想做的事情。我在這裏做錯了什麼?從sqllite填充ArrayList,並檢查它是否包含

public void searchEventDetails(){ 

    final ArrayList<String> dbEntries = new ArrayList<>(); 
    final ArrayList<String> entryId = new ArrayList<>(); 

    Cursor cursor = eventDB.getAllDataSearch(); 

    if (cursor.moveToFirst()){ 
     do{ 

       dbEntries.add("Title: "+ cursor.getString(1)+"\n" + "Description: "+cursor.getString(2)); 

       entryId.add(cursor.getString(0)); 

     }while(cursor.moveToNext()); 
    } 
    cursor.close(); 

     if (dbEntries.contains(txtSearch.getText())){ 
      System.out.print("Value"); 
     } 
     ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dbEntries); 

     ListView listView = (ListView) findViewById(R.id.listViewFromDBSearch); 

     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       String itemSelected = "You Selected " + String.valueOf(parent.getItemAtPosition(position)); 
       Toast.makeText(SearchEvent.this, itemSelected, Toast.LENGTH_SHORT).show(); 
       Intent showSearchEvents = new Intent(getApplicationContext(),ShowSearchEvents.class); 
       search = String.valueOf(parent.getItemAtPosition(position)); 

       for (int i=0;i<dbEntries.size();i++){ 
        if (search.equals(dbEntries.get(i))){ 

         SearchEvent.this.id = entryId.get(i); 
        } 

       } 
       showSearchEvents.putExtra("id", SearchEvent.this.id); 
       startActivity(showSearchEvents); 
      } 
     }); 
} 

回答

0

你必須做這樣的

public void searchEventDetails(String search){ 

    final ArrayList<String> dbEntries = new ArrayList<>(); 
    final ArrayList<String> entryId = new ArrayList<>(); 

    Cursor cursor = eventDB.getAllDataSearch(search); 

    if (cursor.moveToFirst()){ 
     do{ 

       dbEntries.add("Title: "+ cursor.getString(1)+"\n" + "Description: "+cursor.getString(2)); 

       entryId.add(cursor.getString(0)); 

     }while(cursor.moveToNext()); 
    } 
    cursor.close(); 


     ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dbEntries); 

     ListView listView = (ListView) findViewById(R.id.listViewFromDBSearch); 

     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       String itemSelected = "You Selected " + String.valueOf(parent.getItemAtPosition(position)); 
       Toast.makeText(SearchEvent.this, itemSelected, Toast.LENGTH_SHORT).show(); 
       Intent showSearchEvents = new Intent(getApplicationContext(),ShowSearchEvents.class); 
       search = String.valueOf(parent.getItemAtPosition(position)); 

       for (int i=0;i<dbEntries.size();i++){ 
        if (search.equals(dbEntries.get(i))){ 

         SearchEvent.this.id = entryId.get(i); 
        } 

       } 
       showSearchEvents.putExtra("id", SearchEvent.this.id); 
       startActivity(showSearchEvents); 
      } 
     }); 
} 

然後在你的方法getAllDataSearch()添加字符串參數喜歡這一點,並使用像您的查詢

public cursor getAllDataSearch(String search){ 

String query = "SELECT * FROM your_table WHERE your column LIKE '%" + search + "%'" 

//your stuff here 
} 

不要忘記使用textwatcher在你的editetxt裏

txtSearch.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // Call back the Adapter with current character to Filter 

      searchEventDetails(txtSearch.getText().toString()); 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count,int after) { 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
     } 
    }); 

希望這個答案有幫助

+0

btw傳遞你的listView.setOnItemClickListener在onCreate – Irabzz