内部にチェックボックスを持つ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をオーバーライドする方法を思いつくことができませんでした。
はい、私の場合、チェックした値だけでなく、チェックした値もすべて取得する必要があります。私のアプローチはincorectと思われる。私はアンドロイドが初めてです – CBeTJlu4ok
はい、私は理解しています - クラスに配列を作成し、onCheckedChangeが呼び出されたときにUpdatePreferencesメソッドでチェック/チェックされていない項目をこの配列に配置/削除できます。対応するリストビュー行も更新しますか?タイトルを変更しますか? –
チェックされているチェックボックスの一覧だけではありませんが、このチェックボックスからタグを取得できるはずです。あなたはクラスの配列を作成することを意味しますか? – CBeTJlu4ok