2012-04-10 68 views
0

我正在构建我的第一个Android应用程序,并且遇到问题。在应用程序中,我有一个使用ListView的搜索功能。这里用户可以搜索某个项目并查看它是否存在。这真的很好,我根据这个教程做了:Android ListView with SearchAndroid搜索Expandable列表查看

我所有的项目都在array.xml中定义的字符串数组中找到。现在,每个项目都可以成为一个或多个集合的一部分。目前,我会在项目名称旁边显示其包含的收藏,如下所示:“ITEM - >收藏”。这可行,但不是一个优雅的选择。我想要做的是将项目的名称作为可扩展ListView中的ITEM,并将它作为SUBITEM所属的集合。因此,我会有2个字符串数组,一个用于父项,一个用于子项。

我发现的所有例子都是“手动”填充列表,我无法弄清楚如何将我的ITEMS数组分配给GROUP以及将COLLECTIONS数组分配给CHILDREN。

我不知道如果我解释这显然不够好,但总之,这将是这样的:

ITEM_1 = 1组名 COLLECTIONS_1 =第一组的孩子

Example Image

谢谢您!

+0

你真的想要一个可展开的列表视图,还是你想要一个双行列表视图,其中顶部是Item和底部的集合?你在图像上显示的内容对我来说看起来像是后者,而不是前者。 – Barak 2012-04-10 13:40:26

+0

我想现在2线解决方案也可以。在定义字符串数组项目时,我通过使用类似于“ITEM \ r \ n - > Collection(s)”的方式半实现了这个想法。现在集合在第二行。有可能将这些格式与第一行进行不同的格式化? – 2012-04-10 19:23:17

回答

0

编辑: 好,成功地创造了可扩展视图就像我想它是这样的:

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; 
    } 
} 

现在的问题是,我怎么能进行排序呢?

+1

如果你有一个后续问题,考虑发布一个新的或者编辑当前的(隐藏在你的答案中不太可能有帮助,因为没有人会期待它:-) – kleopatra 2013-04-03 16:14:26

0

您从这里下载源代码(Expandablelistview with searchview in android

MainActivity.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); 
        } 


       } 

      } 
     }); 


    } 

谢谢!