我正在显示自定义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);
...
}
在活动中,我从每个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];
}
答案 0 :(得分:1)
我认为您需要在此处发布ChannelAdapter
代码以获得准确答案,但据我了解,您尝试在ProfilePictureView
课程中实例化ChannelRow
,而它应该在ChannelAdapter
课程中完成。这样,您可以在适配器中调用findViewById()
并使用ProfilePicutreView
执行任何操作。
修改强>
尝试从profilePic
课程中删除ChannelRow
字段,我认为这可能会造成各种麻烦。你也可以发布带有异常堆栈跟踪的logcat输出吗?