gpt4 book ai didi

java - 我的 RecyclerView 适配器出现错误,填充了 firebase 数据

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

我的 recyclerView 适配器出现错误,我不知道到底是什么原因造成的。

recyclerView所在fragment的代码:

public class materiaFragment extends Fragment {
private RecyclerView recyclerView;
private ArrayList<String> listaProf, listaDisc, listaNomeM, nomeSalas, passarSala ;
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference("usuarios/");
private DatabaseReference referenceExec = FirebaseDatabase.getInstance().getReference("salas/");
private DatabaseReference referencePegarExec, referenceProf, referenceDisc, referenceProf2;
private adapterMateria adapterMateria;
private String nomeAluno, teste="", prof="", disc="";
private TextView txtDida;

public materiaFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_materia, container, false);
recyclerView = view.findViewById(R.id.recyclerViewM);
txtDida= view.findViewById(R.id.txtDida);
final Context context = view.getContext();

listaNomeM= new ArrayList<>();
listaDisc= new ArrayList<>();
listaProf= new ArrayList<>();
nomeSalas= new ArrayList<>();
passarSala= new ArrayList<>();

pegarM();
adapterMateria = new adapterMateria(listaProf, listaDisc, listaNomeM, context );
//adapterSalas adapterMateria = new adapterSalas(listaNomeM, context);
//configurar recycler view
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
//recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayout.VERTICAL));
recyclerView.setAdapter(adapterMateria);

//evento click
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(context, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {

// Toast.makeText(context, "Item selecionado: " + exercicio.getNomeAtv(),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), verMateriaActivity.class);
intent.putExtra("nMateria", listaNomeM.get(position));
intent.putExtra("nSala", passarSala.get(position));
startActivity(intent);
}

@Override
public void onLongItemClick(View view, int position) {

// Toast.makeText(context, "Click longo: " + exercicio.getNomeAtv(),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), verMateriaActivity.class);
startActivity(intent);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

}
})
);
return view;}

/*
@Override
public void onStart() {
super.onStart();
listaProf.clear();
listaDisc.clear();
listaNomeM.clear();
nomeSalas.clear();
passarSala.clear();

}*/



public void pegarM(){


FirebaseAuth autenticacao = FirebaseAuth.getInstance();
String emailUsu = autenticacao.getCurrentUser().getEmail();
reference.orderByChild("email").equalTo(emailUsu).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
nomeAluno = datas.child("nome").getValue().toString();
referenceExec.orderByChild("alunos/"+ nomeAluno+"/codigo").equalTo(nomeAluno).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
nomeSalas.add(datas.getKey());
}
for (int i = 0; i < nomeSalas.size(); i++) {

referencePegarExec = FirebaseDatabase.getInstance().getReference("salas/" + nomeSalas.get(i) + "/materia/");
referenceProf = FirebaseDatabase.getInstance().getReference("salas/" + nomeSalas.get(i) + "/nomeProf");


final int finalI = i; // o erro eu
referencePegarExec.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
if (!dsp.getKey().equals("1")) {
listaNomeM.add(dsp.getKey());
passarSala.add(nomeSalas.get(finalI));
referenceProf.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listaProf.add(dataSnapshot.getValue(String.class));
referenceDisc = FirebaseDatabase.getInstance().getReference("salas/" + nomeSalas.get(finalI) + "/disciplina");

referenceDisc.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataa) {
listaDisc.add(dataa.getValue().toString());
adapterMateria.notifyDataSetChanged();
// txtNomeA.setText(dataa.getValue(String.class));

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

}

}

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}else {
txtDida.setText("Nenhum arquivo didático disponível");
}


}



@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});

}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}

}

适配器:

public class adapterMateria extends RecyclerView.Adapter<adapterMateria.myViewHolder> {
private ArrayList<String> listaProf, listaDisc, listaNomeM ;
private Context context;

public adapterMateria(ArrayList<String> listaProf, ArrayList<String> listaDisc, ArrayList<String> listaNomeM, Context context) {
this.listaProf = listaProf;
this.listaDisc = listaDisc;
this.listaNomeM = listaNomeM;
this.context = context;
}

@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View itemLista = LayoutInflater.from(parent.getContext())
.inflate(R.layout.adaptermateria_lista, parent, false);

return new myViewHolder(itemLista);
}

@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
holder.nomeMateria.setText(listaNomeM.get(position));
holder.materiaM.setText(listaDisc.get(position)); //error
holder.professor.setText(listaProf.get(position));
}

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

public class myViewHolder extends RecyclerView.ViewHolder{
TextView nomeMateria, materiaM, professor;

public myViewHolder(@NonNull View itemView) {
super(itemView);

nomeMateria= itemView.findViewById(R.id.textNomeConteudo);
materiaM= itemView.findViewById(R.id.textMateriaM);
professor= itemView.findViewById(R.id.textProfessor);

}
}

}

logcat错误消息:

2019-11-11 20:12:32.705 27650-27650/com.example.classmob E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.classmob, PID: 27650
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.classmob.adapter.adapterMateria.onBindViewHolder(adapterMateria.java:40)
at com.example.classmob.adapter.adapterMateria.onBindViewHolder(adapterMateria.java:17)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1775)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1801)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1567)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
`2019-11-11 20:12:32.707 27650-27650/com.example.classmob E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:792)
at android.view.View.layout(View.java:20680)
at android.view.ViewGroup.layout(ViewGroup.java:6197)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2831)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2358)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1494)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7288)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6923)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)

我知道导致该错误的原因是适配器的这一行:“holder.materiaM.setText(listaDisc.get(position));”。但是 arrayList、listaDisc 正在正常地从 fragment 获取数据,所以我不知道是什么导致了这个错误。有人可以帮忙吗?

最佳答案

该错误是因为列表大小更改 getItemCount() 可以提供帮助

    @Override
public int getItemCount() {
int listSize;

listSize = listaProf.size()

if(listSize < listaNomeM.size())
listSize = listaNomeM.size()

if(listSize < listaDisc .size())
listSize = listaDisc.size()

return listSize;
}

或者如果您不想丢失元素(但数据)

    @Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
holder.nomeMateria.setText(listaNomeM.get(position));

if(position < listaDisc.size() )
holder.materiaM.setText(listaDisc.get(position));

if(position < listaProf.size() )
holder.professor.setText(listaProf.get(position));
}

最后一件事,检查你的方法来填充listaDisc

关于java - 我的 RecyclerView 适配器出现错误,填充了 firebase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58809089/

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