SQLITE LISTVIEW FILTER USING EDITEXT



请我真的需要列表视图过滤器的帮助,因为我看过很多教程,但它不起作用。我真的厌倦了这个只是我需要 EdiText 过滤器

我的项目包含四个类:

1-位置活动(l'活动预)

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-位置列表活动(列表视图活动)

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-位置数据库 (数据库活动 sqlite)

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-最佳位置代理

在布局中,我有位置.xml和列表.xml

1-位置.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-列表.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>

不好意思。。。太长

我想我明白你想做什么。下面是使用 ListView 的文本筛选器的示例:

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 的结果集进行排序。事实上,当我将TextFilter绑定到SimpleCursorAdapter时,它甚至没有响应。

溶液:

这是一种基本方法,请尝试添加编辑文本和按钮。 假设您要按名称对位置进行排序。 当用户单击该按钮时,您可以使用 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 中来重新查询数据库而不是按钮;有许多不同的方法。希望有帮助。

最新更新