我的 customAdapter 或 Activity.java 有什么问题?

我正在开发 RecyclerView 并使用 CustomAdapter 并且我的应用程序运行顺利,但我的 ListView 没有显示它的内容!我的代码有什么遗漏或错误?我试图返回 arraylist.size() 并输入 list.setHasFixedSize 并没有工作!


潜在所有者_listview.java(活动类)


public class prospectowner_listview extends AppCompatActivity {


    RecyclerView list;

    ArrayList <com.example.android.e7gzlykora.owner> ownerlist = new ArrayList <>();

    private DatabaseReference mFirebaseDatabase;

    private FirebaseDatabase mFirebaseInstance;

    owner owner;

    customAdapter adapter;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.prospectowner_listview);


        list = findViewById(R.id.list);

        LinearLayoutManager manager = new LinearLayoutManager(this);

        list.setLayoutManager(manager);

        list.setHasFixedSize(true);

        manager.setOrientation(LinearLayoutManager.VERTICAL);

        adapter = new customAdapter(prospectowner_listview.this,ownerlist);


        mFirebaseInstance = FirebaseDatabase.getInstance();

        mFirebaseDatabase = mFirebaseInstance.getReference("owners");


        mFirebaseDatabase.addValueEventListener(new ValueEventListener() {

            @Override

            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot data: dataSnapshot.getChildren()){

                    owner o = data.getValue(owner.class);

                    ownerlist.add(o);

                }

                list.setAdapter(adapter);

            }


            @Override

            public void onCancelled(@NonNull DatabaseError databaseError) { }

        });

    }

}

客户适配器.java


