【发布时间】:2012-12-20 17:07:48
【问题描述】:
我在我的自定义 CursorAdapter 中关注这篇博文 InfamousViewHolder Pattern 在 newView 方法中,我创建了一个 Custom RelativeLayout 类,并在 bindView 中填充了数据。
问题在于这些方法从未被调用过。我正确设置了 setListAdapter,因为如果我实现 ViewHolder 模式,列表将正确填充。
实施。这里是代码:
public class SearchUserListViewRow extends RelativeLayout{
public ImageView avatar = null;
public TextView name = null;
public ToggleButton inviteButton = null;
public ToggleButton followButton = null;
public FoundUser user;
public SearchUserListViewRow(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.search_friend_row, this);
avatar = (ImageView) findViewById(R.id.avatar);
name = (TextView) findViewById(R.id.user_name);
inviteButton = (ToggleButton) findViewById(R.id.invite_button);
followButton = (ToggleButton) findViewById(R.id.follow_button);
}
public void init(Cursor cursor) {
user = UsersProcessor.toFoundUser(cursor);
name.setText(user.alias);
inviteButton.setChecked(user.invited);
inviteButton.setVisibility(Section.TO_INVITE.equals(user.section) ?
View.VISIBLE : View.GONE);
inviteButton.setTag(user);
followButton.setEnabled(!user.invited);
followButton.setChecked(user.isGuru);
followButton.setVisibility(Section.TO_FOLLOW.equals(user.section) ?
View.VISIBLE : View.GONE);
followButton.setTag(user);
}
}
和适配器:
public class UserCursorAdapter extends CursorAdapter {
public UserCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = new SearchUserListViewRow(context);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
SearchUserListViewRow row = (SearchUserListViewRow)v;
//ViewHolder like stuff
row.text = "foo";
}
}
和 serarch_friend_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/txt_box"
android:orientation="horizontal"
android:layout_marginTop="-3px"
android:padding="@dimen/padding_row" >
<ImageView
android:id="@+id/avatar"
android:layout_width="@dimen/image_user_avatar"
android:layout_height="@dimen/image_user_avatar"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<FrameLayout
android:id="@+id/button_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<ToggleButton
android:id="@+id/follow_button"
android:visibility="gone" />
<ToggleButton
android:id="@+id/invite_button"0000000000000000000
android:visibility="gone" />
</FrameLayout>
<TextView
android:id="@+id/user_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/content_padding_normal"
android:layout_toLeftOf="@id/button_container"
android:layout_toRightOf="@id/avatar"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="@dimen/text_size_medium"
android:textStyle="bold" />
</RelativeLayout>
提前致谢。
【问题讨论】:
-
问题在于这些方法从未被调用过 - 哪些方法?
newView和/或bindView?如果是,请仔细检查您的Cursor并确保其中包含行。 -
是的,如果我用它显示的列表和调用 newView/bindView 的视图实现相同的行,它就会有行
-
我不太明白你面临什么问题。尝试更好地解释有问题的行为。
-
问题在于光标打开并正确填充,光标不是问题,ListView 不显示任何行,并且方法 newView/BindView 从未使用提供的源代码调用,我不知道为什么
-
ListView 不显示任何行,并且 newView/BindView 方法从未使用提供的源代码调用,我不知道为什么 - 你确定你使用代码中的正确适配器实例(如果使用
Loader确保您在正确的适配器上设置数据)?此外,在您的SearchUserListViewRow中,您不应该调用init方法或更新其中一个视图吗?row.text不会更新使用该字符串的视图。
标签: android android-listview android-relativelayout android-custom-view android-cursoradapter