找回密码
 立即注册
androidRecyclerViewimportlayout | 软件设计/软件工程 2022-05-03 228 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
问题
这是我数据库中的数据:

这是我的项目的代码。 我没有发现任何错误。 它不显示其中的内容,甚至显示空白页。

适配器
  1. import android.view.LayoutInflater;
  2. import android.view.View;
  3. import android.view.ViewGroup;
  4. import android.widget.TextView;

  5. import androidx.annotation.NonNull;
  6. import androidx.recyclerview.widget.RecyclerView;
  7. import androidx.recyclerview.widget.RecyclerView.ViewHolder;

  8. import java.util.ArrayList;

  9. public class MylistAdapter extends RecyclerView.Adapter<MylistAdapter.MylistViewHolder> {
  10.     private ArrayList<RequestUser> users;


  11.     class MylistViewHolder extends RecyclerView.ViewHolder{

  12.         private TextView dispname1;
  13.         private TextView dispphn1;
  14.         private TextView dispcity1;
  15.         private TextView dispaddr1;
  16.         private TextView dispnumber1;

  17.         private MylistViewHolder(@NonNull View itemView) {
  18.             super(itemView);
  19.             dispaddr1=itemView.findViewById(R.id.dispaddr);
  20.             dispcity1=itemView.findViewById(R.id.dispcity);
  21.             dispname1=itemView.findViewById(R.id.dispname);
  22.             dispphn1=itemView.findViewById(R.id.disphn);
  23.             dispnumber1=itemView.findViewById(R.id.dispnumber);
  24.         }
  25.     }


  26.     public MylistAdapter(ArrayList<RequestUser> usrs) {
  27.         this.users= usrs;
  28.     }


  29.     @Override
  30.     public MylistViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  31.         View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false);
  32.         MylistViewHolder mlvh=new MylistViewHolder(v);
  33.         return mlvh;
  34.     }

  35.     @Override
  36.     public void onBindViewHolder(@NonNull MylistViewHolder holder, int position) {

  37.         RequestUser curuser=users.get(position);

  38.         holder.dispphn1.setText(curuser.getRphn1());
  39.         holder.dispname1.setText(curuser.getRname1());
  40.         holder.dispcity1.setText(curuser.getRcity1());
  41.         holder.dispaddr1.setText(curuser.getRaddr1());
  42.         holder.dispnumber1.setText(curuser.getRnumber1());
  43.     }

  44.     @Override
  45.     public int getItemCount() {
  46.         return users.size();
  47.     }
  48. }
复制代码

主要类别

包含主要功能
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.Objects;

public class Transport extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;
    DatabaseReference dbreferance;
    FirebaseAuth firebaseAuth;
    FirebaseDatabase firebaseDatabase;
    private ArrayList<RequestUser> usrs=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transport);

        firebaseAuth=FirebaseAuth.getInstance();
        firebaseDatabase=FirebaseDatabase.getInstance();
        dbreferance= FirebaseDatabase.getInstance().getReference().child("Donate");
        dbreferance.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                usrs.clear();
                for(DataSnapshot child:dataSnapshot.getChildren())
                {
                    //String id=child.getKey();
                    RequestUser usr=child.getValue(RequestUser.class);
                    usrs.add(usr);
                    //RequestUser usr= (RequestUser) child.getValue();
                    //usrs.add(usr);
                    //System.out.println(usr.rname1);
                    Toast.makeText(Transport.this,usr.getRphn1(),Toast.LENGTH_SHORT).show();
                }

            }

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

            }
        });
        recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        layoutManager=new LinearLayoutManager(this);
        adapter=new MylistAdapter(usrs);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);


    }


}
XML布局

对于回收站视图和卡视图
<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/dispname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Line1"
            android:textColor="@android:color/black"
            android:textSize="30sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/dispcity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/dispname"
            android:textSize="25sp"
            android:layout_marginStart="8dp"
            android:text="Line2"
            android:layout_marginLeft="8dp" />
        <TextView
            android:id="@+id/dispaddr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/dispcity"
            android:textSize="15sp"
            android:text="Line3"
            android:layout_marginLeft="16dp"/>
        <TextView
            android:id="@+id/disphn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/dispaddr"
            android:textSize="15sp"
            android:text="Line4"
            android:layout_marginLeft="24dp"/>
        <TextView
            android:id="@+id/dispnumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/dispaddr"
            android:textSize="15sp"
            android:text="Line5"
            android:layout_marginLeft="24dp"/>
    </RelativeLayout>

</androidx.cardview.widget.CardView>

另一种布局

ie卡视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".Transport">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"


        tools:layout_editor_absoluteY="1dp"
        tools:ignore="MissingConstraints" />
</RelativeLayout>
回答
当我们添加一个监听器时,Android 会为它创建一个单独的线程,单独运行。 因此,在您的情况下,我们添加的监听器 android 正在为其创建一个单独的线程,然后它立即为 recyclerView 设置适配器。

