2016-12-13 17 views
0

内部にチェックボックスを持つlistViewのArrayAdapterがあります。 チェックボックスの値が変更された場合、チェックボックスをチェックしたアイテムのリストを取得します。Androidリストビュー内の子アイテムを取得

私の活動のための onCreate関数内

ArrayAdapter<DrawerDetail> adapter = new ArrayAdapter<DrawerDetail>(this, R.layout.settings_minidrawer_item, apps) { 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView == null){ 
      convertView = getLayoutInflater().inflate(R.layout.settings_minidrawer_item, null); 
     } 
     ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon); 
     appIcon.setImageDrawable(apps.get(position).icon); 
     appIcon.setTag(apps.get(position).name); 
     TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_name); 
     appLabel.setText(apps.get(position).label); 
     appLabel.setText(Integer.toString(position)); 
     CheckBox appCheck = (CheckBox)convertView.findViewById(R.id.item_app_check); 
     appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 
       UpdatePreferences(); 
      } 
     }); 

     return convertView; 
    } 
}; 

list.setAdapter(adapter); 

updatePreferences

public void UpdatePreferences() { 
    SharedPreferences.Editor prefEditor = prefs.edit(); 

    SparseBooleanArray checked = list.getCheckedItemPositions(); 

    List<String> drawerListArray = new ArrayList<String>(); 
    View v; 
    CheckBox c; 
    for(int i = 0; i < list.getAdapter().getCount(); i++) { 
     v = list.getAdapter().getView(i, null, list); 
     c = (CheckBox) v.findViewById(R.id.item_app_check); 
     TextView aka = (TextView) v.findViewById(R.id.item_app_name); 
     aka.setText("dd"); 
     Log.e("checked: ", Boolean.toString(c.isChecked())); 
    } 
} 

あなたがSparsBooleanArray checked値をチェックすると、それは常にnullです。またlist.getAdapter().getView()は機能していないようです。

この行は、(エラーを返しませんが、/設定された値を取得することはできません)任意のビューを見つけることができません。

c = (CheckBox) v.findViewById(R.id.item_app_check); 
TextView aka = (TextView) v.findViewById(R.id.item_app_name); 

私は別のaproachesを試してみましたが、それを動作させることができませんでした。今私の推測では、アダプターのためにgetViewメソッドをオーバーライドしており、それらは互いに矛盾していますが、私はどの解決法もgetViewをオーバーライドする方法を思いつくことができませんでした。

答えて

1

これらのビューを取得しないでください。 onCheckedChangeListenerにチェック/チェックされていない情報を渡します。文字列と情報がチェックされているかチェックされていない場合次に、チェックされた項目を配列で収集することができます。

はそのような何かを試してみてください。

public class TestClass { 

    private List<String> checkedStrings = new ArrayList<>(); 

    ... 

    void updateArray(boolean checked, String checkedText){ 
     if(checked){ 
      if(!checkedStrings.contains(checkedText)){ 
       checkedStrings.add(checkedText); 
      } 
     } else { 
      checkedStrings.remove(checkedText); 
     } 
    } 
} 

そして、あなたのArrayAdapterで:

appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 
       updateArray(isChecked, apps.get(position).label); 
      } 
     }); 
+0

はい、私の場合、チェックした値だけでなく、チェックした値もすべて取得する必要があります。私のアプローチはincorectと思われる。私はアンドロイドが初めてです – CBeTJlu4ok

+0

はい、私は理解しています - クラスに配列を作成し、onCheckedChangeが呼び出されたときにUpdatePreferencesメソッドでチェック/チェックされていない項目をこの配列に配置/削除できます。対応するリストビュー行も更新しますか?タイトルを変更しますか? –

+0

チェックされているチェックボックスの一覧だけではありませんが、このチェックボックスからタグを取得できるはずです。あなたはクラスの配列を作成することを意味しますか? – CBeTJlu4ok

1

あなたのコードを交換してみてください。

c = (CheckBox) v.findViewById(R.id.item_app_check); 

このコードで:

View nextChild = null; 

// Find CheckBox inside the parent view 
for (int i = 0; i < ((ViewGroup) v).getChildCount(); ++i) { 
    nextChild = ((ViewGroup) v).getChildAt(i); 
    if (nextChild instanceof CheckBox) { 
     // Do your work; 

     break; 
    } 
} 
+0

なぜそれは直接見つけることができないのですか? – CBeTJlu4ok

+1

別のクラス(あなたの場合はTestClass)のアダプタクラスに属するビューにアクセスしようとしているためです。したがって、アダプタクラス内からアクセスしようとしている場合は、(idによって)それを呼び出すだけです。 – AndroWeed

関連する問題