2012-06-06 44 views
-2

請我真的需要幫助的ListView我filter.because見過很多教程,但它並沒有work.and我真的厭倦了這 只是我需要的EdiText過濾SQLITE LISTVIEW FILTER使用EDITEXT

我的項目包含四類:

1- LocationActivity(1'-活性prencipale)

public class LocationActivity extends Activity implements LocationListener{ 

    public final static String LOCATION_ID = "location_id"; 

    private long location_id; 
    private EditText name; 
    private EditText comment; 

    private RatingBar ratevalue; 

    private EditText latitude; 
    private EditText longitude; 
    private TextView current_latitude; 
    private TextView current_longitude; 
    private TextView current_source; 
    private TextView current_accuracy; 
    private LocationDatabase db; 
    private BestLocationProxy best_location_proxy; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     db = new LocationDatabase(this); 
     best_location_proxy = new BestLocationProxy(this); 

     if (savedInstanceState != null) { 
      location_id = savedInstanceState.getLong(LOCATION_ID); 
     } 

     Intent intent = getIntent(); 
     location_id = intent.getLongExtra(LOCATION_ID, -1); 

     setContentView(R.layout.location); 
     name = (EditText) findViewById(R.id.name); 

     comment = (EditText) findViewById(R.id.comment); 
     ratevalue = (RatingBar) findViewById(R.id.ratingbar); 
     latitude = (EditText) findViewById(R.id.latitude); 
     longitude = (EditText) findViewById(R.id.longitude); 
     current_latitude = (TextView) findViewById(R.id.current_latitude); 
     current_longitude = (TextView) findViewById(R.id.current_longitude); 
     current_source = (TextView) findViewById(R.id.current_source); 
     current_accuracy = (TextView) findViewById(R.id.current_accuracy); 

     updateLocation(best_location_proxy.getLastKnownLocation()); 

     Button set_location = (Button) findViewById(R.id.set_location); 

     set_location.setOnClickListener(new OnClickListener(){ 

      public void onClick(View arg0) { 
       Location l = best_location_proxy.getLastKnownLocation(); 
       if (l == null) { 
        return; 
       } 
       latitude.setText(Double.toString(l.getLatitude())); 
       longitude.setText(Double.toString(l.getLongitude())); 
      } 
     }); 

     Button closeButton = (Button) findViewById(R.id.close_location_window); 
     closeButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       finish(); 
      } 
     }); 

     if (location_id != -1) { 
      Cursor c = db.getLocation(location_id); 
      if (c.getCount() != 1) { 
       finish(); 
       return; 
      } 
      c.moveToFirst(); 
      int name_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_NAME); 
      int comment_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_COMM); 
      int ratevalue_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_RATE); 
      int latitude_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_LATITUDE); 
      int longitude_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_LONGITUDE); 

      name.setText(c.getString(name_id)); 

      comment.setText(c.getString(comment_id)); 
      ratevalue.setRating(c.getLong(ratevalue_id)); 
      latitude.setText(Double.toString(c.getDouble(latitude_id))); 
      longitude.setText(Double.toString(c.getDouble(longitude_id))); 
      c.close(); 
     } 

    } 


    @Override 
    protected void onResume() { 
     super.onResume(); 
     best_location_proxy.requestLocationUpdates(100000, 0, this); 
    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     best_location_proxy.removeUpdates(this); 

     String s_name = name.getText().toString(); 
     if (s_name.equals("")) { 
      return; 
     } 

     String s_comment = comment.getText().toString(); 
     if (s_comment.equals("")) { 
      return; 
     } 
     Double d_ratevalue = (double) ratevalue.getRating(); 

     Double d_latitude = null; 
     String s_latitude = latitude.getText().toString(); 
     if (!s_latitude.equals("")) { 
      d_latitude = Double.parseDouble(s_latitude); 
     } 

     Double d_longitude = null; 
     String s_longitude = longitude.getText().toString(); 
     if (!s_longitude.equals("")) { 
      d_longitude = Double.parseDouble(s_longitude); 
     } 

     if (location_id != -1) { 
      db.updateLocation(location_id, s_name, s_comment, d_ratevalue, d_latitude, d_longitude); 
     } else { 

      location_id = db.createLocation(s_name, s_comment, d_ratevalue, d_latitude, d_longitude); 
     } 
    } 


    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putLong(LOCATION_ID, location_id); 
    } 

    private void updateLocation(Location l){ 

     if (l == null){ 
      current_source.setText(R.string.no_provider); 
      current_latitude.setText(R.string.unavailable); 
      current_longitude.setText(R.string.unavailable); 
      current_accuracy.setText(R.string.unavailable); 
      return; 
     } 

     String source; 
     if (l.getProvider().equals(LocationManager.GPS_PROVIDER)){ 
      source = getString(R.string.location_map); 
     } else if (l.getProvider().equals(LocationManager.NETWORK_PROVIDER)){ 
      source = getString(R.string.cell); 
     } else { 
      source = getString(R.string.unknown); 
     } 

     current_source.setText(source); 
     current_latitude.setText(Double.toString(l.getLatitude())); 
     current_longitude.setText(Double.toString(l.getLongitude())); 
     current_accuracy.setText(Float.toString(l.getAccuracy())); 
    } 


    public void onLocationChanged(Location location) { 
     updateLocation(location); 
    } 


    public void onProviderDisabled(String provider) { 
    } 


    public void onProviderEnabled(String provider) {  
    } 


    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

} 

