将 ProfilePictureView 添加到 ListView 中的每一行

Add ProfilePictureView to each row in a ListView

我正在显示一个自定义 ListView,其中每行包含一个 ProfilePictureView。我想从 Facebook 抓取用户的个人资料图片来显示它。但是,默认的 Facebook 个人资料图片仅显示。我认为发生错误是因为我没有在 XML 元素上调用 findViewById,但是我不知道该怎么做。

Java 每行代码

public class ChannelRow { 

private ProfilePictureView profilePic;
private String userName;
private String channelName;
private String publisherToken;
private String sessionID;

public ChannelRow(String userName, String channelName, String publisherToken, String sessionID,
                  String profilePicId, Context context){
    this.profilePic = new ProfilePictureView(context);
    this.userName = userName;
    this.channelName = channelName;
    this.publisherToken = publisherToken;
    this.sessionID = sessionID;
    profilePic.setProfileId(profilePicId);
...
}

这里activity,我从每个ParseObject中抓取用户的信息,并设置在ChannelRow

    private void createSessionsList(ArrayList<ParseObject> objects) {
    ArrayList<ChannelRow> channels = new ArrayList<>();
    ChannelRow newRow;

    for (ParseObject o : objects) {
        newRow = new ChannelRow((String) o.get("hostName"), (String) o.get("chatTitle"),
                (String) o.get("publisherToken"), (String) o.get("sessionID"),
                (String) o.get("facebookId"), getApplicationContext());
        channels.add(newRow);
    }

    channelData = channels.toArray(new ChannelRow[channels.size()]);
    adapter = new ChannelAdapter(this, R.layout.channel_row, channelData);
    adapter.notifyDataSetChanged();
    channelListView.setAdapter(adapter);

}

这是每行的 XML 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.facebook.widget.ProfilePictureView
    android:id="@+id/profile_picture"
    android:layout_width="100dp"
    android:layout_height="100dp" />
....
</LinearLayout>

这是 ListView 的适配器

    private Context context;
private int layoutResourceId;
private ChannelRow[] data;

public ChannelAdapter(Context context, int layoutResourceId, ChannelRow[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}
    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ChannelRowHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ChannelRowHolder();
        holder.userName = (TextView)row.findViewById(R.id.userNameTextView);
        holder.channelName = (TextView)row.findViewById(R.id.channelNameTextView);
        holder.profilePic = (ProfilePictureView) row.findViewById(R.id.profile_picture);

        row.setTag(holder);
    }
    else
    {
        holder = (ChannelRowHolder)row.getTag();
    }

    ChannelRow channelRow = data[position];
    holder.userName.setText(channelRow.getUserName());
    holder.channelName.setText(channelRow.getChannelName());
    holder.profilePic.setProfileId(channelRow.getProfileId());

    return row;
}

static class ChannelRowHolder
{
    ProfilePictureView profilePic;
    TextView userName;
    TextView channelName;
}

    @Override
public int getCount(){
    return data.length;
}

@Override
public ChannelRow getItem(int pos){
    return data[pos];
}

我认为您需要 post 您的 ChannelAdapter 代码以获得精确的答案,但据我了解,您正试图在 [=12] 中实例化 ProfilePictureView =] class,而应该在 ChannelAdapter class 中完成。这样你就可以在适配器中调用 findViewById() 并用 ProfilePicutreView.

做任何你想做的事

编辑

尝试从 ChannelRow class 中删除 profilePic 字段,我认为这可能会引起各种麻烦。您还可以 post logcat 输出异常堆栈跟踪吗?