2012-04-01 15 views
2

ListViewsは常に私の弱点であり、今はListviewListviewに入れて練習しています。とにかく、私が最初に私のプログラムの開始時に私の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ある値の新しい配列と各時間Aリスト項目をクリックします。私がこのようにしようとしている理由は、それぞれの位置に異なる値を持つ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

BaseAdapterを使用して、リストアダプタとして、それを設定するの考えています。最初のリストの要素をクリックする(新しいアダプタを設定するだけで)最初のリストの要素をクリックするたびに、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の余分なクラスを持っていますか?それは悪いですか?私はいつも私ができるだけすべてを最小限にしようとしていると思っていたと思った –

+0

@TheObliviator私の答えは、あなたが27項目のメインリストを持っている場合、それらの27項目のそれぞれに対して、リストにも表示されます(これは私が理解しているもので、27のアクティビティーを1つのメインアクティビティーとクリックした位置に対応する配列を表示する1つのアクティビティーがないわけではありません)。これがあなたが望むものでない場合は、あなたの質問を編集して、あなたが望むものをよりよく説明する必要があります。 – Luksprog

+0

これはまさに私が欲しいものです。そして、私は学校から家に帰るともう少しそれを試してみるつもりですが、ある種の例や擬似コードが役に立ちます。 –

2

あなたは(私はあなたが何をしている理解していれば)あなたのアプローチは間違っている http://developer.android.com/reference/android/widget/BaseAdapter.html

+0

どうすれば問題を解決できますか? –

+0

'ArrayAdapter'を使うのではなく、' BaseAdapter'を拡張すると、データをより詳細に制御できます。最初から質問をはっきりと理解した場合 – user1304907

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

ありがとうあなたは私に本当に良い答えをくれました.ExpressableListActivityについての情報をありがとう、もっと頻繁にそれを使い始めるつもりです –

関連する問題