2011-11-09 10 views
1

私はチェックボックスの助けを借りて複数の選択肢でカスタマイズリストを作成しています。最後に、リストの項目選択のイベントで選択されたチェックボックスを設定することができました。androidのカスタムリストビュー

しかし、チェックボックスがリストの選択に従って選択されていない場合 最初の行をクリックすると、4行目のチェックボックスが自動的にクリックされます。短いシーケンスでは維持されません。 コード私が働いているとは、など

ListAdapter adapter = new SimpleAdapter(
        this, 
        Datalist , 
        R.layout.customlist, 
        new String[] {"fileName","contentLength","keyPath"}, 
        new int[] {R.id.title,R.id.size, R.id.path} 
); 
      setListAdapter(adapter); 

protected void onListItemClick(ListView l, View v, int position, long id) { 
      super.onListItemClick(l, v, position, id); 
      ViewGroup group=(ViewGroup)v; 
     CheckBox check=(CheckBox)group.findViewById(R.id.sharecheckbox); 
     check.toggle(); 
} 
+0

http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7739006#7739006 – MKJParekh

+0

ここにこのチュートリアルをチェックカスタマイズされたリストの良い例を考えてみましょう。 [http://www.vogella.de/articles/AndroidListView/article.html] (http://www.vogella.de/articles/AndroidListView/article.html) – Pratik

答えて

1
ListView mainListView; 
    mainListView = (ListView) findViewById(R.id.mainListView); 

    // Create and populate a List of planet names. 
    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars", 
             "Jupiter", "Saturn", "Uranus", "Neptune"};  
    ArrayList<String> planetList = new ArrayList<String>(); 
    planetList.addAll(Arrays.asList(planets)); 

    // Create ArrayAdapter using the planet list. 
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList); 

    // Add more planets. If you passed a String[] instead of a List<String> 
    // into the ArrayAdapter constructor, you must not add more items. 
    // Otherwise an exception will occur. 
    listAdapter.add("Ceres"); 
    listAdapter.add("Pluto"); 
    listAdapter.add("Haumea"); 
    listAdapter.add("Makemake"); 
    listAdapter.add("Eris"); 

    // Set the ArrayAdapter as the ListView's adapter. 
    mainListView.setAdapter(listAdapter);   
    } 
} 
1

下にあるリストビューでは、Google I/Oから、この非常に詳細なexplanationあたりとして、再利用するリスト項目を維持するために使用しています。

したがって、checkは、リスト内のその特定のアイテムについて同じままではありません(つまり、リストビューを再描画するか、他のアイテムがチェックされる可能性があります)。

私は自分がチェック状態配列(onListItemClickでそれへの設定値を())を維持することをお勧め:

  • 自身Datalistに「にisChecked」を維持し、適切なチェックボックスを設定するために独自のSimpleAdapter.ViewBinderを使用するには、setViewValue()が呼ばれるたびに述べて;
  • ArrayAdapterを拡張し、状態バインドをgetView()にチェックしました。
関連する問題