为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUH函数 2022-05-25 15:47:56
我正在开发 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) { }        });    }}客户适配器.javapublic 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 <>();    }
查看完整描述

4 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

我认为问题来自您的适配器,请更改您的适配器代码,如下所示:


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


    private ArrayList<owner> ownerlist;

    private Context mContext;


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

        this.mContext = c;

        this.ownerlist = p;

    }


    @NonNull

    @Override

    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 

    viewType) {

        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.prospectowners,parent,false));

    }


    @Override

    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        owner owner = ownerlist.get(position);


        holder.name.setText(owner.getName());

        holder.field.setText(owner.getFieldname());

        holder.mobile.setText(owner.getMobile());

        holder.address.setText(owner.getAddress());

        holder.cost.setText(owner.getCost());

    }


    @Override

    public int getItemCount() {

        return ownerlist.size();

    }


    public void addData(ArrayList<owner> newData){

        this.ownerlist.addAll(newData);

        notifyOnDataSetChanged();

    }


    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView name;

        public TextView field;

        public TextView mobile;

        public TextView address;

        public TextView cost;


       public MyViewHolder(View itemView) {

            super(itemView);

            this.name = itemView.findViewById(R.id.name);

            this.field =  itemView.findViewById(R.id.fieldName);

            this.mobile =  itemView.findViewById(R.id.mobileowner);

            this.address =  itemView.findViewById(R.id.address);

            this.cost =  itemView.findViewById(R.id.cost);

        }

    }

}

然后像这样调用方法addData:


@Override

            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

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

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

                    ownerlist.add(o);

                }

                list.setAdapter(adapter);

                adapter.addData(ownerlist);


            }

顺便说一句,实际上您可以在侦听器之外设置适配器


UPDATE(此更新设置适配器值的位置默认为空数据,然后在监听触发时填充)


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


    private ArrayList<owner> ownerlist;

    private Context mContext;


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

        this.mContext = c;

        this.ownerlist = new Arraylist<>();

    }


    @NonNull

    @Override

    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 

    viewType) {

        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.prospectowners,parent,false));

    }


    @Override

    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        owner owner = ownerlist.get(position);


        holder.name.setText(owner.getName());

        holder.field.setText(owner.getFieldname());

        holder.mobile.setText(owner.getMobile());

        holder.address.setText(owner.getAddress());

        holder.cost.setText(owner.getCost());

    }


    @Override

    public int getItemCount() {

        return ownerlist.size();

    }


    public void addData(ArrayList<owner> newData){

        this.ownerlist.addAll(newData);

        notifyOnDataSetChanged();

    }


    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView name;

        public TextView field;

        public TextView mobile;

        public TextView address;

        public TextView cost;


       public MyViewHolder(View itemView) {

            super(itemView);

            this.name = itemView.findViewById(R.id.name);

            this.field =  itemView.findViewById(R.id.fieldName);

            this.mobile =  itemView.findViewById(R.id.mobileowner);

            this.address =  itemView.findViewById(R.id.address);

            this.cost =  itemView.findViewById(R.id.cost);

        }

    }

}


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);

        list.setAdapter(adapter);


        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);

                }

                adapter.addData(ownerlist);

            }


            @Override

            public void onCancelled(@NonNull DatabaseError databaseError) { }

        });

    }

}



查看完整回答
反对 回复 2022-05-25
?
跃然一笑

TA贡献1826条经验 获得超6个赞

在适配器中创建新功能:


public void addData(List<Owner> owners){

 this.ownerList = owners;

 notifyOnDataSetChanged();

}

并在onDataChange通话中:


adapter.addData(owners);


查看完整回答
反对 回复 2022-05-25
?
月关宝盒

TA贡献1772条经验 获得超5个赞

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


//img1.sycdn.imooc.com//628ddf610001f8bf06140192.jpg

查看完整回答
反对 回复 2022-05-25
?
阿晨1998

TA贡献2037条经验 获得超6个赞

试试这个,

在您的活动课程中更改

list.setAdapter(adapter)

list.setAdapter(new customAdapter(prospectowner_listview.this, ownerlist));


查看完整回答
反对 回复 2022-05-25
  • 4 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信