2012-04-10 115 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); 
        } 


       } 

      } 
     }); 


    } 

謝謝!