gpt4 book ai didi

java - 在从android数据库加载的 ListView 中搜索

转载 作者:行者123 更新时间:2023-12-02 06:56:01 25 4
gpt4 key购买 nike

我的应用程序中有数据库,该数据库已完全加载到 listview 中,但是在 listview 中搜索时,它不起作用,并且应用程序没有崩溃,下面是我从数据库检索数据并加载到 listview 中的代码。

    -------------DatabaseClass----
public Cursor getCursor() {
// TODO Auto-generated method stub
// SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
// queryBuilder.setTables(TABLE_NAME);
// openDataBase();

Cursor c = null;
try {
String[] asColumnsToReturn = new String[] { "lang_from","_id" };
c = myDataBase.query("words2", asColumnsToReturn, null, null, null,
null, null);

} catch (Exception e) {
System.out.print("" + e);
}
return c;
}

-----------MainActivity----------------
public class MainActivity extends Activity {

DatabaseClass myDbHelper;
private Cursor ourCursor = null;
private IngredientAdapter adapter = null;

ListView myListView;
EditText etSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etSearch = (EditText) findViewById(R.id.etsearch);
myListView = (ListView) findViewById(R.id.listView1);
myListView.setTextFilterEnabled(true);
// = new DatabaseClass();
myDbHelper = new DatabaseClass(this);

try {

myDbHelper.createDataBase();

} catch (IOException ioe) {

throw new Error("Unable to create database");

}

try {

myDbHelper.openDataBase();
ourCursor = myDbHelper.getCursor();
startManagingCursor(ourCursor);
try {
adapter = new IngredientAdapter(this, ourCursor);
} catch (Exception e) {
System.out.print("" + e);
}
myListView.setAdapter(adapter);
} catch (SQLException sqle) {

throw sqle;

}
etSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
// MainActivity.this.adapter.getFilter().filter(cs);
Log.d("TAGGGGGGGG", cs.toString());
adapter.getFilter().filter(cs);

// adapter.notifyDataSetChanged();
//MainActivity.this.adapter.getFilter().filter(cs.toString());
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// adapter.getFilter().filter(s.toString());

}
});
}

class IngredientAdapter extends CursorAdapter {
@SuppressWarnings("deprecation")
public IngredientAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}

@Override
public void bindView(View row, Context ctxt, Cursor c) {
// TODO Auto-generated method stub
IngredientHolder holder = (IngredientHolder) row.getTag();
holder.populateFrom(c, myDbHelper);
Log.d("TAAAAAAAAAAAAAAAGGGGG", "GOOD");
}

@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
IngredientHolder holder = new IngredientHolder(row);
row.setTag(holder);
Log.d("TAAAAAAAAAAAAAAAGGGGG", "GOOD");
return (row);
}
}

class IngredientHolder {
private TextView name = null;

IngredientHolder(View row) {
name = (TextView) row.findViewById(R.id.ingredientText);

}

void populateFrom(Cursor c, DatabaseClass r) {
name.setText(r.getName(c));
}
}
}

请帮忙,对英语不好的人表示歉意

最佳答案

我已经在我的应用程序中实现了这种功能。

简要说明。实现您自己的扩展 Filter 的类,如下一个类:

private class PlanetFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = planetList;
results.count = planetList.size();
}
else {
// We perform filtering operation
List<Planet> nPlanetList = new ArrayList<Planet>();

for (Planet p : planetList) {
if (p.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
nPlanetList.add(p);
}

results.values = nPlanetList;
results.count = nPlanetList.size();

}
return results;
}
}

在您的基本适配器中实现 Filterable 接口(interface),并且它必须实现 getFilter() 方法:

@Override
public Filter getFilter() {
if (planetFilter == null)
planetFilter = new PlanetFilter();

return planetFilter;
}

要将所有内容结合在一起,请在您输入文本的 edittext 上使用 textWatcher

editTxt.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text ["+s+"]");
aAdpt.getFilter().filter(s.toString());
}

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

}

@Override
public void afterTextChanged(Editable s) {
}
});

该代码取自下一个 tutorial .

关于java - 在从android数据库加载的 ListView 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17338025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com