public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder>{


    private final ArrayList<owner> ownerlist;

    private final Context mContext;


    public customAdapter(Context c , ArrayList<owner> p) {

        this.mContext = c;

        this.ownerlist = new ArrayList <>();

    }


HUH函数
浏览 115回答 4
4回答

红颜莎娜

我认为问题来自您的适配器,请更改您的适配器代码,如下所示:public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder>{&nbsp; &nbsp; private ArrayList<owner> ownerlist;&nbsp; &nbsp; private Context mContext;&nbsp; &nbsp; public customAdapter(Context c , ArrayList<owner> p) {&nbsp; &nbsp; &nbsp; &nbsp; this.mContext = c;&nbsp; &nbsp; &nbsp; &nbsp; this.ownerlist = p;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int&nbsp;&nbsp; &nbsp; viewType) {&nbsp; &nbsp; &nbsp; &nbsp; return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.prospectowners,parent,false));&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {&nbsp; &nbsp; &nbsp; &nbsp; owner owner = ownerlist.get(position);&nbsp; &nbsp; &nbsp; &nbsp; holder.name.setText(owner.getName());&nbsp; &nbsp; &nbsp; &nbsp; holder.field.setText(owner.getFieldname());&nbsp; &nbsp; &nbsp; &nbsp; holder.mobile.setText(owner.getMobile());&nbsp; &nbsp; &nbsp; &nbsp; holder.address.setText(owner.getAddress());&nbsp; &nbsp; &nbsp; &nbsp; holder.cost.setText(owner.getCost());&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount() {&nbsp; &nbsp; &nbsp; &nbsp; return ownerlist.size();&nbsp; &nbsp; }&nbsp; &nbsp; public void addData(ArrayList<owner> newData){&nbsp; &nbsp; &nbsp; &nbsp; this.ownerlist.addAll(newData);&nbsp; &nbsp; &nbsp; &nbsp; notifyOnDataSetChanged();&nbsp; &nbsp; }&nbsp; &nbsp; public class MyViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; public TextView name;&nbsp; &nbsp; &nbsp; &nbsp; public TextView field;&nbsp; &nbsp; &nbsp; &nbsp; public TextView mobile;&nbsp; &nbsp; &nbsp; &nbsp; public TextView address;&nbsp; &nbsp; &nbsp; &nbsp; public TextView cost;&nbsp; &nbsp; &nbsp; &nbsp;public MyViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = itemView.findViewById(R.id.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.field =&nbsp; itemView.findViewById(R.id.fieldName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.mobile =&nbsp; itemView.findViewById(R.id.mobileowner);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.address =&nbsp; itemView.findViewById(R.id.address);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.cost =&nbsp; itemView.findViewById(R.id.cost);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}然后像这样调用方法addData:@Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot data: dataSnapshot.getChildren()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; owner o = data.getValue(owner.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ownerlist.add(o);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.addData(ownerlist);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }顺便说一句,实际上您可以在侦听器之外设置适配器UPDATE(此更新设置适配器值的位置默认为空数据,然后在监听触发时填充)public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder>{&nbsp; &nbsp; private ArrayList<owner> ownerlist;&nbsp; &nbsp; private Context mContext;&nbsp; &nbsp; public customAdapter(Context c , ArrayList<owner> p) {&nbsp; &nbsp; &nbsp; &nbsp; this.mContext = c;&nbsp; &nbsp; &nbsp; &nbsp; this.ownerlist = new Arraylist<>();&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int&nbsp;&nbsp; &nbsp; viewType) {&nbsp; &nbsp; &nbsp; &nbsp; return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.prospectowners,parent,false));&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {&nbsp; &nbsp; &nbsp; &nbsp; owner owner = ownerlist.get(position);&nbsp; &nbsp; &nbsp; &nbsp; holder.name.setText(owner.getName());&nbsp; &nbsp; &nbsp; &nbsp; holder.field.setText(owner.getFieldname());&nbsp; &nbsp; &nbsp; &nbsp; holder.mobile.setText(owner.getMobile());&nbsp; &nbsp; &nbsp; &nbsp; holder.address.setText(owner.getAddress());&nbsp; &nbsp; &nbsp; &nbsp; holder.cost.setText(owner.getCost());&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount() {&nbsp; &nbsp; &nbsp; &nbsp; return ownerlist.size();&nbsp; &nbsp; }&nbsp; &nbsp; public void addData(ArrayList<owner> newData){&nbsp; &nbsp; &nbsp; &nbsp; this.ownerlist.addAll(newData);&nbsp; &nbsp; &nbsp; &nbsp; notifyOnDataSetChanged();&nbsp; &nbsp; }&nbsp; &nbsp; public class MyViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; public TextView name;&nbsp; &nbsp; &nbsp; &nbsp; public TextView field;&nbsp; &nbsp; &nbsp; &nbsp; public TextView mobile;&nbsp; &nbsp; &nbsp; &nbsp; public TextView address;&nbsp; &nbsp; &nbsp; &nbsp; public TextView cost;&nbsp; &nbsp; &nbsp; &nbsp;public MyViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = itemView.findViewById(R.id.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.field =&nbsp; itemView.findViewById(R.id.fieldName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.mobile =&nbsp; itemView.findViewById(R.id.mobileowner);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.address =&nbsp; itemView.findViewById(R.id.address);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.cost =&nbsp; itemView.findViewById(R.id.cost);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class prospectowner_listview extends AppCompatActivity {&nbsp; &nbsp; RecyclerView list;&nbsp; &nbsp; ArrayList <com.example.android.e7gzlykora.owner> ownerlist = new ArrayList <>();&nbsp; &nbsp; private DatabaseReference mFirebaseDatabase;&nbsp; &nbsp; private FirebaseDatabase mFirebaseInstance;&nbsp; &nbsp; owner owner;&nbsp; &nbsp; customAdapter adapter;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.prospectowner_listview);&nbsp; &nbsp; &nbsp; &nbsp; list = findViewById(R.id.list);&nbsp; &nbsp; &nbsp; &nbsp; LinearLayoutManager manager = new LinearLayoutManager(this);&nbsp; &nbsp; &nbsp; &nbsp; list.setLayoutManager(manager);&nbsp; &nbsp; &nbsp; &nbsp; list.setHasFixedSize(true);&nbsp; &nbsp; &nbsp; &nbsp; manager.setOrientation(LinearLayoutManager.VERTICAL);&nbsp; &nbsp; &nbsp; &nbsp; adapter = new customAdapter(prospectowner_listview.this,ownerlist);&nbsp; &nbsp; &nbsp; &nbsp; list.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; mFirebaseInstance = FirebaseDatabase.getInstance();&nbsp; &nbsp; &nbsp; &nbsp; mFirebaseDatabase = mFirebaseInstance.getReference("owners");&nbsp; &nbsp; &nbsp; &nbsp; mFirebaseDatabase.addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot data: dataSnapshot.getChildren()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; owner o = data.getValue(owner.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ownerlist.add(o);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.addData(ownerlist);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) { }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}

跃然一笑

在适配器中创建新功能:public void addData(List<Owner> owners){&nbsp;this.ownerList = owners;&nbsp;notifyOnDataSetChanged();}并在onDataChange通话中:adapter.addData(owners);

月关宝盒

数组列表始终为空,因为您初始化为新列表的适配器构造函数。请检查一下

阿晨1998

试试这个,在您的活动课程中更改list.setAdapter(adapter)到list.setAdapter(new&nbsp;customAdapter(prospectowner_listview.this,&nbsp;ownerlist));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java