gpt4 book ai didi

java - 从工作线程使用 RecyclerView.setAdapter

转载 作者:行者123 更新时间:2023-12-02 03:02:20 25 4
gpt4 key购买 nike

使用 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() 方法中设置 members 适配器,如下所示:

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: 我尝试首先在 InitMemberList() 内将 memberRecList 适配器设置为空适配器,然后在从 Firebase 获取数据后设置新适配器。没用。给了我同样的错误。

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

注 3: 使用 memberCount 来查找成员列表检索是否已完成,使我能够在之后开始处理数据>完全检索。如果没有这个,尝试访问数据会出现空指针异常,因为大部分数据无法及时接收。

注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 不可能做到这一点。

最佳答案

由于以下原因,您会收到 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 填充 View 时,您不应将其添加到其父级,Android 会为您完成此操作。在以下行中将标志更改为 false 以不将 View 添加到父级:

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

关于java - 从工作线程使用 RecyclerView.setAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42274411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com