单击对话框按钮时如何刷新recyclerview? [英] how to refresh recyclerview when I click the dialog button?

查看:111
本文介绍了单击对话框按钮时如何刷新recyclerview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击alertdialog保存按钮时,我想将新项目添加到recyclerview并刷新它以同时显示新项目.... 它从sqlite数据库中获取数据.并从同一数据库

I want when I click the alertdialog save button add new item to recyclerview and refresh it to show the new item at the same time .... its take the data from sqlite database.and retrieve from same DB

 EditText title, description;
    Button save;
    RecyclerView recycler;

    insertDatadb db = new insertDatadb(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        recycler = findViewById(R.id.data_recycle);
        adapter=new Adapter(this,db.getData());
        recycler.setItemAnimator(new DefaultItemAnimator());
        showData();
        recycler.setAdapter(adapter);
    }


    public boolean onCreateOptionsMenu (Menu menu){

        getMenuInflater().inflate(R.menu.mymenu,menu);
        return super.onCreateOptionsMenu(menu);


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.menu_add:
                displaydialog();

        }
        return super.onOptionsItemSelected(item);
    }




    public void displaydialog(){
        final View myview = getLayoutInflater().inflate(R.layout.custom_dialog, null);

        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                .setTitle("Add New Task")
                .setMessage("What do You Want to do ?")
                .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                    @Override

                    public void onClick(DialogInterface dialogInterface, int i) {

                        title = myview.findViewById(R.id.et_title);
                        description = myview.findViewById(R.id.et_description);
                        String mytitle = title.getText().toString();
                        String mydesc = description.getText().toString();




                        if (mytitle.isEmpty() && mydesc.isEmpty()) {
                            Toast.makeText(MainActivity.this, "No Data ", Toast.LENGTH_LONG).show();
                        } else {

                            boolean result = db.insertData(mytitle, mydesc);

                            if (result == true) {
                                Toast.makeText(MainActivity.this, "Add Successfully", Toast.LENGTH_LONG).show();
                                title.setText("");
                                description.setText("");



                            } else {
                                Toast.makeText(MainActivity.this, "Add Failed", Toast.LENGTH_LONG).show();
                            }
                        }
                    }



                })
                .setNegativeButton("Cancel",null);



        builder.setView(myview);
        AlertDialog dialog = builder.create();
        dialog.show();

    }
    public void showData ( ) {
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter);

    }

}

这是.适配器和支架类..................................... ........ ...................... ...... . .............. ...... ...... ........ ......

this is. the adapter and holder class ............................ .................... ...................... ......... . .............. ......... ........... ........ ......

`上下文上下文; ArrayList项目; 私有ArrayList数据;

` Context context; ArrayList items; private ArrayList data;

public Adapter(Context context, ArrayList<MyHolder> items) {

    this.context=context;
    this.items=items;

}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    LayoutInflater inflate = LayoutInflater.from(context);
    View v = inflate.inflate(R.layout.recyclerlayout,viewGroup,false);
    MyHolder holder=new MyHolder(v);

    return holder;
}



@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
    myHolder.txt.setText((CharSequence) items.get(i));

    myHolder.setItemClickListenet(new refresh_recycle() {
        @Override
        public void onItemClick(int pos) {

        }
    });


}


@Override
public int getItemCount() {
    return items.size();
}






public class MyHolder extends RecyclerView.ViewHolder implements  View.OnClickListener{


    TextView txt;
    refresh_recycle refresh ;


    public MyHolder(@NonNull View itemView) {
        super(itemView);
        txt=itemView.findViewById(R.id.txt_recycle);

    }

    public void setItemClickListenet(refresh_recycle refresh)
    {
        this.refresh=refresh;

    }

    @Override
    public void onClick(View view) {
        this.refresh.onItemClick(getLayoutPosition());
    }
}
public void setData(ArrayList<MyHolder> data){
    this.data = data;
    notifyDataSetChanged();
    // where this.data is the recyclerView's dataset you are 
    // setting in adapter=new Adapter(this,db.getData());
}

}`......

推荐答案

将新数据添加到数据库后,必须重置recyclerView中的数据并调用notifyDataSetChanged().像这样

Once you add the new data to your db, you will have to reset the data in your recyclerView and call notifyDataSetChanged() . You would need a setData method in your recyclerView like so

public void setData(List<YourDataType> data){
   this.data = data;
   notifyDataSetChanged();
 // where this.data is the recyclerView's dataset you are 
 // setting in adapter=new Adapter(this,db.getData());
}

添加新数据时,必须在代码中添加以下几行.

When you add new data, you will have to add the following lines to your code.

if (result == true) {
    Toast.makeText(MainActivity.this, "Add 
                    Successfully",Toast.LENGTH_LONG).show();
    title.setText("");
    description.setText("");
    adapter.setData(db.getData);
    //this will reset your recyclerView's data set and notify the change
    //and reload the list 
}

这篇关于单击对话框按钮时如何刷新recyclerview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