[英]Android Search Expandable ListView
我在构建我的第一个 android 应用程序时遇到了问题。 在应用程序中,我有一个使用 ListView 的搜索 function。 用户可以在这里搜索某个项目并查看它是否存在。 这非常有效,我是根据本教程完成的: Android ListView with Search
我的所有项目都在 array.xml 中定义的字符串数组中找到。 现在,每个项目都可以是一个或多个 collections 的一部分。目前我显示 collections 它包含在项目名称旁边,如下所示:“ ITEM -> Collection(s) ”。 这可行,但并不是真正优雅的选择。 我想要做的是将项目的名称作为可扩展 ListView 中的 ITEM,并将它所属的 collections 作为 SUBITEM。 因此,我会有 2 个字符串 arrays,一个用于父项,一个用于子项。
我发现的所有示例都是“手动”填充列表,我无法弄清楚如何将我的 ITEMS 数组分配给 GROUP 并将我的 COLLECTIONS 数组分配给 CHILDREN。
我不知道我是否解释得足够清楚,但简而言之,它会是这样的:
ITEM_1 = 第一组名称 COLLECTIONS_1 = 第一组的孩子
谢谢!
编辑:好的,设法像我想要的那样创建可扩展视图:
public class Search extends ListActivity {
private String[] l1;
private String[] l2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
l1 = getResources().getStringArray(R.array.items);
l2 = getResources().getStringArray(R.array.collections);
Integer i;
int to = l1.length;
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
for(i=0;i < to;i++){
list.add(putData(l1[i], l2[i]));
}
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
}
现在的问题是,我该如何排序?
你从这里下载源代码( Expandablelistview with searchview in android )
主活动.java:
public class MainActivity extends AppCompatActivity {
ExpandableListView ev_list;
List> lv_country = new ArrayList<>();
CustomAdapter customAdapter;
EditText et_search;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
init();
}
private void init() {
ev_list = (ExpandableListView) findViewById(R.id.ev_list);
et_search = (EditText) findViewById(R.id.et_search);
fn_arraylist();
customAdapter = new CustomAdapter(lv_country, getApplicationContext());
ev_list.setAdapter(customAdapter);
ev_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true; // This way the expander cannot be collapsed
}
});
for (int i = 0; i < customAdapter.getGroupCount(); i++) {
ev_list.expandGroup(i);
}
et_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (et_search.getText().toString().length() == 0) {
customAdapter = new CustomAdapter(lv_country, getApplicationContext());
ev_list.setAdapter(customAdapter);
for (int i = 0; i < customAdapter.getGroupCount(); i++) {
ev_list.expandGroup(i);
}
} else {
List> lv_search = new ArrayList<>();
for (int i = 0; i < lv_country.size(); i++) {
List> lv_searchstate = new ArrayList<>();
List> lv_state = (List>) lv_country.get(i).get("State");
for (int j = 0; j < lv_state.size(); j++) {
if (lv_state.get(j).get("Name").toString().toLowerCase().contains(et_search.getText().toString())) {
lv_searchstate.add(lv_state.get(j));
}
}
if (lv_searchstate.size() != 0) {
HashMap hashMap_search = new HashMap<>();
hashMap_search.put("Name", lv_country.get(i).get("Name").toString());
hashMap_search.put("State", lv_searchstate);
lv_search.add(hashMap_search);
}
}
customAdapter = new CustomAdapter(lv_search, getApplicationContext());
ev_list.setAdapter(customAdapter);
for (int i = 0; i < customAdapter.getGroupCount(); i++) {
ev_list.expandGroup(i);
}
}
}
});
}
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.