我正在尝试在“回收者”视图中显示餐厅列表,但不幸的是,“回收者”视图未显示任何内容。
这是我的适配器类
package com.example.khajasangram.Adaptors;
public class RestaurantAdaptor extends
RecyclerView.Adapter<RestaurantAdaptor.RestaurantViewHolder> {
RecyclerView recyclerView;
Context context;
ArrayList<String> name = new ArrayList<>();
ArrayList<String> address = new ArrayList<>();
ArrayList<String> contact = new ArrayList<>();
ArrayList<String> id = new ArrayList<>();
ArrayList<String> created_date = new ArrayList<>();
public RestaurantAdaptor(RecyclerView recyclerView, Context context,
ArrayList<String> name, ArrayList<String> address,ArrayList<String> id,
ArrayList<String> contact, ArrayList<String> created_date) {
this.recyclerView = recyclerView;
this.context = context;
this.name = name;
this.address = address;
this.id = id;
this.contact = contact;
this.created_date = created_date;
}
@NonNull
@Override
public RestaurantViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
int viewType) {
LayoutInflater layoutInflater =
LayoutInflater.from(parent.getContext());
View view =
layoutInflater.inflate(R.layout.restaurant_list_layout,parent,false);
return new RestaurantViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RestaurantViewHolder holder, int
position) {
holder.r_name.setText(name.get(position));
holder.r_address.setText(address.get(position));
holder.r_contact.setText(contact.get(position));
}
@Override
public int getItemCount() { return name.size(); }
public class RestaurantViewHolder extends RecyclerView.ViewHolder {
TextView r_name;
TextView r_address;
TextView r_contact;
TextView r_id;
TextView r_created_date;
public RestaurantViewHolder(@NonNull View itemView) {
super(itemView);
r_name = itemView.findViewById(R.id.res_name);
r_address = itemView.findViewById(R.id.res_address);
r_contact = itemView.findViewById(R.id.res_contact);
}
}
}
这是我上课的地方,我将值传递给适配器
package com.example.khajasangram;
public class RdetailsdisplayActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<String> dname ;
ArrayList<String> daddress ;
ArrayList<String> dcontact ;
ArrayList<String> dcreated_date;
ArrayList<String> did;
RestaurantAdaptor adaptor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rdetailsdisplay);
if(!isConnected(RdetailsdisplayActivity.this))
buildDialog(RdetailsdisplayActivity.this).show();
else{
setContentView(R.layout.activity_rdetailsdisplay);
recyclerView = (RecyclerView)findViewById(R.id.myrecylerView);
dname = new ArrayList<>();
daddress = new ArrayList<>();
dcontact = new ArrayList<>();
dcreated_date = new ArrayList<>();
did = new ArrayList<>();
DatabaseReference reference;
reference =
FirebaseDatabase.getInstance().getReference("Khajasangram")
.child("Restaurant");
reference.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren())
{
String name =
snapshot.child("name").getValue(String.class);
String address =
snapshot.child("address").getValue(String.class);
Long value=
snapshot.child("contact").getValue(Long.class);
String contact = String.valueOf(value);
String created_date =
snapshot.child("created_date").getValue(String.class);
Long idvalue =
snapshot.child("id").getValue(Long.class);
String id = String.valueOf(idvalue);
Toast.makeText(RdetailsdisplayActivity.this, "name =
"+name, Toast.LENGTH_SHORT).show();
dname.add(name);
daddress.add(address);
dcontact.add(contact);
did.add(id);
dcreated_date.add(created_date);
adaptor = new
RestaurantAdaptor(recyclerView,RdetailsdisplayActivity.this,dname,
daddress,dcontact,did,dcreated_date);
recyclerView.setAdapter(adaptor);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}
}
public boolean isConnected(Context context) {
//code
}
public AlertDialog.Builder buildDialog(Context c) {
//code
}
这是我的restaurant_list_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="121dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:layout_marginRight="10dp"
android:background="@drawable/listview_design"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical">
<TextView
android:id="@+id/res_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#0000FF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Bright Educational Academy(BEA)"
/>
<TextView
android:id="@+id/res_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:padding="5dp"
android:textSize="12sp"
android:textStyle="bold"
android:text="Thapathali Road, Kathmandu"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#C9C9C9"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/res_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call"
android:clickable="true"
android:onClick="call_bright"
android:background="@drawable/on_pressing"
android:padding="5dp"
android:drawableLeft="@drawable/ic_call_black_24dp"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:id="@+id/veg_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/on_pressing"
android:gravity="center"
android:padding="5dp"
android:text="Map" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
</LinearLayout>
</LinearLayout>
这是我的Logcat中的错误消息:
07-04 19:58:51.676 20221-20248/com.example.khajasangram E/GED: Failed to get GED Log Buf, err(0)
07-04 19:59:04.212 20221-20221/com.example.khajasangram E/RecyclerView: No adapter attached; skipping layout
07-04 19:59:04.254 20221-20221/com.example.khajasangram E/RecyclerView: No adapter attached; skipping layout
07-04 19:59:06.081 20221-20354/com.example.khajasangram E/NativeCrypto: ssl=0x7f9b48ca00 cert_verify_callback x509_store_ctx=0x7f81b3d208 arg=0x0
07-04 19:59:06.081 20221-20354/com.example.khajasangram E/NativeCrypto: ssl=0x7f9b48ca00 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
07-04 19:59:07.556 20221-20221/com.example.khajasangram E/RecyclerView: No layout manager attached; skipping layout