Android ListView中的自定义RelativeLayout类

Android ListView中的自定义RelativeLayout类,android,android-listview,android-relativelayout,android-custom-view,android-cursoradapter,Android,Android Listview,Android Relativelayout,Android Custom View,Android Cursoradapter,我在我的自定义游标适配器中关注这篇博客文章 在newView方法中,我创建了一个自定义RelativeLayout类,并在bindView中填充数据 问题是这些方法从未被调用过。我正确设置了setListAdapter,因为如果我实现了ViewHolder模式,列表将正确填充 实施。代码如下: public class SearchUserListViewRow extends RelativeLayout{ public ImageView avatar = null; pub

我在我的自定义游标适配器中关注这篇博客文章 在newView方法中,我创建了一个自定义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
在基础游标为空的情况下可能不会被调用。 先检查一下这个

第二点,您的代码不代表ViewHolder模式,因为它的主要思想是在视图标记中存储对项目视图的引用。您应该创建/扩展一个新项,并在调用
findViewById()
将返回的值放入holder实例后执行。然后将holder实例保存到标记


最后,当您要将光标的值绑定到视图中时,请从标记中获取holder并填充,例如
setText()
setImage()
等等。

问题在于这些方法从未被调用-哪些方法<代码>新建视图和/或
绑定视图
?如果是,则再次检查您的
光标
,并确保其中有行。是的,如果我对viewholders执行相同的操作,则会有行。显示的列表和调用的newView/bindView我不太了解您面临的问题。尝试更好地解释有问题的行为。问题在于光标打开并正确填充,光标不是问题,ListView不显示任何行,newView/BindView方法从未用提供的源代码调用过,我不知道为什么ListView不显示任何行,newView/BindView方法从未用提供的源代码调用过,我不知道为什么-您确定在代码中使用了正确的适配器实例吗(如果使用
加载器
请确保在右侧适配器上设置数据)?另外,在您的
SearchUserListViewRow
中,您是否应该调用
init
方法或更新其中一个视图?
行。text
不会更新使用该字符串的视图。是的,我的光标有行。如果我使用ViewHolder模式实现相同的光标,则会显示列表,并调用newView/bindView。i想知道为什么我的代码不显示列表,并且使用ViewHolder yes。