- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在小米应用程序中显示数据,但无法显示此错误:
RecyclerView 未连接适配器;跳过布局
这是我的 MainActivity,我在其中显示我的信息,我认为这是我的自定义适配器“CurpAdapter”的问题,因此,该应用程序不显示我的任何信息,该信息保存在 firebase 项目中,并且无法在控制台中显示。
public class MainActivity extends AppCompatActivity {
DatabaseReference database;
private List<Curp> curps;
private RecyclerView reciclador;
RecyclerView.Adapter adap;
private CurpAdapter cupadapter;
List<Curp> lista=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseApp.initializeApp(this);
database = FirebaseDatabase.getInstance().getReference();
reciclador=(RecyclerView)findViewById(R.id.reciclador);
LinearLayoutManager lim=new LinearLayoutManager(this);
lim.setOrientation(LinearLayoutManager.VERTICAL);
reciclador.setLayoutManager(lim);
database.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Curp value=new Curp();
value=(Curp) dataSnapshot.getValue(Curp.class);
lista.add(value);
cupadapter=new CurpAdapter(lista);
adap = new CurpAdapter(lista);
reciclador.setAdapter(adap);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
@Override
public void onCancelled(DatabaseError databaseError) { }
});
}
@Override
protected void onStart(){
super.onStart();
DatabaseReference conditionRef= database.child("root");
}
public void lanzar(View view){
Intent generar = new Intent(this, Generar.class);
startActivity(generar);
}
}
这是我的自定义适配器 CurpAdapter 类
public class CurpAdapter extends RecyclerView.Adapter<CurpAdapter.curpHolder> {
private List<Curp> curps;
public CurpAdapter(List<Curp> curps) {
this.curps = curps;
}
@Override
public curpHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
return new curpHolder(v);
}
@Override
public void onBindViewHolder(curpHolder holder, int position) {
Curp curpc=curps.get(position);
holder.curpCpompleto.setText(curpc.curpGen);
holder.iniciales.setText(curpc.curpGen.substring(0,3));
holder.imagen.setImageLevel(0);
}
@Override
public int getItemCount() {
return 0;
}
public class curpHolder extends RecyclerView.ViewHolder{
ImageView imagen;
TextView iniciales, curpCpompleto;
public curpHolder(View itemView) {
super(itemView);
iniciales=(TextView)itemView.findViewById(R.id.iniciales);
curpCpompleto=(TextView)itemView.findViewById(R.id.curpCompleta);
imagen=(ImageView)itemView.findViewById(R.id.imagen);
}
}
}
最佳答案
您必须在database.addChildEventListener
之前创建适配器。这意味着下面这两行应该从 addChildEventListener
移出并在其之前调用:
adap = new CurpAdapter(lista);
reciclador.setAdapter(adap);
这将在 onCreate
方法的开头创建包含空数据列表的适配器。
接下来,您必须在 CurpAdapter 中添加 setData 方法。它看起来像这样:
public void setData(List<Curp> curps){
this.curps = curps.
}
现在,在 lista.add(value);
之后的 onChildAdded
中,您必须调用
adap.setData(list);
adap.notifyDataSetChange()
关于java - 自定义适配器出现此错误 RecyclerView 未连接适配器;跳过布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46694951/
我的算法- private static MyList skip$DeleteItem(MyList L , int M , int N){ MyList curr = L; MyLi
我正在 SWI-Prolog 下开发,但我的目标是 Erlog (https://github.com/rvirding/erlog)。我需要一种使用非标准 Prolog 语法的方法。 有没有办法
我正在尝试从应用程序下载一大堆文件。它的shell命令是“下载文件名”。 我有一个文本文件,其中包含必须下载的所有文件名。我要做的就是运行一个脚本/命令,以便在执行上述命令时 1.从文本文件中提取文件
我试图循环遍历所有用户的评论,但使用 if 语句查找特定值。问题是我的应用程序崩溃了,因为一些用户没有发表评论,因此我得到“无法读取‘收集’未定义的属性”。如何跳过 if 语句的未定义值?代码如下:
我们有按年份分区的索引,例如: items-2019 items-2020 考虑以下数据: POST items-2019/_doc { "@timestamp": "2019-01-01" }
我只是编写一个页面来按实体编号查看每个 ASCII 条目,我想知道是否有一种更简单/更干净的方法来跳过不需要的数字。 var x = new Ar
我希望能够普遍使用重复条目,但也能够跳过特定日期。例子: ** TODO swim practice SCHEDULED 但是,我提前知道 2013-12-25 不会有练习。但是,当我将项目标
如何跳过像这样的 for 循环的一次迭代: for (int i = 65; i <= 90; i++) { if (!(i == 73)) { uniq.add((char) i);
这个问题已经存在: Scanner issue when using nextLine after nextXXX [duplicate] 已关闭 9 年前。 ask=1; while(ask==1)
我在使用一个程序时遇到了一些麻烦,我应该允许用户在程序中输入任意数量的数字,直到他们不再想要为止。然后程序应该计算输入数字的平均值和最大值。我哪里做错了? import java.util.Scann
我有一个名为segments的 Sprite 数组,我想在每个循环中跳过segments的第一个元素。我目前正在这样做: var first = true; for each (var segment
我目前正在编写一个 for 循环来遍历包含 38 个元素的 2D。然而,其中一些元素为空,我希望 for 循环简单地跳过它们(因为在我正在解决的难题中,它们没有与它们相关的移动)。快速搜索表明,我可以
我想使用pre-commit处理我的 git 项目的 git hooks。但是,当我使用它时,git commit 命令不断跳过 unittest 执行: (smartexchange) trnboo
这个问题在这里已经有了答案: Does scanf() take '\n' as input leftover from previous scanf()? (1 个回答) 关闭 9 年前。 我正在
我正在迭代 csv 文件中的每一行,并仅选择/计算满足条件的行。但是,当连续出现错误时,它会停止循环。有没有办法告诉 python 跳过错误并移动到下一行?我使用了 try 函数但没有工作。我的代码是
感谢您提供的优秀示例,我试过了,它按我的预期工作。很高兴看到有人了解问题的本质。但是,我认为我应该用 Lift 标记问题,因为我正在使用 Lift 框架,这就是(仍然)发生这个问题的地方(尽管我仍然认
大家好,我正在编写一个算法来从 NodeTree 中删除具体分支(例如 DSF)。如果您选择 Node 的名称,算法将检查该 Node 是否是其他 Node 的父 Node ;如果是,它会获取该 No
附有代码和输出。 基本上它是第二次跳过我的输入。就像我启动代码一样,它让我输入一个选项,然后第二次跳过输入,直接转到开关的默认情况。 然后第三次它就会允许我输入。不明白为什么。 任何帮助将不胜感激。
我在 JavaScript 中有一个 for 循环,它会定期跳过间隔,但我无法确定原因。 console.log(parseInt($('input.num-to-add').val())); num
我正在 JasperSoft 中填写参数。在我的报告中我有参数:参数_1、参数_2、参数_3 int a; for (a = 0; a < headers.length; a++) {
我是一名优秀的程序员,十分优秀!