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

回收器视图不刷新和显示数据

回收器视图不刷新和显示数据

尚方宝剑之说 2023-07-13 14:19:00
在我的回收器视图中,当添加数据时,回收器视图不会显示它,直到用户关闭活动并再次打开它。我认为这与notifydataetchanger有关。请在这件事上给予我帮助现在唯一有效的就是创建一个 Intent 。但这使得应用程序的 loob 非常糟糕我的适配器package com.example.myapplication;import android.content.Context;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 java.util.List;public class Rec_adaptor_aza extends RecyclerView.Adapter<Rec_adaptor_aza.ViewHolder> {    Context context;    public Rec_adaptor_aza(Context context, List<Model_aza> list_aza) {        this.context = context;        this.list_aza = list_aza;    }    List<Model_aza> list_aza;    @NonNull    @Override    public Rec_adaptor_aza.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {       View view= LayoutInflater.from(context).inflate(R.layout.rec_row_aza,parent,false);        return new ViewHolder(view);    }    @Override    public void onBindViewHolder(@NonNull Rec_adaptor_aza.ViewHolder holder, int position) {    Model_aza modelAza =list_aza.get(position);    holder.txt_name.setText(modelAza.getName_aza());    holder.txt_semat.setText(modelAza.getSemat_aza());    holder.txt_saat_voood.setText(modelAza.getSaaat_vorood_aza());    holder.txt_saat_khoroo.setText(modelAza.getSaat_khorooj_aza());    }    @Override    public int getItemCount() {        return list_aza.size();    }    public class ViewHolder extends RecyclerView.ViewHolder {        TextView txt_name,txt_semat,txt_saat_voood,txt_saat_khoroo;        public ViewHolder(@NonNull View itemView) {            super(itemView);            txt_name=itemView.findViewById(R.id.txt__person__name);            txt_semat=itemView.findViewById(R.id.txt__person__semat);            txt_saat_voood=itemView.findViewById(R.id.txt__person__enter);            txt_saat_khoroo=itemView.findViewById(R.id.txt__person__out);        }    }}
查看完整描述

4 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

只需添加以下代码,在您的clickListner中,您需要在列表中添加相同的对象,添加后您需要使用notifyDataSetChanged() 通知适配器有关插入的项目。


btn__add__field.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                EditText edtname = findViewById(R.id.edt__person__name);

                EditText edt_semat_aza = findViewById(R.id.edt__person__semat);

                EditText edt_vorood_aza = findViewById(R.id.edt__person__enter);

                EditText edt_khorooj_aza = findViewById(R.id.edt__person__out);

                String name_aza = edtname.getText().toString();

                String semat_aza = edt_semat_aza.getText().toString();

                String saat_vorood_aza = edt_vorood_aza.getText().toString();

                String saat_khorooj_aza = edt_khorooj_aza.getText().toString();

                long result = dataBase_aza.insert_info(name_aza, semat_aza, saat_vorood_aza, saat_khorooj_aza,id);

                Toast.makeText(Activity_Gozaresh_giri.this, result + "", Toast.LENGTH_SHORT).show();


                /*

                * Adding entered data in list*/

                Model_aza mm=new Model_aza();

                mm.setName_aza(name_aza);

                mm.setSemat_aza(semat_aza);

                mm.setSaaat_vorood_aza(saat_vorood_aza);

                mm.setSaat_khorooj_aza(saat_khorooj_aza);


                list_aza.add(mm);

                rec_adaptor_aza.notifyDataSetChanged();


            }

        });


查看完整回答
反对 回复 2023-07-13
?
慕侠2389804

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

在您的 中layoutmanager设置之前adapter设置,如下所示recylerviewactivity


    RecyclerView recyclerView_aza = findViewById(R.id.rec_aza);

    // set layout manager before set adapter

    recyclerView_aza.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    recyclerView_aza.setAdapter(new Rec_adaptor_aza(this, list_aza));        


查看完整回答
反对 回复 2023-07-13
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

像下面这样使用它:-


