Java RecyclerView:未连接适配器:跳过布局
我正在尝试实现将数据从数据库获取到回收器视图,但运行窗口没有显示连接的适配器。我看过各种教程,但没有任何帮助。我是android编程新手。请帮助我学习并改正我的错误 代码如下:Java RecyclerView:未连接适配器:跳过布局,java,android,android-layout,android-recyclerview,android-sqlite,Java,Android,Android Layout,Android Recyclerview,Android Sqlite,我正在尝试实现将数据从数据库获取到回收器视图,但运行窗口没有显示连接的适配器。我看过各种教程,但没有任何帮助。我是android编程新手。请帮助我学习并改正我的错误 代码如下: public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.RecyclerViewHolder>{ private Context mContext; private Cursor mCurs
public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.RecyclerViewHolder>{
private Context mContext;
private Cursor mCursor;
public RecycleviewAdapter(Context context ,Cursor cursor){
mContext= context;
mCursor =cursor;
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder{
public TextView time_data1;
public TextView activity_data;
public TextView location_data;
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
location_data=itemView.findViewById(R.id.location_data);
activity_data=itemView.findViewById(R.id.activity_data);
time_data1=itemView.findViewById(R.id.time_data1);
}
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view=inflater.inflate(R.layout.recycle_item,parent,false);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
Package pkg = new Package();
if(!mCursor.move(position)){
return;
}
String time=mCursor.getString(1);
String name=mCursor.getString(2);
holder.location_data.setText(name);
holder.time_data1.setText(time);
if(pkg.getDst().equals(mCursor.getString(2))){
holder.activity_data.setText("Delivered");
}
else {
holder.activity_data.setText("In Transit");
}
}
@Override
public int getItemCount() {
return mCursor.getCount() ;
}
public void swapCursor(Cursor newCursor){
if(mCursor!=null){
mCursor.close();
}
mCursor=newCursor;
if(newCursor!=null){
notifyDataSetChanged();
}
}
}
回收器xml:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="408dp"
android:layout_height="453dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="0dp" />
recycle_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:text="Location" />
<TextView
android:id="@+id/location_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="71dp"
android:layout_marginLeft="71dp"
android:layout_marginTop="11dp"
android:text="TextView" />
<TextView
android:id="@+id/activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:text="Activity" />
<TextView
android:id="@+id/activity_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="71dp"
android:layout_marginLeft="71dp"
android:layout_marginTop="30dp"
android:text="TextView" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:text="Date/Time" />
<TextView
android:id="@+id/time_data1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="71dp"
android:layout_marginLeft="71dp"
android:layout_marginTop="50dp"
/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
您已经创建了适配器,但没有将其连接到RecyclerView
mAdapter= new RecycleviewAdapter(MySuperApplication.getContext(),cursor1);
添加这一行:
recyclerView.setAdapter(yourAdapter)代码>您正在初始化RecyclerView
并在循环中设置适配器
while (cursor1.moveToNext()){
RecyclerView recyclerView= findViewById(R.id.recycleview);
recyclerView.setLayoutManager(new LinearLayoutManager(MySuperApplication.getContext()));
mAdapter= new RecycleviewAdapter(MySuperApplication.getContext(),cursor1);
// Toast.makeText(getApplicationContext(),"data2"+cursor1.getString(cursor1.getColumnIndex(String.valueOf(1))),Toast.LENGTH_LONG).show();
}
像这样只做一次
Cursor cursor1 =databse.rawQuery("select * from track_info where track_id1 = ? order by name desc ",new String[]{pkg.getId()});
recyclerView.setLayoutManager(new LinearLayoutManager(MySuperApplication.getContext()));
mAdapter= new RecycleviewAdapter(MySuperApplication.getContext(),cursor1);
recyclerView.setAdapter(mAdapter);
你忘记设置适配器了
在这一行之后:
mAdapter=new RecycleviewAdapter(MySuperApplication.getContext(),cursor1);
加:
recyclerView.setAdapter(mAdapter)
并将其移出while循环。您需要调用:recyclerView.setAdapter(您的适配器)代码>在你实例化了你的Adapter后,我尝试了,但没有为它工作me@KaushikBurkuleafter你能指哪条线out@Traendy感谢您的回复从while循环中删除您的recyclerview初始化。在while循环中,在填充数组列表后在Arraylist中添加数据,然后设置adapter.mAdapter=new RecycleviewAdapter(MySuperApplication.getContext(),cursor1);recyclerView.setAdapter(mAdapter);我做了这些更改,但没有帮助。您没有调用swapCursor
为适配器设置数据。所以适配器没有数据显示。我是android编程新手,你能帮我怎么做吗
Cursor cursor1 =databse.rawQuery("select * from track_info where track_id1 = ? order by name desc ",new String[]{pkg.getId()});
recyclerView.setLayoutManager(new LinearLayoutManager(MySuperApplication.getContext()));
mAdapter= new RecycleviewAdapter(MySuperApplication.getContext(),cursor1);
recyclerView.setAdapter(mAdapter);