2- LocationListActivity(列表視圖活性)

public class LocationListActivity extends ListActivity { 

    private Cursor locations; 
    private LocationDatabase db; 

    private final static String RADAR_ACTION = "com.google.android.radar.SHOW_RADAR"; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     db = new LocationDatabase(this); 

     setContentView(R.layout.list); 
     registerForContextMenu(this.getListView()); 
     getListView().setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       Cursor c = (Cursor) parent.getAdapter().getItem(position); 

       int name_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_NAME); 
       int latitude_id = c 
         .getColumnIndex(LocationDatabase.FIELD_LOCATIONS_LATITUDE); 
       int longitude_id = c 
         .getColumnIndex(LocationDatabase.FIELD_LOCATIONS_LONGITUDE); 
       String name = c.getString(name_id); 
       float latitude = c.getFloat(latitude_id); 
       float longitude = c.getFloat(longitude_id); 

       startBest(latitude, longitude, name); 
      } 
     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 

     MenuItem add = menu.add(R.string.location_add); 
     add.setIcon(android.R.drawable.ic_menu_add); 
     add.setOnMenuItemClickListener(new OnMenuItemClickListener() { 

      public boolean onMenuItemClick(MenuItem item) { 
       Intent i = new Intent(); 
       i.setClass(LocationListActivity.this, LocationActivity.class); 
       startActivity(i); 
       return true; 
      } 
     }); 

     return true; 
    } 


    @Override 
    protected void onResume() { 
     super.onResume(); 
     updateList(); 
    } 


    @Override 
    protected void onStop() { 
     super.onStop(); 
     if (locations != null){ 
      locations.close(); 
     } 
    } 


    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; 
     Cursor c = (Cursor) getListView().getItemAtPosition(info.position); 

     int id_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_ID); 
     int name_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_NAME); 
     int latitude_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_LATITUDE); 
     int longitude_id = c.getColumnIndex(LocationDatabase.FIELD_LOCATIONS_LONGITUDE); 

     final long id = c.getLong(id_id); 
     final String name = c.getString(name_id); 
     final float latitude = c.getFloat(latitude_id); 
     final float longitude = c.getFloat(longitude_id); 

     menu.setHeaderTitle(name); 

     MenuItem map = menu.add(R.string.location_map); 
     map.setOnMenuItemClickListener(new OnMenuItemClickListener() { 

      public boolean onMenuItemClick(MenuItem item) { 
       startMap(latitude, longitude, name); 
       return true; 
      } 
     }); 

     MenuItem edit = menu.add(R.string.location_edit); 
     edit.setOnMenuItemClickListener(new OnMenuItemClickListener() { 

      public boolean onMenuItemClick(MenuItem item) { 
       Intent i = new Intent(); 
       i.setClass(LocationListActivity.this, LocationActivity.class); 
       i.putExtra(LocationActivity.LOCATION_ID, id); 
       startActivity(i); 
       return true; 
      } 
     }); 

     MenuItem delete = menu.add(R.string.location_delete); 
     delete.setOnMenuItemClickListener(new OnMenuItemClickListener() { 

      public boolean onMenuItemClick(MenuItem item) { 
       db.deleteLocation(id); 
       updateList(); 
       return true; 
      } 
     }); 
    } 

    private void updateList() { 

     if (locations != null) { 
      locations.close(); 
     } 

     locations = db.getAllLocations(); 
     ListAdapter adapter = new SimpleCursorAdapter(this, 
       android.R.layout.simple_list_item_1, locations, 
       new String[] { LocationDatabase.FIELD_LOCATIONS_NAME, }, 
       new int[] { android.R.id.text1 }); 
     setListAdapter(adapter); 
    } 

    private void startRadar(float latitude, float longitude){ 
     Intent i = new Intent(RADAR_ACTION); 
     i.putExtra("latitude", latitude); 
     i.putExtra("longitude", longitude); 
     startActivity(i); 
    } 

    private void startMap(float latitude, float longitude, String name){ 
     Formatter f = new Formatter(Locale.US); 
     f.format("geo:0,0?q=%1$.5f,%2$.5f(%3$s)", latitude, longitude, name); 
     Uri uri = Uri.parse(f.toString()); 
     Intent i = new Intent(Intent.ACTION_VIEW, uri); 
     startActivity(i); 
    } 

    private void startBest(float latitude, float longitude, String name){ 
     if (isRadarAvailable()){ 
      startRadar(latitude, longitude); 
     } else { 
      startMap(latitude, longitude, name); 
     } 
    } 

    private boolean isRadarAvailable(){ 
     return isIntentAvailable(RADAR_ACTION); 
    } 

    private boolean isIntentAvailable(String action) { 
     PackageManager packageManager = getPackageManager(); 
     final Intent intent = new Intent(action); 
     @SuppressWarnings("rawtypes") 
     List list = 
       packageManager.queryIntentActivities(intent, 
         PackageManager.MATCH_DEFAULT_ONLY); 
     return list.size() > 0; 
    } 
} 