在添加侦听器之前创建适配器,并在 for 循环之后的 onDataChanged() 方法中使用 recyclerView 设置适配器。
@Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            usrs.clear();
            for(DataSnapshot child:dataSnapshot.getChildren())
            {
                //String id=child.getKey();
                RequestUser usr=child.getValue(RequestUser.class);
                usrs.add(usr);
                //RequestUser usr= (RequestUser) child.getValue();
                //usrs.add(usr);
                //System.out.println(usr.rname1);
                Toast.makeText(Transport.this,usr.getRphn1(),Toast.LENGTH_SHORT).show();
            }
            recyclerView.setAdapter(adapter);
        }

(problem
This is the data in my database:
This is the code of my project. I didn't find any mistakes. It doesn't display the contents, even blank pages.
Adapter
import android. view. LayoutInflater;
import android. view. View;
import android. view. ViewGroup;
import android. widget. TextView;
import androidx. annotation. NonNull;
import androidx. recyclerview. widget. RecyclerView;
import androidx. recyclerview. widget. RecyclerView. ViewHolder;
import java. util. ArrayList;
public class MylistAdapter extends RecyclerView. Adapter<MylistAdapter. MylistViewHolder> {
private ArrayList<RequestUser> users;
class MylistViewHolder extends RecyclerView. ViewHolder{
private TextView dispname1;
private TextView dispphn1;
private TextView dispcity1;
private TextView dispaddr1;
private TextView dispnumber1;
private MylistViewHolder(@NonNull View itemView) {
super(itemView);
dispaddr1=itemView. findViewById(R.id.dispaddr);
dispcity1=itemView. findViewById(R.id.dispcity);
dispname1=itemView. findViewById(R.id.dispname);
dispphn1=itemView. findViewById(R.id.disphn);
dispnumber1=itemView. findViewById(R.id.dispnumber);
}
}
public MylistAdapter(ArrayList<RequestUser> usrs) {
this. users= usrs;
}
@Override
public MylistViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v=LayoutInflater. from(parent.getContext()). inflate(R.layout.row_item,parent,false);
MylistViewHolder mlvh=new MylistViewHolder(v);
return mlvh;
}
@Override
public void onBindViewHolder(@NonNull MylistViewHolder holder, int position) {
RequestUser curuser=users. get(position);
holder. dispphn1. setText(curuser.getRphn1());
holder. dispname1. setText(curuser.getRname1());
holder. dispcity1. setText(curuser.getRcity1());
holder. dispaddr1. setText(curuser.getRaddr1());
holder. dispnumber1. setText(curuser.getRnumber1());
}
@Override
public int getItemCount() {
return users. size();
}
}
Main categories
Including main functions
import androidx. annotation. NonNull;
import androidx. annotation. RequiresApi;
import androidx. appcompat. app. AppCompatActivity;
import androidx. recyclerview. widget. LinearLayoutManager;
import androidx. recyclerview. widget. RecyclerView;
import android. os. Build;
import android. os. Bundle;
import android. widget. Toast;
import com. google. firebase. auth. FirebaseAuth;
import com. google. firebase. auth. FirebaseUser;
import com. google. firebase. database. DataSnapshot;
import com. google. firebase. database. DatabaseError;
import com. google. firebase. database. DatabaseReference;
import com. google. firebase. database. FirebaseDatabase;
import com. google. firebase. database. ValueEventListener;
import java. util. ArrayList;
import java. util. Objects;
public class Transport extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView. LayoutManager layoutManager;
private RecyclerView. Adapter adapter;
DatabaseReference dbreferance;
FirebaseAuth firebaseAuth;
FirebaseDatabase firebaseDatabase;
private ArrayList<RequestUser> usrs=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super. onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
firebaseAuth=FirebaseAuth. getInstance();
firebaseDatabase=FirebaseDatabase. getInstance();
dbreferance= FirebaseDatabase. getInstance(). getReference(). child("Donate");
dbreferance. addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
usrs. clear();
for(DataSnapshot child:dataSnapshot.getChildren())
{
//String id=child. getKey();
RequestUser usr=child. getValue(RequestUser.class);
usrs. add(usr);
//RequestUser usr= (RequestUser) child. getValue();
//usrs. add(usr);
//System. out. println(usr.rname1);
Toast. makeText(Transport.this,usr.getRphn1(),Toast. LENGTH_ SHORT). show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
recyclerView. setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
adapter=new MylistAdapter(usrs);
recyclerView. setLayoutManager(layoutManager);
recyclerView. setAdapter(adapter);
}
}
XML layout
For recycle bin view and card view
<? xml version="1.0" encoding="utf-8"?>
<androidx. cardview. widget. CardView android:layout_ width="match_parent"
android:layout_ height="wrap_content"
xmlns:android=
)





上一篇:OPENERP学习笔记 DOMAIN 的应用
下一篇:在 Python 中出现错误“SyntaxWarning: 'str' object is not callable”