大家好我是android的新手我在自定义simplecursoradapter类的帮助下,在Sqlite数据库的列表视图中显示带有文本视图的图像视图,并且它工作正常。 现在我想为这些listview设置搜索视图,从sqlite显示如何添加搜索框和查询搜索请发布完整代码
我的ImageCursorAdapter.java
public class ImageCursorAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
byte[] image;
public ImageCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
}
public View getView(int pos, View inView, ViewGroup parent) {
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_view__patients, null);
}
this.c.moveToPosition(pos);
String firstName = this.c.getString(this.c.getColumnIndex("_id"));
String lastName = this.c.getString(this.c.getColumnIndex("Name"));
image = this.c.getBlob(this.c.getColumnIndex("Image"));
ImageView iv = (ImageView) v.findViewById(R.id.all_img);
if (image != null) {
if (image.length > 0) {
iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
} else {
iv.setImageResource(R.mipmap.ic_launcher);
}
}
TextView fname = (TextView) v.findViewById(R.id.db_id);
fname.setText(firstName);
TextView lname = (TextView) v.findViewById(R.id.name_id);
lname.setText(lastName);
return (v);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper db;
SQLiteDatabase sqLiteDatabase;
EditText et;
Cursor row;
ListView lv;
ImageCursorAdapter adapter;
int[] xml_id;
String[] column;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
db = new DatabaseHelper(this);
open();
db.getReadableDatabase();
db.getWritableDatabase();
lv = (ListView) findViewById(R.id.listV);
int layoutstyle = R.layout.activity_view__patients;
xml_id = new int[]{
R.id.db_id,
R.id.name_id,
R.id.all_img
};
column = new String[]{
"patients._id",
"patients.Name",
"patients.Image"
};
row = db.fetchAllData();
adapter = new ImageCursorAdapter(getApplicationContext(), layoutstyle, row, column, xml_id);
lv.setAdapter(adapter);
private void open() {
sqLiteDatabase = openOrCreateDatabase(DatabaseHelper.DATABASE_NAME, Context.MODE_PRIVATE, null);
}
}
答案 0 :(得分:0)
您可以在listview控件上方添加edittext框。 并进行如下的SQL查询..
public List<MyTable> getSearchData(String value) {
MyTable data = null;
List<MyTable> myTableList=new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + "User" + " WHERE name=" + value;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
data = new MyTable();
data.id = (c.getInt(c.getColumnIndex("id")));
data.comment = (c.getString(c.getColumnIndex("comment")));
data.itemId = (c.getInt(c.getColumnIndex("item_id")));
myTableList.add(data);
}
c.close();
db.close();
return myTableList;
}
点击按钮或其他点击事件时调用此方法..
getSearchData(edittext.getText().toString().trim());
答案 1 :(得分:0)
根据您的代码,这是一个有效的例子: -
首先是DatabaseHelper类(为方便起见而命名): -
public class SO49813098DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "patients";
public static final int DBVERSION = 1;
public static final String TB_PATIENT = "patient";
public static final String PATIENT_COL_ID = BaseColumns._ID;
public static final String PATIENT_COL_NAME = "patient_name";
public static final String PATIENT_COL_IMAGE = "patient_image";
SQLiteDatabase mDB;
public SO49813098DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String crtsql = "CREATE TABLE IF NOT EXISTS " + TB_PATIENT + "(" +
PATIENT_COL_ID + " INTEGER PRIMARY KEY," +
PATIENT_COL_NAME + " TEXT," +
PATIENT_COL_IMAGE + " BLOB DEFAULT X'000000000000'" +
")";
db.execSQL(crtsql);
}
public long addPatient(String patient_name) {
ContentValues cv = new ContentValues();
cv.put(PATIENT_COL_NAME,patient_name);
return mDB.insert(TB_PATIENT,null,cv);
}
public Cursor getPatients(String filter_name) {
String whereclause = PATIENT_COL_NAME + " LIKE ?";
String[] whereargs = new String[]{"%" + filter_name + "%"};
return mDB.query(TB_PATIENT,
null,
whereclause,
whereargs,
null,
null,
PATIENT_COL_NAME
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
ImageCursorAdpter: -
public class ImageCursorAdapter extends CursorAdapter {
private Cursor csr;
private Context context;
public ImageCursorAdapter(Context context, Cursor c) {
super(context,c,0);
//csr = c;
this.context = context;
}
@Override
public View newView(Context context,
Cursor c,
ViewGroup parent) {
return LayoutInflater.from(context).inflate(
R.layout.activity_view__patients,
parent,
false
);
}
public View getView(int pos, View inView, ViewGroup parent) {
return super.getView(pos,inView,parent);
}
@Override
public void bindView(View view, Context context, Cursor csr) {
TextView dbid = (TextView) view.findViewById(R.id.db_id);
TextView name = (TextView) view.findViewById(R.id.name_id);
ImageView iv = (ImageView) view.findViewById(R.id.all_img);
dbid.setText(String.valueOf(csr.getLong(csr.getColumnIndex(SO49813098DBHelper.PATIENT_COL_ID))));
name.setText(csr.getString(csr.getColumnIndex(SO49813098DBHelper.PATIENT_COL_NAME)));
iv.setImageResource(R.mipmap.ic_launcher_round);
}
}
bindView
方法而不是getView
方法来填充视图,因为它传递了光标(我认为你在定位光标方面遇到的问题之一)。 MainActivity: -
public class MainActivity extends AppCompatActivity {
Cursor patients;
ListView patients_listview;
EditText patients_filter;
String filter;
SO49813098DBHelper mDBHelper;
ImageCursorAdapter ica;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SO49813098();
}
private void SO49813098() {
// Sets the Databaehelper
mDBHelper = new SO49813098DBHelper(this);
// Add some data for testing
if (DatabaseUtils.queryNumEntries(mDBHelper.getWritableDatabase(),SO49813098DBHelper.TB_PATIENT) < 1) {
mDBHelper.addPatient("Fred");
mDBHelper.addPatient("Bert");
mDBHelper.addPatient("Harry");
mDBHelper.addPatient("The quick brown fox jumped over the lazy white dog");
}
// Get the views according to their id's
patients_listview = (ListView) this.findViewById(R.id.patients);
patients_filter = (EditText) this.findViewById(R.id.filter);
filter = ""; // Initially set filter to show all
// get the Cursor
patients = mDBHelper.getPatients(filter);
// setup the Adapter
ica = new ImageCursorAdapter(this, patients);
// Apply the adapter to the listview
patients_listview.setAdapter(ica);
// Add TextWatcher to the Filter Edittext to invoke the SO49813098RefreshList method
patients_filter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
SO49813098RefreshList(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
// refresh the list by getting the Cursor according to the search
// and then swapping the Cursor
private void SO49813098RefreshList(String s) {
patients = mDBHelper.getPatients(s);
ica.swapCursor(patients);
}
}