3- LocationDataase(數據庫活動源碼)

public class LocationDatabase extends SQLiteOpenHelper { 

    public final static String TAG = LocationDatabase.class.toString(); 

    public final static String DB_NAME = "locations"; 
    public final static int DB_VERSION = 1; 

    public final static String TABLE_LOCATIONS = "locations"; 

    public final static String FIELD_LOCATIONS_ID = "_id"; 
    public final static String FIELD_LOCATIONS_NAME = "name"; 
    public final static String FIELD_LOCATIONS_COMM = "comment"; 
    public final static String FIELD_LOCATIONS_RATE = "ratevalue"; 
    public final static String FIELD_LOCATIONS_LATITUDE = "latitude"; 
    public final static String FIELD_LOCATIONS_LONGITUDE = "longitude"; 

    public final static String[] PROJECTION_LOCATIONS = { FIELD_LOCATIONS_ID, 
      FIELD_LOCATIONS_NAME, FIELD_LOCATIONS_COMM, FIELD_LOCATIONS_RATE, FIELD_LOCATIONS_LATITUDE, 
      FIELD_LOCATIONS_LONGITUDE }; 

    public LocationDatabase(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 
     getWritableDatabase(); // make upgrades work 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE " + TABLE_LOCATIONS + " (" + 
       FIELD_LOCATIONS_ID + " INTEGER PRIMARY KEY NOT NULL, " + 
       FIELD_LOCATIONS_NAME + " Text, " + 

       FIELD_LOCATIONS_COMM + " Text, " + 
       FIELD_LOCATIONS_RATE + " REAL, " + 
       FIELD_LOCATIONS_LATITUDE + " REAL, " 
       + FIELD_LOCATIONS_LONGITUDE + " REAL)"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
    } 

    public long createLocation(String name, String comment, Double ratevalue, Double latitude, Double longitude) { 
     Log.d(TAG, "Inserting location " + name + comment + ratevalue); 
     SQLiteDatabase db = getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(FIELD_LOCATIONS_NAME, name); 

     values.put(FIELD_LOCATIONS_COMM, comment); 
     values.put(FIELD_LOCATIONS_RATE, ratevalue); 
     values.put(FIELD_LOCATIONS_LATITUDE, latitude); 
     values.put(FIELD_LOCATIONS_LONGITUDE, longitude); 

     long id = db.insert(TABLE_LOCATIONS, null, values); 
     Log.d(TAG, Long.toString(id)); 
     db.close(); 
     return id; 
    } 

