0

我这里有我的代码。数据库上的数据按标题过滤,我如何按标题或作者过滤数据?我认为它在 Catalogue.java 上的这些代码行中:

dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
         public Cursor runQuery(CharSequence constraint) {
             return dbHelper.fetchCollectionsByTitle(constraint.toString());

这是我的代码:

目录.java

package com.cvsu.catalogue.db;

import com.cvsu.catalogue.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
//import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

@SuppressLint("NewApi")
public class Catalogue extends Activity {

 private CollectionsDbAdapter dbHelper;
 private SimpleCursorAdapter dataAdapter;

 public final static String TITLE_EXTRA = "com.cvsu.catalogue.db._TITLE";
 public final static String AUTHOR_EXTRA = "com.cvsu.catalogue.db._AUTHOR";
 public final static String LOCATION_EXTRA = "com.cvsu.catalogue.db._LOCATION";
 public final static String CALLNUMBER_EXTRA = "com.cvsu.catalogue.db._CALLNUMBER";
 public final static String PUBLISHER_EXTRA = "com.cvsu.catalogue.db._PUBLISHER";
 public final static String DATEPUBLISHED_EXTRA = "com.cvsu.catalogue.db._DATEPUBLISHED";
 public final static String DESCRIPTION_EXTRA = "com.cvsu.catalogue.db._DESCRIPTION";


 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.catalogue_title);

  dbHelper = new CollectionsDbAdapter(this);
  dbHelper.open();

  //Generate ListView from SQLite Database
  displayListView();

 }

 private void displayListView() {


  Cursor cursor = dbHelper.fetchAllCollections();

  // The desired columns to be bound
  String[] columns  = new String[] {
    CollectionsDbAdapter.KEY_TITLE,
    CollectionsDbAdapter.KEY_AUTHOR,
    CollectionsDbAdapter.KEY_LOCATION,
    CollectionsDbAdapter.KEY_CALLNUMBER,
    CollectionsDbAdapter.KEY_PUBLISHER,
    CollectionsDbAdapter.KEY_DATEPUBLISHED,
    CollectionsDbAdapter.KEY_DESCRIPTION
  };

  // the XML defined views which the data will be bound to
  int[] to = new int[] {
    R.id.txtTitle,
    R.id.txtAuthor,
    //R.id.location,
    //R.id.callnumber,
    //R.id.publisher,
    //R.id.datepublished,
    //R.id.description,
  };

  // create the adapter using the cursor pointing to the desired data
  //as well as the layout information
  dataAdapter = new SimpleCursorAdapter(
    this, R.layout.book_info_title,
    cursor,
    columns,
    to,
    0);

  final ListView listView = (ListView) findViewById(R.id.listView1);
  // Assign adapter to ListView
  listView.setAdapter(dataAdapter);


  listView.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> listview, View view,
     int position, long id ) {
   // Get the cursor, positioned to the corresponding row in the result set
   /*Cursor cursor = (Cursor) listView.getItemAtPosition(position);

   // Get the state's capital from this row in the database.
   String bookTitle =
    cursor.getString(cursor.getColumnIndexOrThrow("title"));
   Toast.makeText(getApplicationContext(),
   bookTitle, Toast.LENGTH_SHORT).show();*/

   Intent i = new Intent (CatalogueTitle.this, BookInfoPage.class);

   i.putExtra(TITLE_EXTRA, String.valueOf(id));
   i.putExtra(AUTHOR_EXTRA, String.valueOf(id));
   i.putExtra(LOCATION_EXTRA, String.valueOf(id));
   i.putExtra(CALLNUMBER_EXTRA, String.valueOf(id));
   i.putExtra(PUBLISHER_EXTRA, String.valueOf(id));
   i.putExtra(DATEPUBLISHED_EXTRA, String.valueOf(id));
   i.putExtra(DESCRIPTION_EXTRA, String.valueOf(id));
   startActivity(i);
   }
  });

  EditText myFilter = (EditText) findViewById(R.id.myFilter);
  myFilter.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
   }

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

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
    dataAdapter.getFilter().filter(s.toString());
   }
  });

  dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
         public Cursor runQuery(CharSequence constraint) {
             return dbHelper.fetchCollectionsByTitle(constraint.toString());
         }
     });

 }

