Android CheckedTextView - Modifica dello stato controllato al clic
-
30-09-2019 - |
Domanda
Ho un listview che deve essere scelta multipla (cioè ogni elemento della lista ha una casella di controllo che può essere controllato / incontrollato) La vista elenco è in un tabhost ed è il contenuto di teh prima scheda.
Il mio set up è in questo modo:
La mia scheda è impostato con:
TabSpec tab = tabHost.newTabSpec("Services");
tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Services").setContent(new Intent(this, ServiceList.class)));
Quando si avvia la scheda viene cliccato nuovo servicelist attività
servicelist è definito come tale:
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();
}
}
}
Quindi, tutto funziona bene fino a quando clicco sul mio elemento dell'elenco per modificare lo stato casella di controllo.
La parte di teh set di codici per gli eventi Fare clic sulla maniglia mi sta causando problemi:
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);
}
La mia comprensione è che la vista v passato al metodo onListItemClick reperesents mio elemento lista, quindi sto cercando di fusione v come CheckedTextView e set teh controllata valore su true, tuttavia questo fa sì che solo la mia app in crash. Mi sto perdendo qualcosa di semplice qui, o c'è un modo più semplice per fare questo?
Grazie
Kevin
Soluzione
Hai debug per controllare se 'v' è nullo? Se è così, è necessario utilizzare un layoutInflator per recuperare la vista.
if(v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.service_list, null);
}