问题
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 ............................ .................... ...................... ......... . .............. ......... ........... ........ ......
` 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());
}
}`......
回答1:
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
}
回答2:
In simplest way, you must define your DataList
and your adapter
as fields and after showing dialog, add new object(s)
to your DataList
. Then call adapter.notifyDataSetChanged()
.
It worked for me, but there are some better ways.
来源:https://stackoverflow.com/questions/51841180/how-to-refresh-recyclerview-when-i-click-the-dialog-button