gpt4 book ai didi

java - 在 Android 中使用 Textwatcher 和 simpleexpandablelistview

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

在我之前的应用程序中,我有一个 ListView 和底部的编辑文本。我一直在编辑文本中使用文本观察器来过滤 ListView 。 ListView 的内容来自简单的游标适配器。

 edittext.addTextChangedListener(filterTextWatcher);

cursor.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
Cursor cursor=mDbHelper.fetchFilteredNotes(edittext.getText().toString());
return cur;
}
});

private TextWatcher filterTextWatcher = new TextWatcher() {

public void afterTextChanged(android.text.Editable s) {
};

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

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

在我当前的应用程序中,我有一个可扩展 ListView 。我想使用类似的功能,例如过滤可扩展 ListView 的内容。

我不确定我是否可以为此使用文本观察器。有没有其他方法可以完成此操作,或者我也可以在其中使用 textwatcher 吗? ?

这是代码的相关部分:

private DbAdapter mDbHelper;    
List<Map<String, String>> groupData,groupDataCat;
List<List<Map<String, String>>> childData,childDataCat;
Map<String, String> curGroupMap;
List<Map<String, String>> meaning=null;
Map<String, String> curChildMap;
private static final String WORD = "WORD";
private static final String MEANING = "MEANING";
SimpleExpandableListAdapter mAdapter;
ExpandableListView elvCat;


temp=mDbHelper.fetchAllNotes();
temp.moveToFirst();
getActivity().startManagingCursor(temp);

if(temp.moveToFirst()){
groupDataCat = new ArrayList<Map<String, String>>();
childDataCat = new ArrayList<List<Map<String, String>>>();

for (int i = 0; i < temp.getCount(); i++) {
Map<String, String> catGroupMap = new HashMap<String, String>();
groupDataCat.add(catGroupMap);
catGroupMap.put(WORD, temp.getString(temp.getColumnIndexOrThrow(DbAdapter.KEY_WORD)));

List<Map<String, String>> meaning = new ArrayList<Map<String, String>>();
Map<String, String> catChildMap = new HashMap<String, String>();
meaning.add(catChildMap);
catChildMap.put(MEANING, temp.getString(temp.getColumnIndexOrThrow(DbAdapter.KEY_MEANING)));
childDataCat.add(meaning);
temp.moveToNext();
}
}

mAdapter = new SimpleExpandableListAdapter(
WordListFragment.this.getActivity(),
groupDataCat,
R.layout.word_list_item,
new String[] {WORD},
new int[] { R.id.tvWord },
childDataCat,
R.layout.meaning_list_item,
new String[] {MEANING},
new int[] { R.id.tvMeaning}
);

view=inflater.inflate(R.layout.word_list_temp, container, false);
elvCat = (ExpandableListView) view.findViewById(R.id.elvWord);
elvCat.setAdapter(mAdapter);


return view;
}

最佳答案

我让它工作的方法是在 onTextChanged() 方法中,我调用一个函数来查询数据库并获取过滤后的数据。我根据查询结果再次重新填充可扩展 ListView 。

private void populateList(String filter) {
temp = mDbHelper.fetchSugNotes(filter);


temp.moveToFirst();
this.startManagingCursor(temp);
groupDataCat = new ArrayList<Map<String, String>>();
childDataCat = new ArrayList<List<Map<String, String>>>();

for (int i = 0; i < temp.getCount(); i++) {
Map<String, String> catGroupMap = new HashMap<String, String>();
groupDataCat.add(catGroupMap);
catGroupMap.put(WORD, temp.getString(temp
.getColumnIndexOrThrow(DbAdapter.KEY_WORD)));

List<Map<String, String>> meaning = new ArrayList<Map<String, String>>();
Map<String, String> catChildMap = new HashMap<String, String>();
meaning.add(catChildMap);
catChildMap.put(MEANING, temp.getString(temp
.getColumnIndexOrThrow(DbAdapter.KEY_MEANING)));
childDataCat.add(meaning);
temp.moveToNext();
}

mAdapter = new SimpleExpandableListAdapter(this, groupDataCat,
R.layout.word_list_item, new String[] { WORD },
new int[] { R.id.tvWord }, childDataCat,
R.layout.meaning_list_item, new String[] { MEANING },
new int[] { R.id.tvMeaning });

elvCat.setAdapter(mAdapter);

关于java - 在 Android 中使用 Textwatcher 和 simpleexpandablelistview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18254903/

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