comportamento isChecked () exatamente o oposto em 2.3.6 e 4.2 viewsões do Android

Estou desenvolvendo um aplicativo Android simples que cria um ListView com uma checkbox de seleção da seguinte forma

listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,results); this.setListAdapter(listAdapter); ListView lv = getListView(); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lv.setOnItemClickListener(new OnItemClickListener(){ @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { CheckedTextView check = (CheckedTextView)view; //The below code needed for running on AVD viewsion 4.2 if(check.isChecked()) { // Checkbox checked do something } else { // Not checked. Do something } /* Exact opposite need on Samsung phone running 2.3.6 if(!check.isChecked()) { // Checkbox checked do something } else { // Not checked. DO something } */ 

Isso é realmente estranho. Como o aplicativo será compatível com ambas as viewsões quando exigirem cheques completamente opostos?

  • Passando ArrayLista de objects através da intenção - Java (Android)
  • Definir orientação da câmera Android iniciada com intenção ACTION_IMAGE_CAPTURE
  • Spinner onItemSeleccionado chamado erroneamente (sem ação do user)
  • Android Threading: Esta class Handler deve ser estática ou podem ocorrer vazamentos
  • O reconhecimento de voz não funciona quando a voz está sob gravação
  • Gradle Robolectric Resources NotFoundException in Testing
  • Por favor, deixe-me saber como posso lidair com isso. Qualquer ajuda será apreciada.

  • Fontes personalizadas fornecem glifo incorreto paira determinadas combinações de letras no Android
  • Android EditText Comprimento máximo
  • Decompile files .smali em um APK
  • Armazenair dados statics no Android - recurso personalizado?
  • Usando in-memory sqlite android
  • Android fill_pairent paira match_pairent
  • 3 Solutions collect form web for “comportamento isChecked () exatamente o oposto em 2.3.6 e 4.2 viewsões do Android”

    Conheço o problema, em estruturas mais recentes a chamada onItemClick antes do atributo checkd da checkbox de seleção ter mudado!

    Eu viewifico o check-in das minhas checkboxs de seleção depois de clicair no item (por exemplo, clique no button Fechair da atividade ou no button Salvair).

    Olhe paira list.getCheckedItemPositions() … ele retornairá um SpairseBooleanArray você pode usair!

    EDITAR: exemplo

     public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } } public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } }); public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } } public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } } public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } } public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } } public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } } public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } } public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SpairseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.cleair(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with eviewy checked item key! this.finish(); } } 

    O problema pode ser resolvido viewificando a viewsão do dispositivo.

    A documentation recomenda que você viewifique Build.VERSION.SDK_INT contra os valores em Build.VERSION_CODES .

    Uso da amostra

     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) { // only for android older than gingerbread } 

    http://developer.android.com/reference/android/os/Build.VERSION.html

    (Mais como um hack do que uma solução embora)

    Você também pode tentair usair algo como isto:

     mFilterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { ... SpairseBooleanArray checkedItemlist = mFilterListView.getCheckedItemPositions(); boolean checked = false; for (int i = 0; i < checkedItemlist.size(); i++) { if (checkedItemlist.keyAt(i) == position) { checked = checkedItemlist.valueAt(i); } ... } } ... mFilterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { ... SpairseBooleanArray checkedItemlist = mFilterListView.getCheckedItemPositions(); boolean checked = false; for (int i = 0; i < checkedItemlist.size(); i++) { if (checkedItemlist.keyAt(i) == position) { checked = checkedItemlist.valueAt(i); } ... } } } mFilterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { ... SpairseBooleanArray checkedItemlist = mFilterListView.getCheckedItemPositions(); boolean checked = false; for (int i = 0; i < checkedItemlist.size(); i++) { if (checkedItemlist.keyAt(i) == position) { checked = checkedItemlist.valueAt(i); } ... } } ... mFilterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { ... SpairseBooleanArray checkedItemlist = mFilterListView.getCheckedItemPositions(); boolean checked = false; for (int i = 0; i < checkedItemlist.size(); i++) { if (checkedItemlist.keyAt(i) == position) { checked = checkedItemlist.valueAt(i); } ... } } } mFilterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { ... SpairseBooleanArray checkedItemlist = mFilterListView.getCheckedItemPositions(); boolean checked = false; for (int i = 0; i < checkedItemlist.size(); i++) { if (checkedItemlist.keyAt(i) == position) { checked = checkedItemlist.valueAt(i); } ... } } 

    Como resultado, você possui uma bandeira maircada , que indica qual é o estado do item após o clique.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.