2012-04-01 52 views
2

ListViews一直是我的弱點,現在我正在練習Listview,在Listview內。無論如何,我第一次叫我ListView在我的節目的開始,並將其與保存在數組中加載它我strings.xml重複使用相同的ListView顯示不同的數據

String[] departments = getResources().getStringArray(
       R.array.departments_array); 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, 
       departments)); 
     setContentView(R.layout.main); 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

我想要做的就是更新這個ListView與值的新陣列中的每個時間列表項被點擊。我試圖這樣做的原因是因爲我打算爲每個位置使用具有不同值的27個不同數組,如果不是爲每個數組項創建ListView,我覺得它會在我的資源上更輕會更新這一個ListView。我知道我可能不是這樣做的最有效的方式,但如果有另一種實現我的想法的方式,請告訴我。

lv.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        // When clicked, show a toast with the TextView text 
        switch (position) { 
        case 0: 
         try { 


    //It is here that i dont know what to do, I was going to call 
//the Listview the same way i did previously using my setlistadapter, 
//but i kept getting errors about the code being undefined 


          String[] listitems1 = getResources().getStringArray(
            R.array.items_array); 

         } catch (ClassCastException e) { 
          Toast.makeText(getApplicationContext(), "Error", 
            Toast.LENGTH_SHORT).show(); 
         } 
         break; 
        case 1: 
         try { 
         //The listview will be changed again here 
         } catch (ClassCastException e) { 
          Toast.makeText(getApplicationContext(), "Error", 
            Toast.LENGTH_SHORT).show(); 
         } 
         break; 
        } 
       }; 

      }); 

回答

1

你的做法是錯誤的(如果我理解你在做什麼)。每次用戶點擊(並且簡單地設置一個新的適配器都可以工作)時,而不是每次用戶點擊(並且簡單地設置一個新的適配器都可以工作)時替換適配器ListView,而不是在初始列表中的一個元素上開始新的活動,並且在新活動中設置適配器ListView基於該位置的正確數組。

一個小例子:

主要類:

/** 
* The main class with the initial 27 items array. 
*/ 
public class Class1 extends ListActivity { 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // start the second activity that will show our array depending on the 
     // position clicked 
     Intent i = new Intent(this, Class2.class); 
     // put the position in the Intent so we can know in the other activity 
     // what array to load. 
     i.putExtra("pos", position); 
     startActivity(i); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // I just used a simple array of 2 items, you'll load your 27 items 
     // array 
     String[] items = { "1", "2" }; 
     setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, items)); 
    } 

} 

次活動將展示基於先前所選位置的數組:

public class Class2 extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // get the Intent that started the activity 
     Intent i = getIntent(); 
     // find out what position did that other activity send to us. 
     int position = i.getIntExtra("pos", -1); 
     // load the ListView with an adapter based on the array that you 
     // want(according to that position) 
     if (position == 0) { 
      // the first element in the main list 
      String[] items = getResources().getStringArray(R.array.a1); 
      setListAdapter(new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, items)); 
     } else if (position == 1) { 
      // the second element in the main list 
      String[] items = getResources().getStringArray(R.array.a2); 
      setListAdapter(new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, items)); 
     } else { 
      // etc 
     } 
    } 

} 
+0

如果我爲每個數組開始一個新的活動,在列表視圖中,那會導致我有大約27個額外的類?那不好嗎?我一直認爲我想盡量儘量減少一切 – 2012-04-01 21:59:38

+0

@TheObliviator我的答案是,當你有一個主要清單27個項目,併爲這27個項目中的每一個你有另一個值的數組,你想要也顯示在列表中(這是我的理解,並且沒有你沒有27個活動,你只需要一個主要活動和一個活動來顯示與點擊位置相對應的數組,我可以給你一個例子)。如果這不是你想要的,你應該編輯你的問題,更好地解釋你想要的。 – Luksprog 2012-04-02 07:30:06

+0

這正是我想要的,而且即時嘗試並考慮一些當我從學校回家時,但是是某種例子或僞代碼將幫助很多 – 2012-04-02 13:29:27

1

Luksprog的答案確實是正確的,它對於深層次的列表非常有用(您不要放置限制,只需在加載正確列表的情況下產生新的活動實例)

如果列表不超過2級深,你可以使用ExpandableListActivity代替ListActivity基本上是單級列表的增強版本,您正在使用的本地處理組摺疊/展開因此你不需要爲每個子級生成一個新的活動。

再次注意,這種方法僅適用於列出了不要去深度超過2級

在這裏你有一個很好的例子來自Google本身:

public class ExpandableList3 extends ExpandableListActivity { 
    private static final String NAME = "NAME"; 
    private static final String IS_EVEN = "IS_EVEN"; 

    private ExpandableListAdapter mAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); 
     List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>(); 
     for (int i = 0; i < 20; i++) { 
      Map<String, String> curGroupMap = new HashMap<String, String>(); 
      groupData.add(curGroupMap); 
      curGroupMap.put(NAME, "Group " + i); 
      curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd"); 
      //filling with dummy data... 
      List<Map<String, String>> children = new ArrayList<Map<String, String>>(); 
      for (int j = 0; j < 15; j++) { 
       Map<String, String> curChildMap = new HashMap<String, String>(); 
       children.add(curChildMap); 
       curChildMap.put(NAME, "Child " + j); 
       curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd"); 
      } 
      childData.add(children); 
     } 

     // Set up our adapter 
     mAdapter = new SimpleExpandableListAdapter(
      this, 
      groupData, 
      android.R.layout.simple_expandable_list_item_1, 
      new String[] { NAME, IS_EVEN }, 
      new int[] { android.R.id.text1, android.R.id.text2 }, 
      childData, 
      android.R.layout.simple_expandable_list_item_2, 
      new String[] { NAME, IS_EVEN }, 
      new int[] { android.R.id.text1, android.R.id.text2 } 
      ); 
     setListAdapter(mAdapter); 
    } 

} 
+0

謝謝你們都給了我很好的答案,並感謝有關ExpandableListActivity的信息,我會開始更多地使用它 – 2012-04-03 18:18:54