2013-08-27 127 views
1

我有CursorAdapter來顯示數據庫中的數據。現在,我想將搜索功能添加到自定義列表視圖。所以,我嘗試了這一點。但是,這不起作用。Android自定義ListView中的搜索功能

searchOption.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence cs, int start, int before, int count) { 
       // TODO Auto-generated method stub 

       AbstractActivity.this.cursorAdapter.getFilter().filter(cs); 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 

      } 
     }); 

這裏是適配器代碼

public class AbstractCursorAdapter extends CursorAdapter { 
    Cursor cursorOne; 

    String getName; 

    @SuppressWarnings("deprecation") 
    public AbstractCursorAdapter(Context context, Cursor c) { 
     super(context, c); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     // TODO Auto-generated method stub 
     TextView title = (TextView)view.findViewById(R.id.abTitle); 

     title.setText(cursor.getString(cursor.getColumnIndexOrThrow("TITLE"))); 
     ; 

     TextView topic = (TextView)view.findViewById(R.id.abTopic); 

     topic.setText(cursor.getString(cursor.getColumnIndexOrThrow("TOPIC"))); 

     TextView type = (TextView)view.findViewById(R.id.abType); 

     type.setText(cursor.getString(cursor.getColumnIndexOrThrow("TYPE"))); 

     String value = cursor.getString(cursor.getColumnIndexOrThrow("_id")); 

     String sqlQuery = "select abstracts_item._id AS ID,abstract_author.NAME AS NAME from abstracts_item,abstract_author,authors_abstract where abstracts_item._id = authors_abstract.abstractsitem_id and abstract_author._id = authors_abstract.abstractauthor_id and ID = " 
       + value; 


     cursorOne = DatabaseHelper.database.rawQuery(sqlQuery, null); 

     if (cursorOne != null) { 
      cursorOne.moveToFirst(); 
      do { 

       if (cursorOne.getPosition() == 0) { 

        getName = cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME")); 

       } else { 

        getName = getName + "," 
          + cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME")); 

       } 

      } while (cursorOne.moveToNext()); 
     } 

     TextView authorNames = (TextView)view.findViewById(R.id.SubTitle); 

     String formatterNames = ""; 

     String[] namesArray = getName.split(","); 

     // Log.e("Length", String.valueOf(namesArray.length)); 

     if (namesArray.length > 1) { 
      for (int i = 0; i < namesArray.length; i++) { 

       if (i == namesArray.length - 1) { 
        formatterNames = formatterNames + " & " + namesArray[i]; 
       }else if (i == 0){ 
        formatterNames = formatterNames +namesArray[i]; 
       } 
       else { 
        formatterNames = formatterNames + " , " +namesArray[i]; 
       } 
      } 


      /* 
      * Get Width 
      */ 

      WindowManager WinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); 
      int displayWidth = WinMgr.getDefaultDisplay().getWidth(); 

      Paint paint = new Paint(); 
      Rect bounds = new Rect(); 

      int text_height = 0; 
      int text_width = 0; 

      //paint.setTypeface(Typeface.DEFAULT); 

      paint.getTextBounds(formatterNames, 0, formatterNames.length(), bounds); 

      text_height = bounds.height(); 
      text_width = bounds.width(); 


      Log.e("SIZE", "Text =" + String.valueOf(text_width) + "------" +"Layout = " + String.valueOf(displayWidth)); 

      if(text_width > displayWidth){ 

       String output= formatterNames.split(",")[0] + " et al. " ; 
       authorNames.setText(output); 

      }else{ 
       authorNames.setText(formatterNames.replaceAll("((?:^|[^A-Z.])[A-Z])[a-z]*\\s(?=[A-Z])", 
         "$1.")); 
      } 


     } else { 

      authorNames 
      .setText(getName.replaceAll("((?:^|[^A-Z.])[A-Z])[a-z]*\\s(?=[A-Z])", "$1.")); 
     } 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup viewgroup) { 
     // TODO Auto-generated method stub 
     LayoutInflater inflater = LayoutInflater.from(viewgroup.getContext()); 
     View returnView = inflater.inflate(R.layout.abstract_content, viewgroup, false); 
     return returnView; 
    } 



} 

我會很高興,如果你們幫助找出什麼在我的代碼的問題,或者我如何添加搜索功能,自定義列表視圖。

+0

你有沒有使用任何自定義適配器 –

+0

@MohammodHossain是。 – user2579475

+0

發佈您的自定義適配器代碼部分 –

回答

3

嘗試使用它

adapter.setFilterQueryProvider(new FilterQueryProvider() { 
    public Cursor runQuery(CharSequence constraint) { 
     // fetch all query data by input String 
     // fetchDataByName take input String when search 
     return dbHelper.fetchDataByName(constraint.toString()); 
    } 
}); 

public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
    AbstractActivity.this.cursorAdapter.getFilter().filter(cs);  
} 
+0

感謝您的幫助。 – user2579475

0

在beforeTextChanged()方法中創建一個新的cursorAdapter適配器對象。

new SimpleCursorAdapter (this, Android.Resource.Layout.SimpleListItem1, cursor, 
     fromColumns, 
     toControlIDs); 
+0

這不是個好主意。您需要過濾填充列表的基礎數據並在適配器上調用「notifyDataSetChanged」 – Raghunandan