public static void main(String[] args) {

}
}

集合.Java

package com.cvsu.catalogue.db;

public class Collections {

   String title = null;
   String author = null;
   String location = null;
   String callnumber = null;
   String publisher = null;
   String datepublished = null;
   String description = null;

   public String getTitle() {
    return title;
   }
   public void setTitle(String title) {
    this.title = title;
   }
   public String getAuthor() {
    return author;
   }
   public void setAuthor(String author) {
    this.author = author;
   }
   public String getLocation() {
    return location;
   }
   public void setLocation(String location) {
    this.location = location;
   }
   public String getCallNumber() {
    return callnumber;
   }
   public void setCallNumber(String callnumber) {
    this.callnumber = callnumber;
   }
   public String getPublisher() {
        return publisher;
  }
  public void setPublisher(String publisher) {
   this.publisher = publisher;
  }
   public String getDatePublished() {
        return datepublished;
  }
  public void setDatePublished(String datepublished) {
    this.datepublished = datepublished;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  } 
  }

CollectionsDbAdapter.java

package com.cvsu.catalogue.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CollectionsDbAdapter {

 public static final String KEY_ROWID = "_id";
 public static final String KEY_TITLE = "title";
 public static final String KEY_AUTHOR = "author";
 public static final String KEY_LOCATION = "location";
 public static final String KEY_CALLNUMBER = "callnumber";
 public static final String KEY_PUBLISHER = "publisher";
 public static final String KEY_DATEPUBLISHED = "datepublished";
 public static final String KEY_DESCRIPTION = "description";

 private static final String TAG = "CollectionsDbAdapter";
 private DatabaseHelper mDbHelper;
 private SQLiteDatabase mDb;

 private static final String DATABASE_NAME = "LibraryCollections";
 private static final String SQLITE_TABLE = "Collections";
 private static final int DATABASE_VERSION = 1;

 private final Context mCtx;

 private static final String DATABASE_CREATE =
  "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
  KEY_ROWID + " integer PRIMARY KEY autoincrement," +
  KEY_TITLE + "," +
  KEY_AUTHOR + "," +
  KEY_LOCATION + "," +
  KEY_CALLNUMBER + "," +
  KEY_PUBLISHER + "," +
  KEY_DATEPUBLISHED + "," +
  KEY_DESCRIPTION + "," +
  " UNIQUE (" + KEY_CALLNUMBER +"));";

 private static class DatabaseHelper extends SQLiteOpenHelper {

  DatabaseHelper(Context context) {
   super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }


  @Override
  public void onCreate(SQLiteDatabase db) {
   Log.w(TAG, DATABASE_CREATE);
   db.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
     + newVersion + ", which will destroy all old data");
   db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
   onCreate(db);
  }
 }

 public CollectionsDbAdapter(Context ctx) {
  this.mCtx = ctx;
 }

 public CollectionsDbAdapter open() throws SQLException {
  mDbHelper = new DatabaseHelper(mCtx);
  mDb = mDbHelper.getWritableDatabase();
  return this;
 }

 public void close() {
  if (mDbHelper != null) {
   mDbHelper.close();
  }
 }

 public long createCollections(String title, String author,
   String location, String callnumber, String publisher, String datepublished, String description) {

  ContentValues initialValues = new ContentValues();
  initialValues.put(KEY_TITLE, title);
  initialValues.put(KEY_AUTHOR, author);
  initialValues.put(KEY_LOCATION, location);
  initialValues.put(KEY_CALLNUMBER, callnumber);
  initialValues.put(KEY_PUBLISHER, publisher);
  initialValues.put(KEY_DATEPUBLISHED, datepublished);
  initialValues.put(KEY_DESCRIPTION, description);

  return mDb.insert(SQLITE_TABLE, null, initialValues);
 }

 public Cursor fetchCollectionsByTitle(String inputText) throws SQLException {
  Log.w(TAG, inputText);
  Cursor mCursor = null;
  if (inputText == null  ||  inputText.length () == 0)  {
   mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
     KEY_TITLE, KEY_AUTHOR, KEY_LOCATION, KEY_CALLNUMBER, KEY_PUBLISHER, KEY_DATEPUBLISHED, KEY_DESCRIPTION},
     null, null, null, null, null);

  }

  else {
   mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
     KEY_TITLE, KEY_AUTHOR, KEY_LOCATION, KEY_CALLNUMBER, KEY_PUBLISHER, KEY_DATEPUBLISHED, KEY_DESCRIPTION},
     KEY_TITLE + " like '%" + inputText + "%'", null,
     null, null, null, null);
  }

  if (mCursor != null) {
       mCursor.moveToFirst();
      }

      return mCursor;

     }

 public Cursor fetchCollectionsByAuthor(String inputText) throws SQLException {
      Log.w(TAG, inputText);
      Cursor mCursor = null;
      if (inputText == null  ||  inputText.length () == 0)  {
       mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
         KEY_TITLE, KEY_AUTHOR, KEY_LOCATION, KEY_CALLNUMBER, KEY_PUBLISHER, KEY_DATEPUBLISHED, KEY_DESCRIPTION},
         null, null, null, null, null);

      }

      else {
       mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
         KEY_TITLE, KEY_AUTHOR, KEY_LOCATION, KEY_CALLNUMBER, KEY_PUBLISHER, KEY_DATEPUBLISHED, KEY_DESCRIPTION},
         KEY_AUTHOR + " like '%" + inputText + "%'", null,
         null, null, null, null);
      }

      if (mCursor != null) {
           mCursor.moveToFirst();
          }

          return mCursor;

         }

 public Cursor fetchAllCollections() {

  Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
    KEY_TITLE, KEY_AUTHOR, KEY_LOCATION, KEY_CALLNUMBER, KEY_PUBLISHER, KEY_DATEPUBLISHED, KEY_DESCRIPTION},
    null, null, null, null, null);

  if (mCursor != null) {
   mCursor.moveToFirst();
  }
  return mCursor;
 }
}
4

