Android CheckedTextView - Cambio de estado desprotegido al hacer clic
-
30-09-2019 - |
Pregunta
Tengo una vista de lista que debe ser de elección múltiple (es decir, cada elemento de la lista tiene una casilla de verificación que se puede comprobar / desactivada) La vista de lista se encuentra en una tabhost y es el contenido de teh primera pestaña.
Mi puesta a punto es así:
Mi pestaña está configurado con:
TabSpec tab = tabHost.newTabSpec("Services");
tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Services").setContent(new Intent(this, ServiceList.class)));
se inicia Cuando se hace clic en la pestaña nueva ServiceList actividad
ServiceList se define como tal:
public class ServiceList extends ListActivity{
private EscarApplication application;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service_list);
ServiceList.this.application = (EscarApplication) this.getApplication();
final ListView listView = getListView();
}
protected void onListItemClick(ListView l, View v, int position, long id) {
String.valueOf(id);
Long.toString(id);
((CheckedTextView) v).setChecked(true);
super.onListItemClick(l, v, position, id);
}
@Override
public void onStart() {
super.onStart();
//Generate and display the List of visits for this day by calling the AsyncTask
GenerateServiceList services = new GenerateServiceList();
int id = ServiceList.this.application.getVisitId();
services.execute(id);
listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
//The AsyncTask to generate a list
private class GenerateServiceList extends AsyncTask<Integer, String, Cursor> {
// can use UI thread here
protected void onPreExecute() {
}
// automatically done on worker thread (separate from UI thread)
protected Cursor doInBackground(Integer...params) {
int client_id = params[0];
ServiceList.this.application.getServicesHelper().open();
Cursor cur = ServiceList.this.application.getServicesHelper().getPotentialVisitServices(client_id);
return cur;
}
// can use UI thread here
protected void onPostExecute(Cursor cur){
startManagingCursor(cur);
// the desired columns to be bound
String[] columns = new String[] {ServicesAdapter.KEY_SERVICE};
// the XML defined views which the data will be bound to
int[] to = new int[] {R.id.display_service};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(ServiceList.this, R.layout.service_list_element, cur, columns, to);
// set this adapter as your ListActivity's adapter
ServiceList.this.setListAdapter(mAdapter);
ServiceList.this.application.getServicesHelper().close();
}
}
}
Por lo tanto, todo funciona bien hasta que haga clic en mi elemento de la lista para cambiar el estado casilla de verificación.
La parte de teh conjunto de códigos para manejar eventos de clic me está causando problemas:
protected void onListItemClick(ListView l, View v, int position, long id) {
String.valueOf(id);
Long.toString(id);
((CheckedTextView) v).setChecked(true);
super.onListItemClick(l, v, position, id);
}
Mi entendimiento es que la vista v pasado al método onListItemClick reperesents mi elemento de la lista, por lo que estoy tratando de V CAST como un conjunto CheckedTextView y teh comprueba el valor de verdad, sin embargo esto sólo hace que mi aplicación se bloquee. Me estoy perdiendo algo simple aquí, o hay una manera más fácil de hacer esto?
Gracias
Kevin
Solución
¿Ha depurado para comprobar si 'v' es nulo? Si es así, es necesario utilizar un layoutInflator para recuperar la vista.
if(v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.service_list, null);
}