【发布时间】:2016-08-12 12:26:46
【问题描述】:
我有一个视图,其中我将单个 EditText 和屏幕分隔在两个部分(2 个 ListViews)上。我想将数据输入到 EditText 并使用这些数据以不同的方式更新两个 ListView。例如,对于第一个 ListView,以我的输入开头的单词。到第二个 ListView 去包含我输入的单词。
我制作了两个不同的自定义 ListAdapter 来实现所需的功能。然后我将它们添加到我的 EditText 对象并开始出现错误。如果我只使用一个适配器,一切都很好。
那么,我怎样才能将两个适配器用于单个 EditText 以不同的方式进行此更新 2 ListViews。
这是我的代码。
ListView lv;
ListView lv2;
ArrayAdapter<String> adapterForSearch;
ArrayAdapter<String> adapterForTurkishSearch;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.dictionary_screen);
mContext = this;
List<Map<String,?>> dictionaryTopics = new LinkedList<>();
dictionaryTopics.add(createItem("Я ТЫ МЫ ВЫ", formWordCouplesSet(words.getMeU())));
dictionaryTopics.add(createItem("Animals", formWordCouplesSet(words.getAnimals())));
// create our list and custom adapter
SeparatedListAdapter adapter = new SeparatedListAdapter(this);
adapter.addSection("Turkish Dictionary", new SimpleAdapter(this, dictionaryTopics, R.layout.list_complex,
new String[]{ITEM_TITLE, ITEM_CAPTION}, new int[]{R.id.list_complex_title, R.id.list_complex_caption}));
list = (ListView) findViewById(R.id.dictionary_topics);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int index=0; index<((ViewGroup)view).getChildCount(); ++index) {
View nextChild = ((ViewGroup)view).getChildAt(index);
if (index >0) {
if (checkVisibility(nextChild))
{
nextChild.setVisibility(View.GONE); dimCounter--; checkDimCounter();
}
else {
nextChild.setVisibility(View.VISIBLE); dimCounter++; checkDimCounter();
}
}
}
}
});
//Search Implementation
inputSearch = (EditText) findViewById(R.id.inputSearch);
lv = (ListView) findViewById(R.id.list_view);
lv2 = (ListView) findViewById(R.id.list_view2);
adapterForSearch = new MyAdapter<>(this, R.layout.list_item2, R.id.product_name, wordCombos);
adapterForTurkishSearch = new MyTurkishAdapter<>(this, R.layout.list_item2, R.id.product_name, wordCombos);
TextWatcher watcher1;
watcher1 = new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
if (cs.length() == 0)
{
lv.setAdapter(null);
}
else {
lv.setAdapter(adapterForSearch);
adapterForSearch.getFilter().filter(cs);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
};
TextWatcher watcher2;
watcher2 = new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
if (cs.length() == 0)
{
lv2.setAdapter(null);
}
else {
lv2.setAdapter(adapterForTurkishSearch);
adapterForTurkishSearch.getFilter().filter(cs);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
};
inputSearch.addTextChangedListener(watcher1);
inputSearch.addTextChangedListener(watcher2);
}
【问题讨论】:
标签: android android-arrayadapter listadapter textwatcher