    public void updateLocation(Long location_id, String name, String comment, Double ratevalue, Double latitude, 
      Double longitude) { 
     Log.d(TAG, "Updating location"); 
     SQLiteDatabase db = getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(FIELD_LOCATIONS_NAME, name); 

     values.put(FIELD_LOCATIONS_COMM, comment); 
     values.put(FIELD_LOCATIONS_RATE, ratevalue); 
     values.put(FIELD_LOCATIONS_LATITUDE, latitude); 
     values.put(FIELD_LOCATIONS_LONGITUDE, longitude); 

     db.update(TABLE_LOCATIONS, values, "_id = ?", 
       new String[] { location_id.toString() }); 
     db.close(); 
    } 

    public Cursor getAllLocations() { 
     Log.d(TAG, "Selecting all locations"); 
     SQLiteDatabase db = getReadableDatabase(); 
     Cursor c = db.query(TABLE_LOCATIONS, PROJECTION_LOCATIONS, null, null, 
       null, null, FIELD_LOCATIONS_NAME); 
     Log.d(TAG, Integer.toString(c.getCount())); 
     db.close(); 
     return c; 
    } 

    public Cursor getLocation(long location_id) { 
     Log.d(TAG, "Selecting location " + Long.toString(location_id)); 
     SQLiteDatabase db = getReadableDatabase(); 
     Cursor c = db.query(TABLE_LOCATIONS, PROJECTION_LOCATIONS, "_id = ?", 
       new String[] { Long.toString(location_id) }, null, null, null); 
     Log.d(TAG, Integer.toString(c.getCount())); 
     db.close(); 
     return c; 
    } 

    public void deleteLocation(long location_id){ 
     Log.d(TAG, "Deleting location " + Long.toString(location_id)); 
     SQLiteDatabase db = getWritableDatabase(); 
     db.delete(TABLE_LOCATIONS, "_id = ?", new String[] {Long.toString(location_id)}); 
    } 
} 

4- BestLocationProxy

在佈局

我有location.xml和list.xml

1- location.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="311dp" 
     android:orientation="vertical" 
     android:paddingRight="20dip" > 

     <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

       <TextView 
        android:text="@string/name" 
        android:gravity="left|center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingRight="10dip" /> 
       <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:editable="true" 

        android:id="@+id/name" 
        android:singleLine="true" 
        android:layout_weight="1" /> 


           <TextView 
        android:text="@string/comment" 
        android:gravity="left|center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingRight="10dip" /> 
       <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:editable="true" 

        android:id="@+id/comment" 
        android:singleLine="true" 
        android:layout_weight="1" /> 

       <TextView 
        android:text="@string/Rate" 
        android:gravity="left|center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingRight="10dip" /> 
</TableLayout> 

       <RatingBar 
        android:id="@+id/ratingbar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:stepSize="1" 
        /> 

      <TableLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       > 
       <TextView 
        android:text="@string/latitude" 
        android:gravity="left|center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingRight="10dip" /> 
       <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:editable="true" 

        android:id="@+id/latitude" 
        android:singleLine="true" 
        android:layout_weight="1" 
        android:numeric="signed|decimal" /> 


       <TextView 
        android:text="@string/longitude" 
        android:gravity="left|center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingRight="10dip" /> 
       <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:editable="true" 

        android:id="@+id/longitude" 
        android:singleLine="true" 
        android:layout_weight="1" 
        android:numeric="signed|decimal" /> 

     </TableLayout> 
     <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
     <TableRow> 
     <TextView 
      android:text="@string/source" 
      android:gravity="right|center_vertical" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:paddingRight="10dip" /> 
     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/current_source" 
      android:layout_weight="1" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 
    </TableRow> 


      <TableRow> 
       <TextView 
        android:text="@string/latitude" 
        android:gravity="right|center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingRight="10dip" /> 
       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/current_latitude" 
        android:layout_weight="1" 
        android:textAppearance="?android:attr/textAppearanceSmall" /> 
      </TableRow> 
      <TableRow> 
       <TextView 
        android:text="@string/longitude" 
        android:gravity="right|center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingRight="10dip" /> 
       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/current_longitude" 
        android:layout_weight="1" 
        android:textAppearance="?android:attr/textAppearanceSmall" /> 
      </TableRow> 
      <TableRow> 
       <TextView 
        android:text="@string/accuracy" 
        android:gravity="right|center_vertical" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingRight="10dip" /> 
       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/current_accuracy" 
        android:layout_weight="1" 
        android:textAppearance="?android:attr/textAppearanceSmall" /> 
      </TableRow> 
     </TableLayout> 

     <View 
      android:layout_width="fill_parent" 
      android:background="@android:drawable/divider_horizontal_bright" 
      android:layout_marginBottom="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_height="4dip" /> 

     <Button 
      android:id="@+id/set_location" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/set_location" /> 

     <Button 
      android:id="@+id/close_location_window" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/close" /> 

    </LinearLayout> 