@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_gozaresh_giri);

    btn__add__field1 = findViewById(R.id.btn__add__field1);

    btn__add__field = findViewById(R.id.btn__add__field);

    int id=getIntent().getIntExtra("id",0);

    list_aza = new ArrayList<>();

    Rec_adaptor_aza  adapter = new Rec_adaptor_aza(this, list_aza);  // Add

    RecyclerView recyclerView_aza = findViewById(R.id.rec_aza);

    recyclerView_aza.setAdapter(adapter);

    recyclerView_aza.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    btn__add__field.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            EditText edtname = findViewById(R.id.edt__person__name);

            EditText edt_semat_aza = findViewById(R.id.edt__person__semat);

            EditText edt_vorood_aza = findViewById(R.id.edt__person__enter);

            EditText edt_khorooj_aza = findViewById(R.id.edt__person__out);

            String name_aza = edtname.getText().toString();

            String semat_aza = edt_semat_aza.getText().toString();

            String saat_vorood_aza = edt_vorood_aza.getText().toString();

            String saat_khorooj_aza = edt_khorooj_aza.getText().toString();

            long result = dataBase_aza.insert_info(name_aza, semat_aza, saat_vorood_aza, saat_khorooj_aza,id);

            Model_aza modelAza = new Model_aza();   // Add

            modelAza.setName_aza(name_aza);// Add

            modelAza.setSemat_aza(semat_aza);// Add

            modelAza.setSaaat_vorood_aza(saat_vorood_aza);// Add

            modelAza.setSaat_khorooj_aza(saat_khorooj_aza);// Add

            list_aza.add(modelAza);// Add

            adapter.notifyDataSetChanged();// Add

            Toast.makeText(Activity_Gozaresh_giri.this, result + "", Toast.LENGTH_SHORT).show();

        }

    });

    Cursor cursor1 = dataBase_aza.cursor(id);

    for (cursor1.moveToFirst(); !cursor1.isAfterLast(); cursor1.moveToNext()) {

        Model_aza modelAza = new Model_aza();

        modelAza.setName_aza(cursor1.getString(1));

        modelAza.setSemat_aza(cursor1.getString(2));

        modelAza.setSaaat_vorood_aza(cursor1.getString(3));

        modelAza.setSaat_khorooj_aza(cursor1.getString(4));

        list_aza.add(modelAza);

    }

    adapter.notifyDataSetChanged();// Add

}


查看完整回答
反对 回复 2023-07-13
?
潇湘沐

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

添加条目后,只需通知数据集更改为适配器,如下所示


 @Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_gozaresh_giri);

    btn__add__field1 = findViewById(R.id.btn__add__field1);

    btn__add__field = findViewById(R.id.btn__add__field);

    int id=getIntent().getIntExtra("id",0);

    list_aza = new ArrayList<>();

    Log.d(TAG, "onCreate: onclicked");



    rec_adaptor_aza = new Rec_adaptor_aza(this, list_aza);

    RecyclerView recyclerView_aza = findViewById(R.id.rec_aza);


    recyclerView_aza.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

     recyclerView_aza.setAdapter(rec_adaptor_aza);



    Cursor cursor1 = dataBase_aza.cursor(id);

    for (cursor1.moveToFirst(); !cursor1.isAfterLast(); cursor1.moveToNext()) {

        Model_aza modelAza = new Model_aza();

        modelAza.setName_aza(cursor1.getString(1));

        modelAza.setSemat_aza(cursor1.getString(2));

        modelAza.setSaaat_vorood_aza(cursor1.getString(3));

        modelAza.setSaat_khorooj_aza(cursor1.getString(4));

        list_aza.add(modelAza);

    }


    rec_adaptor_aza.notifyDataSetChanged();


    btn__add__field.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            EditText edtname = findViewById(R.id.edt__person__name);

            EditText edt_semat_aza = findViewById(R.id.edt__person__semat);

            EditText edt_vorood_aza = findViewById(R.id.edt__person__enter);

            EditText edt_khorooj_aza = findViewById(R.id.edt__person__out);

            String name_aza = edtname.getText().toString();

            String semat_aza = edt_semat_aza.getText().toString();

            String saat_vorood_aza = edt_vorood_aza.getText().toString();

            String saat_khorooj_aza = edt_khorooj_aza.getText().toString();

            long result = dataBase_aza.insert_info(name_aza, semat_aza, saat_vorood_aza, saat_khorooj_aza,id);


            rec_adaptor_aza.notifyDataSetChanged(); // change here

            Toast.makeText(Activity_Gozaresh_giri.this, result + "", Toast.LENGTH_SHORT).show();

        }

    });


}


查看完整回答
反对 回复 2023-07-13
  • 4 回答
  • 0 关注
  • 139 浏览

添加回答

举报

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