【发布时间】:2012-09-20 10:16:46
【问题描述】:
我正在自定义 ListView 中加载电话联系人。每行都是一个可检查的 LinearLayout,其中包含一个 CheckedTextView 和另一个 TextView。
我正在使用自定义 ArrayAdapter 提供列表视图。我的问题是我无法在 getView() 中控制 CheckedTextViews。例如,当我尝试以下操作时
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null){
row = inflater.inflate(layout, parent, false);
}
CheckedTextView checkedTextView = (CheckedTextView) row.findViewById(R.id.checkedTextView);
checkedTextView.setText("A");
checkedTextView.setChecked(true);
return row;
}
每当我滚动列表视图时,它应该检查每个文本视图,但这并没有发生。谁能告诉我怎么做?
编辑:在 getView() 中检查很重要,我不能在 setListAdapter() 之后检查所有内容
EDIT2:这是显示每一行视图的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<com.example.multiplecontacts.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckedTextView
android:id="@+id/checkedTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingBottom="0dp"
android:text="CheckedTextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/subTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Small Text"
android:paddingTop="0dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</com.example.multiplecontacts.CheckableLinearLayout>
CheckableLinearLayout 是一个自定义布局,它扩展了 LinearLayout 并实现了我之前所说的 Checkable。我已经从here 拿走了它
【问题讨论】:
-
我不需要默认勾选。实际上上面的代码不是我的实际代码,它只是一个演示我的问题的示例。我所需要的只是能够在 getView() 中切换 CheckedTextView
-
你有
checkbox的布局但是当你按下checkbox时没有检查? -
在 Alex Orlov 的良好反应之后,我这样做了:
public View getView(int position, View convertView, ViewGroup parent) {..... ((ListView) parent).setItemChecked(position, true);....}
标签: android android-layout android-spinner android-checkbox