- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经这样做有一段时间了,但我不断收到错误:E/RecyclerView: No adapter attached; skipping layout
。
我开始更改调用 setAdapter 的 ViewProfilePostsFragment 中的内容,但我没有成功使其工作。我似乎无法弄清楚为什么会发生这种情况。
这是我的脚本,以便您可以告诉我我做错了什么,其中一个脚本调用 setAdapter 方法,另一个脚本调用 RecyclerAdapter 脚本。
ViewProfilePostsFragment.java
public class ViewProfilePostsFragment extends Fragment{
private static final String TAG = "ViewProfilePostsFragmen";
private ArrayList<Photo> mPhotos;
private ArrayList<String> mFriends;
private RecyclerView mRecView;
private ViewProfilePostsAdapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile_posts, container, false);
mRecView = (RecyclerView) view.findViewById(R.id.lvProfilePosts);
mFriends = new ArrayList<>();
mPhotos = new ArrayList<>();
getFriends();
return view;
}
private void getFriends(){
Log.d(TAG, "getFollowing: searching for FRIENDS **************************************");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(getString(R.string.dbname_friends))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
mFriends.add(singleSnapshot.child(getString(R.string.field_user_id)).getValue().toString());
}
//get the photos
getPhotos();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void getPhotos(){
Log.d(TAG, "getPhotos: getting photos");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
for(int i = 0; i < mFriends.size(); i++){
final int count = i;
Query query = reference
.child(getString(R.string.dbname_user_photos))
.child(mFriends.get(i))
.orderByChild(getString(R.string.field_user_id))
.equalTo(mFriends.get(i));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Photo photo = new Photo();
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get(getString(R.string.field_caption)).toString());
photo.setTags(objectMap.get(getString(R.string.field_tags)).toString());
photo.setPhoto_id(objectMap.get(getString(R.string.field_photo_id)).toString());
photo.setUser_id(objectMap.get(getString(R.string.field_user_id)).toString());
photo.setDate_created(objectMap.get(getString(R.string.field_date_created)).toString());
photo.setImage_path(objectMap.get(getString(R.string.field_image_path)).toString());
mPhotos.add(photo);
}
if(count >= mFriends.size() -1){
//display our photos
displayPhotos();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
private void displayPhotos(){
if(mPhotos != null){
Collections.sort(mPhotos, new Comparator<Photo>() {
@Override
public int compare(Photo o1, Photo o2) {
return o2.getDate_created().compareTo(o1.getDate_created());
}
});
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecView.setLayoutManager(llm);
mAdapter = new ViewProfilePostsAdapter(getActivity(), mPhotos);
mRecView.setAdapter( mAdapter );
}
}
ViewProfilePostsAdapter.java
public class ViewProfilePostsAdapter extends RecyclerView.Adapter<ViewProfilePostsAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter";
//private int[] getItemIn;
private LayoutInflater mInflater;
//private int mLayoutResource;
private Context mContext;
private DatabaseReference mReference;
private String currentUsername;
private ArrayList<Photo> list;
public static class ViewHolder extends RecyclerView.ViewHolder {
//private final TextView textView;
ImageView mProfileImage;
String likesString;
TextView username, timeDelta, caption, likes, liked;
ImageView postImage;
ImageView heartRed, heartWhite;
UserAccountSettings settings = new UserAccountSettings();
User user = new User();
StringBuilder users;
String mLikesString;
boolean likeByCurrentUser;
Heart heart;
GestureDetector detector;
Photo photo;
public ViewHolder(View convertView) {
super(convertView);
username = (TextView) convertView.findViewById(R.id.display_name);
postImage = (ImageView) convertView.findViewById(R.id.post_image);
caption = (TextView) convertView.findViewById(R.id.image_caption);
timeDelta = (TextView) convertView.findViewById(R.id.image_time_posted);
likes = (TextView) convertView.findViewById(R.id.tvLikesString);
mProfileImage = (ImageView) convertView.findViewById(R.id.f_profile_image);
heartRed = (ImageView) convertView.findViewById(R.id.image_heart_red);
heartWhite = (ImageView) convertView.findViewById(R.id.image_heart_white);
heart = new Heart(heartWhite, heartRed);
liked = (TextView) convertView.findViewById(R.id.likeText);
}
}
public ViewProfilePostsAdapter(Context context, ArrayList<Photo> photo) {
this.mContext = context;
this.list = photo;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view.
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = mInflater.inflate(R.layout.layout_post_listitem, null);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
Log.d(TAG, "Element " + position + " set.");
mReference = FirebaseDatabase.getInstance().getReference();
viewHolder.photo = getItem(position);
viewHolder.detector = new GestureDetector(mContext, new GestureListener(viewHolder));
getCurrentUsername();
getLikesString(viewHolder);
viewHolder.caption.setText(getItem(position).getCaption());
String timestampDifference = getTimestampDifference(getItem(position));
if(!timestampDifference.equals("0")){
viewHolder.timeDelta.setText(timestampDifference + " DAYS AGO");
}else{
viewHolder.timeDelta.setText("TODAY");
}
final ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(getItem(position).getImage_path(), viewHolder.postImage);
//get the profile image and username
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_user_account_settings))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e(TAG, "onDataChange: OnDataChange ******************************************************************************");
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.e(TAG, "onDataChange: found user: "
+ singleSnapshot.getValue(UserAccountSettings.class).getUsername() + "****************************");
viewHolder.username.setText(singleSnapshot.getValue(UserAccountSettings.class).getUsername());
imageLoader.displayImage(singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
viewHolder.mProfileImage);
viewHolder.settings = singleSnapshot.getValue(UserAccountSettings.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Query userQuery = mReference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found user: " +
singleSnapshot.getValue(User.class).getUsername());
viewHolder.user = singleSnapshot.getValue(User.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public Photo getItem(int pos){
return list.get(pos);
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener{
ViewHolder mHolder;
public GestureListener(ViewHolder holder) {
mHolder = holder;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG, "onDoubleTap: double tap detected.");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_photos))
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
String keyID = singleSnapshot.getKey();
//case1: Then user already liked the photo
if(mHolder.likeByCurrentUser &&
singleSnapshot.getValue(Like.class).getUser_id()
.equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
mReference.child(mContext.getString(R.string.dbname_photos))
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(mHolder.photo.getUser_id())
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
mHolder.heart.toggleLike();
getLikesString(mHolder);
}
//case2: The user has not liked the photo
else if(!mHolder.likeByCurrentUser){
//add new like
addNewLike(mHolder);
break;
}
}
if(!dataSnapshot.exists()){
//add new like
addNewLike(mHolder);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return true;
}
}
private void addNewLike(final ViewHolder holder){
Log.d(TAG, "addNewLike: adding new like");
String newLikeID = mReference.push().getKey();
Like like = new Like();
like.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(like);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(holder.photo.getUser_id())
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(like);
holder.heart.toggleLike();
getLikesString(holder);
}
private void getCurrentUsername(){
Log.d(TAG, "getCurrentUsername: retrieving user account settings");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
currentUsername = singleSnapshot.getValue(UserAccountSettings.class).getUsername();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void getLikesString(final ViewHolder holder){
Log.d(TAG, "getLikesString: getting likes string");
try{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_photos))
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
holder.users = new StringBuilder();
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(singleSnapshot.getValue(Like.class).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found like: " +
singleSnapshot.getValue(User.class).getUsername());
holder.users.append(singleSnapshot.getValue(User.class).getUsername());
holder.users.append(",");
}
String[] splitUsers = holder.users.toString().split(",");
if(holder.users.toString().contains(currentUsername + ",")){//mitch, mitchell.tabian
holder.likeByCurrentUser = true;
}else{
holder.likeByCurrentUser = false;
}
int length = splitUsers.length;
if(length == 1){
holder.likesString = "Liked by " + splitUsers[0];
}
else if(length == 2){
holder.likesString = "Liked by " + splitUsers[0]
+ " and " + splitUsers[1];
}
else if(length == 3){
holder.likesString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ " and " + splitUsers[2];
}
else if(length == 4){
holder.likesString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ ", " + splitUsers[2]
+ " and " + splitUsers[3];
}
else if(length > 4){
holder.likesString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ ", " + splitUsers[2]
+ " and " + (splitUsers.length - 3) + " others";
}
Log.d(TAG, "onDataChange: likes string: " + holder.likesString);
//setup likes string
setupLikesString(holder, holder.likesString);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
if(!dataSnapshot.exists()){
holder.likesString = "";
holder.likeByCurrentUser = false;
//setup likes string
setupLikesString(holder, holder.likesString);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}catch (NullPointerException e){
Log.e(TAG, "getLikesString: NullPointerException: " + e.getMessage() );
holder.likesString = "";
holder.likeByCurrentUser = false;
//setup likes string
setupLikesString(holder, holder.likesString);
}
}
private void setupLikesString(final ViewHolder holder, String likesString) {
Log.d(TAG, "setupLikesString: likes string:" + holder.likesString);
if (holder.likeByCurrentUser) {
Log.d(TAG, "setupLikesString: photo is liked by current user");
holder.liked.setText("UnLike");
holder.heartWhite.setVisibility(View.GONE);
holder.heartRed.setVisibility(View.VISIBLE);
holder.heartRed.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return holder.detector.onTouchEvent(event);
}
});
} else {
Log.d(TAG, "setupLikesString: photo is not liked by current user");
holder.liked.setText("Like");
holder.heartWhite.setVisibility(View.VISIBLE);
holder.heartRed.setVisibility(View.GONE);
holder.heartWhite.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return holder.detector.onTouchEvent(event);
}
});
}
holder.likes.setText(likesString);
}
//---------------------------------------------end of likes----------------------------
private String getTimestampDifference(Photo photo){
Log.d(TAG, "getTimestampDifference: getting timestamp difference.");
String difference = "";
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.CANADA);
sdf.setTimeZone(TimeZone.getTimeZone("Canada/Pacific"));//google 'android list of timezones'
Date today = c.getTime();
sdf.format(today);
Date timestamp;
final String photoTimestamp = photo.getDate_created();
try{
timestamp = sdf.parse(photoTimestamp);
difference = String.valueOf(Math.round(((today.getTime() - timestamp.getTime()) / 1000 / 60 / 60 / 24 )));
}catch (ParseException e){
Log.e(TAG, "getTimestampDifference: ParseException: " + e.getMessage() );
difference = "0";
}
return difference;
}
最佳答案
尝试在onCreateView()中设置一个空适配器,并在想要更新数据时调用adapter.notifyDataSetChanged。
因此,在调用 getFriends() 之前的 onCreateView() 方法中,您应该初始化适配器并将其设置为回收器 View
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile_posts, container, false);
mRecView = (RecyclerView) view.findViewById(R.id.lvProfilePosts);
mFriends = new ArrayList<>();
mPhotos = new ArrayList<>();
mAdapter = new ViewProfilePostsAdapter(getActivity(), mPhotos);
mRecView.setAdapter(mAdapter);
getFriends();
return view;
}
并在你的displayPhotos()中调用mAdapter.notifyDataSetChanged();
private void displayPhotos(){
if(mPhotos != null){
Collections.sort(mPhotos, new Comparator<Photo>() {
@Override
public int compare(Photo o1, Photo o2) {
return o2.getDate_created().compareTo(o1.getDate_created());
}
});
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecView.setLayoutManager(llm);
mAdapter.notifyDataSetChanged();
}
}
关于java - 如何修复 RecyclerView : No adapter attached; skipping layout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48042942/
我将一个 div 设置为 100% 宽度,当以 1024 分辨率查看页面时,宽度应从 100% 变为 1000px,我让它与@media 查询一起正常工作,并且在 FF、safari chrome 上
希望有人能帮助我,我已经被困了几天了。 将我的 Domino 服务器更新到 9.01 Fix 3 后,我在 javascript 控制台上不断收到错误消息: TypeError: this.edito
我们正在使用一个基于RMI的java应用程序。当我们运行应用程序时,即使应用程序处于理想阶段,内存使用量仍然不断增加。我们主要使用Vector和散列图数据结构。如何最大限度地减少java内存使用/修复
概述 Internet Download Manager (IDM)是最流行的 Windows 下载管理器。如果你平时工作中使用过IDM,您会惊叹 IDM 下载文件的速度有多快。IDM
当我打开 brave 浏览器时,会打开一个窗口(如下所示)。它并没有真正干扰浏览器的处理。但令人担忧的是为什么这种情况一直发生...... Error On Opening Brave Browser
这是我今天在求职面试中被问到的一个问题: 看下面的代码: int n=20; for (int i =0; i
我不小心删除了/opt/local/bin/perl5.8.9 ,这似乎是 macports 编译的 perl 的主要二进制文件。 现在我有很多取决于 perl5 的端口,但不想卸载并重新安装所有端口
>>>flip fix (0 :: Int) (\a b -> putStrLn "abc") Output: "abc" 这是使用翻转修复的简化版本。 我在一些 YouTube 视频中看到了这种使用
这个问题已经有答案了: How can I fix 'android.os.NetworkOnMainThreadException'? (64 个回答) 已关闭 3 年前。 我在 Android 应
def main(): cash = float(input("How much money: ")) coins = 0 def changeCounter(n): whil
前一周我遇到了类似的问题,查询需要永远运行。在编写此查询时,我尝试应用从其他查询中学到的一些知识,但执行起来需要很长时间。 运行查询的两个单独部分时,每个部分需要 2 分钟才能完成,这是可以接受的,但
下午,我的 CSS 有问题。第三个下拉菜单放错了,我没有解决办法。 这是我想要的: 之前: http://i53.tinypic.com/2qu85z8.png 之后: http://i51.tiny
更新方法: override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingS
我知道这是一个很多人都遇到过的问题,但我不熟悉 Less 并且是 Bootstrap 的新手,我正在寻找一种全 CSS 解决方案来防止我的导航栏折叠到 768 像素以下:
在我的布局中,我创建了以下 jsfiddle 托管的可调整大小的粘性页脚。但是,在调整大小时它与内容重叠。有没有办法让它在所有浏览器上都能响应? http://jsfiddle.net/9aLc0mg
我想要实现的目标 racer-offset 是为了让用户可以设置图像可以以 px 为单位移动多远。偏移量管理偏移量。 Speed-racer 告诉我们图像在滚动过程中移动的速度。我的问题是它不会停止。
我有一个简单的自动换行函数,它接受一个长字符串作为输入,然后将该字符串分成更小的字符串,并将它们添加到一个数组中,以便稍后输出。现在最后一两个字没有输出。这是主要问题。但是,我还想改进功能。我知道这有
我试图在使用每个 slider 之前禁用“下一步”按钮,我不确定为什么在单击不再是 class="not-clicked"的同一个 slider 时取消禁用该按钮. JSFiddle: (这里看起来有
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 8 年前。 如何让程序输出所有信息? IT
On this page ,在“生活”下有一个带有自动生成的子菜单的菜单。子菜单存在一些问题(它会闪烁并改变大小——如果你滚动它就会看到)。我需要以某种方式覆盖它当前正在读取的 css 并使其统一。
我是一名优秀的程序员,十分优秀!