使用工作线程

时间:2017-02-16 12:47:00

标签: java android firebase android-recyclerview android-viewholder

使用 memberRecList RecyclerView,我试图水平列出一个组的每个成员,但是当我设置它的适配器时我遇到了这个问题:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

注意:我在评论中添加了一些注释,滚动到底部查看它们。

这是RecycleView初始化:

// I do InitMemberList() in the OnCreate method of my activity
private void InitMemberList()
{
    LinearLayoutManager layoutManager =
        new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

    memberRecList = (RecyclerView) findViewById(R.id.member_list);
    memberRecList.setHasFixedSize(true);
    memberRecList.setLayoutManager(layoutManager);
    //NOTE 1 (scroll to bottom)
 }

我的数据集是ArrayList 成员。我使用FirebaseDatabase从工作线程中检索它,如下所示:

 //this is also in the OnCreate() method, doesn't matter if it's before
 //or after InitMembersList() because it starts a separate thread.
 //I'm showing it just in case I have any errors here
 private void GetMemberInfo(final String userId)
 {
    //NOTE 2 (scroll to bottom)
    //Getting the reference of the database
    DatabaseReference ref = userDB.getReference().getRef();
    Query query = ref.child(userId);
    //Firebase's method names are confusing. These below just retrieve the
    //data from the database ONCE.
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            tempMember = dataSnapshot.getValue(User.class); //Getting member info
            members.add(tempMember); //adding member info to the list
            memberCount --; //number of members left to retrieve
            if(memberCount == 0) { 
            //NOTE 3 (scroll to bottom)
            PopulateMemberList(); //the adapter is set inside here
            }

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {}
     });
}

我在PopulateMemberList()方法中设置成员适配器,如下所示:

private void PopulateMemberList()
{
    MembersListAdapter adapter = new MembersListAdapter(members);
    memberRecList.setAdapter(adapter);
}

这是我的自定义 MembersListAdapter

public class MembersListAdapter extends RecyclerView.Adapter<MembersListAdapter.MemberViewHolder> {

    private ArrayList<User> users = new ArrayList<>();

    public MembersListAdapter(ArrayList<User> users) {
        this.users = users;
    }

    public class MemberViewHolder extends RecyclerView.ViewHolder{
        TextView rep_txt;
        ImageView memberImageView;

        public MemberViewHolder(View itemView) {
            super(itemView);
            rep_txt = (TextView) itemView.findViewById((R.id.member_rep_text));
            memberImageView = (ImageView) itemView.findViewById(R.id.member_profile_image);
        }
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    @Override
    public MemberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //NOTE 4 (scroll to bottom)
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.member_list_item, parent, true);
        return new MemberViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MemberViewHolder holder, int position) {
            final User currentMember = users.get(position);

            //changing the text of my textviews

            //Loading Member Image
            Picasso.with(GroupActivity.this)
                    .load(currentMember.getProfile_picture_ref())
                    .into(holder.memberImage);
    }

备注:

注1:我尝试将 memberRecList 适配器首先设置为InitMemberList()内的空适配器,然后在从Firebase获取数据后设置新适配器。没工作。给我同样的错误。

注2:我在循环中使用GetMemberInfo(),我逐个传递 userId 。 Firebase专家会告诉我改为使用dataSnapshot.getChildren()创建一个foreach循环来逐个检索用户然后过滤它们,但由于我的数据库大小,这是不可行的。我将扫描一百万用户,只是为了检索其中的五个。

注3:使用 memberCount 查找成员列表检索是否已完成,这样我就可以 >完全检索。如果没有这个,尝试访问数据会产生Nullpointerexception,因为大部分都无法及时收到。

注4:我假设这是我的错误所在?我尝试获取 itemView 父级,然后从中删除** itemView *(正是错误消息说我应该这样做):

        @Override
    public MemberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.member_list_item, parent, true);

        ViewGroup mParent = (ViewGroup) itemView.getParent();
        if(mParent!=null)
            mParent.removeView(itemView);

        return new MemberViewHolder(itemView);
    }

但是我得到了以下错误的一百万个实例:

E/AndroidRuntime: at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6688)

然后出现StackOverflow错误并且应用程序崩溃:

D/Error: ERR: exClass=java.lang.StackOverflowError
D/Error: ERR: exMsg=stack size 8MB
D/Error: ERR: file=ViewGroup.java
D/Error: ERR: class=android.view.ViewGroup
D/Error: ERR: method=resetResolvedLayoutDirection line=6687
D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB

在android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6687)

之后,最后一行重复至少100次,最后一次:

D/Error: ERR: TOTAL BYTES WRITTEN: 41276

编辑:忘记提及,我知道FirebaseRecyclerAdapter存在,但我无法使用它。如果您知道其他可以帮助我的图书馆,请告诉我们。此外,我更喜欢使用ListView而不是RecyclerView,但我需要水平列出项目(这样我可以向左/向右滚动),我认为使用ListView是不可能的。

1 个答案:

答案 0 :(得分:2)

由于这个原因,你得到IllegalStateException

 @Override
    public MemberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //NOTE 4 (scroll to bottom)
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.member_list_item, parent, true);
        return new MemberViewHolder(itemView);
    }

当您为RecyclerView的视图充气时,不应将其添加到其父级,android会为您执行此操作。 将标记更改为以下行中的false以不向父项添加视图:

 View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.member_list_item, parent, false);