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

在我的回收器视图中,当添加数据时,回收器视图不会显示它,直到用户关闭活动并再次打开它。我认为这与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);


        }

    }

}


尚方宝剑之说
浏览 107回答 4
4回答

GCT1015

只需添加以下代码,在您的clickListner中,您需要在列表中添加相同的对象,添加后您需要使用notifyDataSetChanged() 通知适配器有关插入的项目。btn__add__field.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edtname = findViewById(R.id.edt__person__name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_semat_aza = findViewById(R.id.edt__person__semat);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_vorood_aza = findViewById(R.id.edt__person__enter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_khorooj_aza = findViewById(R.id.edt__person__out);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name_aza = edtname.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String semat_aza = edt_semat_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String saat_vorood_aza = edt_vorood_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String saat_khorooj_aza = edt_khorooj_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long result = dataBase_aza.insert_info(name_aza, semat_aza, saat_vorood_aza, saat_khorooj_aza,id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Activity_Gozaresh_giri.this, result + "", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Adding entered data in list*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Model_aza mm=new Model_aza();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mm.setName_aza(name_aza);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mm.setSemat_aza(semat_aza);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mm.setSaaat_vorood_aza(saat_vorood_aza);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mm.setSaat_khorooj_aza(saat_khorooj_aza);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list_aza.add(mm);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rec_adaptor_aza.notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });

慕侠2389804

在您的 中layoutmanager设置之前adapter设置,如下所示recylerviewactivity&nbsp; &nbsp; RecyclerView recyclerView_aza = findViewById(R.id.rec_aza);&nbsp; &nbsp; // set layout manager before set adapter&nbsp; &nbsp; recyclerView_aza.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));&nbsp; &nbsp; recyclerView_aza.setAdapter(new Rec_adaptor_aza(this, list_aza));&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

海绵宝宝撒

像下面这样使用它:-@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_gozaresh_giri);&nbsp; &nbsp; btn__add__field1 = findViewById(R.id.btn__add__field1);&nbsp; &nbsp; btn__add__field = findViewById(R.id.btn__add__field);&nbsp; &nbsp; int id=getIntent().getIntExtra("id",0);&nbsp; &nbsp; list_aza = new ArrayList<>();&nbsp; &nbsp; Rec_adaptor_aza&nbsp; adapter = new Rec_adaptor_aza(this, list_aza);&nbsp; // Add&nbsp; &nbsp; RecyclerView recyclerView_aza = findViewById(R.id.rec_aza);&nbsp; &nbsp; recyclerView_aza.setAdapter(adapter);&nbsp; &nbsp; recyclerView_aza.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));&nbsp; &nbsp; btn__add__field.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edtname = findViewById(R.id.edt__person__name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_semat_aza = findViewById(R.id.edt__person__semat);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_vorood_aza = findViewById(R.id.edt__person__enter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_khorooj_aza = findViewById(R.id.edt__person__out);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name_aza = edtname.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String semat_aza = edt_semat_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String saat_vorood_aza = edt_vorood_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String saat_khorooj_aza = edt_khorooj_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long result = dataBase_aza.insert_info(name_aza, semat_aza, saat_vorood_aza, saat_khorooj_aza,id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Model_aza modelAza = new Model_aza();&nbsp; &nbsp;// Add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelAza.setName_aza(name_aza);// Add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSemat_aza(semat_aza);// Add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSaaat_vorood_aza(saat_vorood_aza);// Add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSaat_khorooj_aza(saat_khorooj_aza);// Add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list_aza.add(modelAza);// Add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.notifyDataSetChanged();// Add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Activity_Gozaresh_giri.this, result + "", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; Cursor cursor1 = dataBase_aza.cursor(id);&nbsp; &nbsp; for (cursor1.moveToFirst(); !cursor1.isAfterLast(); cursor1.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; Model_aza modelAza = new Model_aza();&nbsp; &nbsp; &nbsp; &nbsp; modelAza.setName_aza(cursor1.getString(1));&nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSemat_aza(cursor1.getString(2));&nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSaaat_vorood_aza(cursor1.getString(3));&nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSaat_khorooj_aza(cursor1.getString(4));&nbsp; &nbsp; &nbsp; &nbsp; list_aza.add(modelAza);&nbsp; &nbsp; }&nbsp; &nbsp; adapter.notifyDataSetChanged();// Add}

潇湘沐

添加条目后,只需通知数据集更改为适配器,如下所示&nbsp;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_gozaresh_giri);&nbsp; &nbsp; btn__add__field1 = findViewById(R.id.btn__add__field1);&nbsp; &nbsp; btn__add__field = findViewById(R.id.btn__add__field);&nbsp; &nbsp; int id=getIntent().getIntExtra("id",0);&nbsp; &nbsp; list_aza = new ArrayList<>();&nbsp; &nbsp; Log.d(TAG, "onCreate: onclicked");&nbsp; &nbsp; rec_adaptor_aza = new Rec_adaptor_aza(this, list_aza);&nbsp; &nbsp; RecyclerView recyclerView_aza = findViewById(R.id.rec_aza);&nbsp; &nbsp; recyclerView_aza.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));&nbsp; &nbsp; &nbsp;recyclerView_aza.setAdapter(rec_adaptor_aza);&nbsp; &nbsp; Cursor cursor1 = dataBase_aza.cursor(id);&nbsp; &nbsp; for (cursor1.moveToFirst(); !cursor1.isAfterLast(); cursor1.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; Model_aza modelAza = new Model_aza();&nbsp; &nbsp; &nbsp; &nbsp; modelAza.setName_aza(cursor1.getString(1));&nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSemat_aza(cursor1.getString(2));&nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSaaat_vorood_aza(cursor1.getString(3));&nbsp; &nbsp; &nbsp; &nbsp; modelAza.setSaat_khorooj_aza(cursor1.getString(4));&nbsp; &nbsp; &nbsp; &nbsp; list_aza.add(modelAza);&nbsp; &nbsp; }&nbsp; &nbsp; rec_adaptor_aza.notifyDataSetChanged();&nbsp; &nbsp; btn__add__field.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edtname = findViewById(R.id.edt__person__name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_semat_aza = findViewById(R.id.edt__person__semat);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_vorood_aza = findViewById(R.id.edt__person__enter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText edt_khorooj_aza = findViewById(R.id.edt__person__out);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name_aza = edtname.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String semat_aza = edt_semat_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String saat_vorood_aza = edt_vorood_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String saat_khorooj_aza = edt_khorooj_aza.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long result = dataBase_aza.insert_info(name_aza, semat_aza, saat_vorood_aza, saat_khorooj_aza,id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rec_adaptor_aza.notifyDataSetChanged(); // change here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Activity_Gozaresh_giri.this, result + "", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java