我在刷新ListView时遇到了问题。我调用我的ListView Activity,我使用带有游标的SQL语句从DB获取数据,用自定义适配器(扩展BaseAdapter)绘制ListView,所有这些都很好。
我想在顶部放置一个EditText,在我的ListView上搜索项目,用类型化的文本过滤实际的视图,同样的活动。我从getText()中提取EditText,并使用本文对结果进行新的SQL语句过滤。我已经检查了游标是否有正确的过滤结果,但是我使用adapter.notifyDataSetChanged()试图更新ListView,但是什么也没有发生(没有错误或强制关闭)。
有人能帮我或告诉我一些技巧来获得这个简单的搜索功能使用数据库吗?我在ArrayLists + getFilter() + SimpleCursorAdapter中发现了许多类似的问题,但是它不适用于我的DB和自定义适配器。
Activity ListaCaracteres:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(es.hsk.ap.R.layout.lista_caracteres);
try{
hanyuDBHelper = new HanyuSQLHelper(this);
hanyuDB = hanyuDBHelper.getReadableDatabase();
c = hanyuDB.rawQuery(" SELECT * FROM Caracteres ", null);
}
catch(SQLiteException ex)
{
Toast mensajeError=Toast.makeText(getApplicationContext(), "Error accediendo a los datos", Toast.LENGTH_SHORT);
mensajeError.show();
Log.e("LogLugares", "Error en getReadableDatabase()", ex);
}
adapter = new AdapterListaCaracteres(this,c);
listaCaracteres = (ListView)findViewById(es.hsk.ap.R.id.listaCaracteres);
listaCaracteres.setAdapter(adapter);
buscador = (EditText)findViewById(R.id.buscador);
buscador.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
}
@Override
public void beforeTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0)
{
// TODO Auto-generated method stub
String texto="";
texto=buscador.getText().toString();
c = hanyuDB.rawQuery(" SELECT * FROM Caracteres WHERE significado LIKE '%"+texto+"%'", null);
adapter.notifyDataSetChanged();
}
});
}我的自定义适配器,AdapterListaCaracteres从BaseAdapter扩展而来:
public class AdapterListaCaracteres extends BaseAdapter implements ListAdapter{
private Context mContext;
private Cursor datos;
private LayoutInflater inflater;
public AdapterListaCaracteres(Context context, Cursor c){
this.mContext = context;
this.datos = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datos.getCount();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datos.getString(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View item = convertView;
ViewHolder holder;
inflater = (LayoutInflater) mContext.getSystemService(Context .LAYOUT_INFLATER_SERVICE);
datos.moveToPosition(position);
if(item==null){
try{
item = inflater.inflate(es.hsk.ap.R.layout.caracter, null);
}
catch(InflateException ex)
{
}
holder = new ViewHolder();
holder.caracter = (TextView)item.findViewById(es.hsk.ap.R.id.caracter);
holder.pinyin = (TextView)item.findViewById(es.hsk.ap.R.id.pinyin);
holder.significado = (TextView)item.findViewById(es.hsk.ap.R.id.significado);
item.setTag(holder);
}
else{
holder = (ViewHolder)item.getTag();
}
holder.caracter.setText(datos.getString(2));
holder.pinyin.setText(datos.getString(3));
holder.significado.setText(datos.getString(4));
return item;
}
static class ViewHolder{
TextView caracter;
TextView pinyin;
TextView significado;
}
}发布于 2012-04-06 09:35:30
试试这个:
/*
* Listener que actuará cuando se escriba en el EditText
*/
buscador.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
String texto = "";
texto = buscador.getText().toString();
c = hanyuDB.rawQuery(" SELECT * FROM Caracteres WHERE significado LIKE '%" + texto + "%'", null);
AdapterListaCaracteres adapter2 = new AdapterListaCaracteres(getApplicationContext(), c, false);
listaCaracteres.setAdapter(adapter2);
return false;
}
});
/*********************************************/(好运;)
亚历克斯
发布于 2012-04-02 10:10:48
重新初始化适配器而不是notifydatasetchanged():
adapter = new AdapterListaCaracteres(this,c);
listaCaracteres.setAdapter(adapter);发布于 2012-04-04 21:29:48
现在起作用了!我的一个朋友(谢谢穆尼!)找到了一种更好的方法来使用EditText对listview结果进行过滤:
buscador.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {而不是:
buscador.addTextChangedListener(new TextWatcher()
{谢谢大家的帮助。
编辑:
更新@AlexMuni和我自己的答案,onKey()方法在仿真器上运行得很好,但在实际设备中却不能很好地工作。
我终于用前面的方法完美地完成了这一工作:
buscador = (EditText)findViewById(R.id.buscador);
buscador.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
texto = buscador.getText().toString();
c = hanyuDB.rawQuery(
" SELECT * FROM Caracteres WHERE significado LIKE '%"
+ texto + "%'", null);
AdapterListaCaracteres adapter2 = new AdapterListaCaracteres(ListaCaracteres.this, c, false);
listaCaracteres.setAdapter(adapter2);
}
@Override
public void beforeTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
}
@Override
public void afterTextChanged(Editable arg0)
{
}
}); https://stackoverflow.com/questions/9974355
复制相似问题