亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

回收器視圖不刷新和顯示數據

回收器視圖不刷新和顯示數據

尚方寶劍之說 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 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號