これがStackOverFlowに関する私の最初の質問です。私は通常、自分で答えを見つけますが、ここで説明する奇妙な問題に本当に悩まされています。
フラグメントアクティビティにListViewを実装しました。このリストビューには、SQLLiteデータベースから取得した現在のレコードに関連するカテゴリのリストが含まれています。
すべて正常に機能しています。DBからデータを取得するためのSimpleCursorAdapterを作成し、ListViewにカテゴリを正しく表示します。この問題は、チェックボックスの事前入力(複数選択リスト)に関連しています。チェックボックスを事前にチェックする方法に応じて、次の2つのケースが発生します。
まず、チェックボックスは事前にチェックされていますが、チェックボックスをクリックして切り替えることはできません。次に、クリックするとチェックボックスが適切に切り替わりますが、事前にチェックされていません...
これが私が問題を抱えているコードの部分です:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.rate_fragment, container,false);
dbCategories = "";
displayCategories = resources.getText(R.string.no_categories).toString();
/** INITIALIZATION */
mViewSwitcher = (ViewSwitcher)v.findViewById(R.id.profileSwitcher);
/** Edition view */
rateGroup = (RadioGroup)v.findViewById(R.id.rate_group);
rateOne = (RadioButton)v.findViewById(R.id.one_button);
rateOne.setTag(1);
rateTwo = (RadioButton)v.findViewById(R.id.two_button);
rateTwo.setTag(2);
rateThree = (RadioButton)v.findViewById(R.id.three_button);
rateThree.setTag(3);
rateFour = (RadioButton)v.findViewById(R.id.four_button);
rateFour.setTag(4);
rateFive = (RadioButton)v.findViewById(R.id.five_button);
rateFive.setTag(5);
descET = (EditText)v.findViewById(R.id.editdescription);
descTextSize = descET.getTextSize();
descET.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
categoriesTV_edit = (TextView)v.findViewById(R.id.edit_categories);
categoriesBT = (Button) v.findViewById(R.id.select_categories);
categoriesBT.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
View categoriesListTitle = getActivity().getLayoutInflater().inflate(R.layout.category_list_title, null);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(v.getContext()).setCustomTitle(categoriesListTitle);
categories = db.getAllCategoriesByRate(currentRate);
categoriesList = new ListView(getActivity());
categoriesList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
categoriesList.setClickable(true);
String[] fromColumns = new String[] {
DatabaseHandler.CATEGORY_NAME
};
int[] toViews = new int[]{
R.id.cat_checked
};
//mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_multiple_choice, categories, fromColumns, toViews, 0);
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.category_item, categories, fromColumns, toViews, 0);
mAdapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == 1) {
CheckedTextView categRow = (CheckedTextView) view;
String catName = cursor.getString(1);
mAdapter.setViewText((TextView) view, catName);
int catChecked = cursor.getInt(2);
//boolean checkedCat = catChecked==1;
//categoriesList.setItemChecked(cursor.getPosition(),checkedCat);
categRow.setChecked(catChecked==1);
int catID = cursor.getInt(0);
categRow.setTag(catID);
return true;
}
else {
return false;
}
}
});
categoriesList.setAdapter(mAdapter);
alt_bld.setView(categoriesList);
いずれかのケースがあるためには、すべてがこれらの2行に依存します。
//boolean checkedCat = catChecked==1;
//categoriesList.setItemChecked(cursor.getPosition(),checkedCat);
コメントされている場合、チェックボックスは事前にチェックされていませんが、クリックの切り替えは機能しています。しかし、これらの行をコメントアウトすると、トグルは機能しなくなりますが、カテゴリは事前にチェックされています。
私も理解していないのは、この行が機能していないということです。
categRow.setChecked(catChecked==1);
しかし、これはうまく機能しています(私はタグを取得することに成功しました):
categRow.setTag(catID);
だから誰かが私が間違っていることを私に説明してくれることを願っています、私がここで誤解していることがあると思います...
注:カーソル「categories」から3つの列を取得します。最初の列はカテゴリのID、2番目の列は名前、3番目の列はステータス:チェック済みかどうか(1または0)です。
よろしくお願いします。