I'm trying to code a BaseAdapter with a CheckBox that will enable to select/unselect several items. When an intem is selected/unselected, I want to perform a specific action. I am using an OnCheckedChangedListener because I could not make the OnItemClickListener work (the OnItemClick() method was never called). Now I can see the onCheckedChanged() method called on my logcat but I can't uncheck an Item and even if I click on an item several times, the method onCheckedChanged() is called only once. Please note that this code I use to add a new menu (row) in the Settings menu of the Android system (may be this introduces some limitations that I am not aware of). Here is my code:
File nfc_routing_overflow_option.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://ift.tt/jtTJvY
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/nfc_routing_overflow"
android:focusable="true"
android:clickable="false"
android:orientation="horizontal"
android:paddingLeft="24dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="?android:attr/selectableItemBackground">
<CheckBox xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/buttonOther"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checked="false"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
<ImageView
android:id="@+id/bannerOther"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:scaleType="centerInside"
/>
<TextView
android:id="@+id/textOther"
android:layout_width="wrap_content"
android:layout_height="64dp"
/>
</LinearLayout>
File nfc_routing_overflow.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
* Copyright 2012, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://ift.tt/jtTJvY
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<ListView
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/overflow_list"
android:paddingTop="16dp"
android:paddingBottom="16dp"/>
</LinearLayout>
File NfcRoutingOverflow.java:
package com.android.settings.nfc;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.R;
import android.util.Log;
import com.android.settings.SettingsPreferenceFragment;
import android.nfc.NfcAdapter;
import android.nfc.cardemulation.ApduServiceInfo;
import android.nfc.cardemulation.CardEmulation;
import android.nfc.cardemulation.AidGroup;
import android.graphics.drawable.Drawable;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ListView;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.AdapterView;
import android.view.View.OnClickListener;
public class NfcRoutingOverflow extends SettingsPreferenceFragment{
public static final String TAG = "SETTINGSNfcRoutingOverflow";
static final boolean DBG = true;
private NfcAdapter mAdapter;
private CardEmulation mCardEmuManager;
private Context mContext;
private LayoutInflater mLayoutInflater;
private NfcOverflowAdapter mOverflowAdapter;
private ListView mListView;
class NfcOtherOverflow
{
CharSequence label;
CharSequence description;
Drawable banner;
public ComponentName componentName;
public AidGroup aidGroup;
public int uid;
boolean selected = false;
}
@Override
protected int getMetricsCategory() {
return MetricsLogger.NFC_PAYMENT;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (DBG) Log.d(TAG, "onCreate()" );
mContext = getActivity();
mAdapter = NfcAdapter.getDefaultAdapter(mContext);
mCardEmuManager = CardEmulation.getInstance(mAdapter);
mLayoutInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
mOverflowAdapter = new NfcOverflowAdapter();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (DBG) Log.d(TAG, "onViewCreated()" );
ViewGroup contentRoot = (ViewGroup) getListView().getParent();
View emptyView = getActivity().getLayoutInflater().inflate(R.layout.nfc_routing_overflow, contentRoot, false);
contentRoot.addView(emptyView);
PackageManager pm = mContext.getPackageManager();
List<ApduServiceInfo> serviceInfos =
mCardEmuManager.getServices(CardEmulation.CATEGORY_OTHER);
if (DBG) Log.d(TAG, "onViewCreated() - Nb of services in category OTHER found: " + serviceInfos.size());
mListView = (ListView) view.findViewById(R.id.overflow_list);
ArrayList<NfcOtherOverflow> appList = new ArrayList<NfcOtherOverflow>();
for(ApduServiceInfo service:serviceInfos)
{
NfcOtherOverflow appOther = new NfcOtherOverflow();
ArrayList<AidGroup> aidGroups = service.getAidGroups();
if (DBG)
Log.d(TAG, "onViewCreated() - Processing service: " + service.getDescription() + ", with " + aidGroups.size() + " AidGroup");
if(aidGroups.size() > 0)
{
for(AidGroup aidGroup : aidGroups)
{
appOther.label = service.loadLabel(pm);
if (appOther.label == null)
{
appOther.label = service.loadAppLabel(pm);
}
appOther.componentName = service.getComponent();
appOther.description = service.getDescription();
appOther.banner = service.loadBanner(pm);
appOther.aidGroup = aidGroup;
appOther.uid = service.getUid();
appList.add(appOther);
}
}
}
if (appList.size() != 0)
{
NfcOtherOverflow[] apps = appList.toArray(new NfcOtherOverflow[appList.size()]);
mOverflowAdapter.updateApps(apps, null);
}
mListView.setAdapter(mOverflowAdapter);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (DBG) Log.d(TAG, "onCreateOptionsMenu()" );
}
class NfcOverflowAdapter extends BaseAdapter implements OnCheckedChangeListener{
// Only modified on UI thread
private NfcOtherOverflow[] appInfos;
public NfcOverflowAdapter() {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - constructor" );
}
public void updateApps(NfcOtherOverflow[] appInfos, NfcOtherOverflow currentDefault) {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - updateApps() - Nb apps: " + appInfos.length);
// Clone app infos, only add those with a banner
this.appInfos = appInfos;
// notifyDataSetChanged();
}
@Override
public int getCount() {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - getCount()" );
return appInfos.length;
}
@Override
public NfcOtherOverflow getItem(int i) {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - getItem()" );
return appInfos[i];
}
@Override
public long getItemId(int i) {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - getItemId()" );
return appInfos[i].componentName.hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - getView() - position: " + position);
final ViewHolder holder;
NfcOtherOverflow appInfo = appInfos[position];
if (convertView == null)
{
convertView = mLayoutInflater.inflate(R.layout.nfc_routing_overflow_option, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.bannerOther);
holder.radioButton = (CheckBox) convertView.findViewById(R.id.buttonOther);
holder.textView = (TextView) convertView.findViewById(R.id.textOther);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.radioButton.setContentDescription(appInfo.label);
holder.radioButton.setTag(appInfo);
holder.radioButton.setOnCheckedChangeListener(this);
if(appInfo.banner != null)
{
holder.imageView.setImageDrawable(appInfo.banner);
}
else
{
Drawable dummy = mContext.getDrawable(R.drawable.ic_settings_nfc_payment);
holder.imageView.setImageDrawable(dummy);
}
holder.imageView.setTag(appInfo);
holder.imageView.setContentDescription(appInfo.label);
holder.textView.setContentDescription(appInfo.label);
holder.textView.setText(appInfo.aidGroup.getDescription());
AidGroup aidGroup = appInfo.aidGroup;
boolean isEnabled = aidGroup.isGroupEnabled();
if(isEnabled == true)
{
holder.radioButton.setChecked(true);
}
else
{
holder.radioButton.setChecked(false);
}
return convertView;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (DBG) Log.d(TAG, "NfcOverflowAdapter - onCheckedChanged() - isChecked: " + isChecked);
NfcOtherOverflow appInfo = (NfcOtherOverflow) buttonView.getTag();
AidGroup aidGroup = appInfo.aidGroup;
if (DBG) Log.d(TAG, "NfcOverflowAdapter - onClick() - AidGroup description: " + aidGroup.getDescription());
aidGroup.setIsEnabled(isChecked);
notifyDataSetChanged();
}
public class ViewHolder {
public CheckBox radioButton;
public ImageView imageView;
public TextView textView;
}
public void updateRouting(View view)
{
if (DBG) Log.d(TAG, "NfcOverflowAdapter - updateRouting()" );
NfcOtherOverflow appInfo = (NfcOtherOverflow) view.getTag();
AidGroup aidGroup = appInfo.aidGroup;
if (DBG) Log.d(TAG, "NfcOverflowAdapter - onCheckedChanged() - AidGroup description: " + aidGroup.getDescription());
boolean isEnabled = true;
if(((CheckBox)view).isChecked() == false)
{
isEnabled = false;
}
aidGroup.setIsEnabled(isEnabled);
//Inform system of changes
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PACKAGE_CHANGED);
intent.putExtra(Intent.EXTRA_UID, appInfo.uid);
intent.putExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST, appInfo.componentName);
mContext.sendBroadcast(intent);
// /notifyDataSetChanged();
}
}
}
Could someone please help me figure out what's wrong with my code?
NOTE: The method updateRouting() of the NfcOverflow Class is not used for now.
Aucun commentaire:
Enregistrer un commentaire