Android CheckedTextView-クリック時にチェックステータスを変更します
-
30-09-2019 - |
質問
複数選択する必要があるリストビューがあります(つまり、各リスト項目にはチェック /チェックされていないチェックボックスがあります)リストビューはTabhostにあり、最初のタブのコンテンツです。
私のセットアップはそうです:
私のタブは以下でセットアップされています
TabSpec tab = tabHost.newTabSpec("Services");
tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Services").setContent(new Intent(this, ServiceList.class)));
タブがクリックされると、新しいアクティビティセリケリストが開始されます
ServiceListは次のように定義されています。
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();
}
}
}
したがって、チェックボックス状態を変更するためにリスト項目をクリックするまで、すべてが正常に機能します。
クリックイベントを処理するために設定されたTEHコードの部分は、私に問題を引き起こしています:
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);
}
私の理解では、onlistitemclickメソッドに渡されたView Vがリスト要素を繰り返しているので、VをCheckedTextViewとしてキャストし、tehのチェック値をTrueに設定しようとしていますが、これによりアプリがクラッシュするだけです。ここで簡単なものが足りないのですか、それとも簡単に行う方法はありますか?
ありがとう
ケビン
解決
「V」がnullかどうかを確認するためにデバッグしましたか?その場合、レイアウトインフレーターを使用してビューを取得する必要があります。
if(v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.service_list, null);
}
所属していません StackOverflow