</ScrollView> 

2- list.xml

<?xml version="1.0" encoding="utf-8"?> 


<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:longClickable="true" 
     android:layout_weight="1" /> 
    <ScrollView 
     android:id="@android:id/empty" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true"> 
     <TextView 
      android:id="@+id/emptyText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/empty_msg" 
      android:textSize="20sp" 
      android:textColor="?android:attr/textColorSecondary" 
      android:paddingLeft="10dip" 
      android:paddingRight="10dip" 
      android:paddingTop="10dip" 
      android:lineSpacingMultiplier="0.92" /> 
    </ScrollView> 
</LinearLayout> 

對不起......太龍

+1

你的問題具體是什麼?此外,使用適當的大寫字母,標點符號和真實的文字不是簡短的做法是一種很好的做法;更多會員將閱讀並回復您的帖子。 – Sam

+0

我很抱歉兄弟 – Izabilla

+0

我的問題是在listview類中,我的列表正常工作的每件事都OK.i在頂部添加了過濾器按鈕。我剛剛錯過了配置列表視圖類 中的按鈕,如 setContentView(R.layout.filterable_listview); filterText =(EditText)findViewById(R.id.search_box); filterText.addTextChangedListener(filterTextWatcher); setListAdapter(新ArrayAdapter (這一點,android.R.layout.simple_list_item_1, getStringArrayList());} 我 有錯誤的最後一個指令// getStringArrayList() – Izabilla

回答

0

我想我明白你正在嘗試做的。下面是使用ListView的TextFilter一個例子:

public class Example extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     LinearLayout layout = new LinearLayout(this); 
     ListView list = new ListView(this); 
     layout.addView(list); 
     setContentView(layout); 

     String[] array = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array); 
     list.setAdapter(adapter); 
     list.setTextFilterEnabled(true); 
    } 
} 

你需要長按菜單按鈕打開鍵盤。

不幸的是,此功能完全不足以對CursorAdapter中的結果集進行排序。事實上,當我將它綁定到SimpleCursorAdapter時,TextFilter甚至沒有響應。

解決方案:

這裏有一個基本方法,嘗試添加一個EditText和一個按鈕。說,你想按名稱排序的位置。當用戶點擊該按鈕您查詢什麼是在EditText上,這樣你的數據庫:

db.query(TABLE_LOCATIONS, PROJECTION_LOCATIONS, 
      FIELD_LOCATIONS_NAME " LIKE '%?%'", editTextString, 
      null, null, FIELD_LOCATIONS_NAME); 

當然,editTextString距離的EditText用戶的輸入。現在,只需設置從數據庫返回到您的適配器這樣的新光標:

adapter.changeCursor(newCursor); 

你將不得不使用相同的方法將不僅僅是名字比較排序,但這是一般的想法。設計明智的是,您可以將EditText和Button搜索移動到PopUpWindow中,或者您可以使用TextWatcher鍵入EditText中的每個新字母來重新查詢數據庫;有很多不同的方法。希望有所幫助。

+0

非常感謝您的關心。我一直在等待很長時間,我會試着...... – Izabilla