2 回答 2

2
@Override
        public Cursor runQuery(CharSequence constraint) {
            Cursor cur = null;
             database.openDataBase();
             if(constraint!=null){
                 cur = database.selectDataWithConstrain(constraint.toString());
             }

        return cur;
        }

使用此约束在您的数据库类中编写查询并获取标题或作者所需的数据

 public Cursor selectDataWithConstrain(String c) {

      // TODO Auto-generated method stub
         Cursor cursor = myDataBase.rawQuery("SELECT * FROM  tbl_xxx WHERE title LIKE '%"+c+"%'", null);

         return cursor;
  }
于 2014-05-15T07:09:20.287 回答
0

 public Cursor fetchdatabyfilter(String inputText,  String filtercolumn) throws SQLException {
        Cursor row = null;
        String query = "SELECT * FROM " + dbTable;
        if (inputText == null  || inputText.length() == 0 ) {
            row = sqlDb.rawQuery(query, null);
        } else {
            query = "SELECT * FROM " + dbTable + " WHERE " + filtercolumn + " like '%" + inputText +  "%'     ";
            row = sqlDb.rawQuery(query, null);
        }
        if (row != null)
        {
            row.moveToFirst();
        }
        return row;
    }
    
mainactivity:


  adapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {

                return db.fetchdatabyfilter(constraint.toString(),"title" );
            }
        });

于 2018-07-18T05:10:55.427 回答