CheckedTextView未选中 [英] CheckedTextView not checked
问题描述
我想要一个Multiselected (checked) listview
.当我选择一个项目时,绿色的复选标记必须出现.为此,我使用CheckedTextViews
.
ListView
从数据库获取数据.我为此使用SimpleCursorAdapter
.
当您单击按钮时,选定的条目(ID)将传递到下一个活动.
I want a Multiselected (checked) listview
. When I select an item then the green check mark must appear. For this I use CheckedTextViews
.
The ListView
gets the data from the database. I'm using a SimpleCursorAdapter
for that.
When you click on the button then the selected entries(IDs) will be passed to the next activity.
我的问题是CheckedTextView
的复选标记没有出现.但是,这些ID将传递到下一个活动.
我究竟做错了什么?如何解决?
My problem is that the check marks of the CheckedTextView
does not appear. But the IDs will be passed to the next activity.
What am I doing wrong? How to fix it?
selecttest.xml
selecttest.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spinner_select_language"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="@style/btn_Font"
android:id="@+id/selecttest_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner_test"
android:onClick="onClick"
android:text="@string/selecttest_start" />
<ListView
android:id="@+id/lv_lesson"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fadeScrollbars="false"
android:choiceMode="multipleChoice"
android:layout_alignParentLeft="true"
android:cacheColorHint="#00000000"
android:layout_below="@+id/selecttest_start" >
</ListView>
</RelativeLayout>
dataset_ctv_lesson.xml
dataset_ctv_lesson.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckedTextView
style="@style/tv_Font"
android:id="@+id/ctv_lesson"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/none"
android:checkMark="@drawable/ctv_state_checker"
/>
</RelativeLayout>
ctv_state_checker.xml
ctv_state_checker.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/btn_check_buttonless_off" />
<item android:state_checked="true"
android:drawable="@drawable/btn_check_buttonless_on" />
</selector>
SelectTestActivity.java
SelectTestActivity.java
public class SelectTestActivity
extends Activity
implements OnItemSelectedListener
{
Database db;
SimpleCursorAdapter adaptercursor, lv_adaptercursor;
ListView lv_lesson;
// Arraylist for checked item in the lesson view
ArrayList<String> checkedlessons = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.selecttest);
// Create Database
db = new Database(this);
// Drop-Down-Menu to select a language
Spinner spinner_language = (Spinner) findViewById(R.id.spinner_select_language);
spinner_language.setOnItemSelectedListener(this);
Cursor cursor = db.createListViewCursor();
String[] displaycolumn = new String[]{"language"};
int[] displayview = new int[] {R.id.tv_language};
adaptercursor = new SimpleCursorAdapter(this, R.layout.datasetlanguage, cursor, displaycolumn, displayview, 0);
spinner_language.setAdapter(adaptercursor);
// ListView to select a lesson
lv_lesson = (ListView) findViewById(R.id.lv_lesson);
cursor = db.createListViewLessonCursor(getSelectedItemIDFromSpinnerLanguage());
displaycolumn = new String[]{"lesson"};
int[] displayview2 = new int[] {R.id.ctv_lesson};
lv_adaptercursor = new SimpleCursorAdapter(this, R.layout.dataset_ctv_lesson, cursor, displaycolumn, displayview2, 0);
lv_lesson.setAdapter(lv_adaptercursor);
lv_lesson.setOnItemClickListener(new ListView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (!checkedlessons.contains(Long.toString(id)))
{
checkedlessons.add(Long.toString(id));
// checked textview
lv_lesson.setItemChecked(position, true);
}
else
{
checkedlessons.remove(Long.toString(id));
// unchecked textview
lv_lesson.setItemChecked(position, false);
}
}
});
// Close the database
db.close();
}
推荐答案
- 尝试删除CheckedTextView的样式,我认为您的样式中的某些值会影响外观.
- 在dataset_ctv_lesson.xml中删除RelativeLayout,并且不需要更改单击的项目的检查状态. ListView可以自己维护检查状态.使用ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)启用多选模式,并使用ListView.getCheckedItemPositions()获取选中的行位置.
这篇关于CheckedTextView未选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!