当我单击alertdialog保存按钮时,我想将新项目添加到recyclerview并刷新它以同时显示新项目...。
它从sqlite
数据库中获取数据。并从同一数据库中检索
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);
}
}
这是。适配器和支架类..................................... .................... ...................... ...... 。 .............. ...... ........... ........ ......
`上下文上下文; ArrayList项目; 私有ArrayList数据;
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());
}
}`......
答案 0 :(得分:1)
将新数据添加到数据库后,必须重置recyclerView
中的数据并调用notifyDataSetChanged()
。您将需要在recyclerView
中使用setData方法,就像这样
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());
}
添加新数据时,必须在代码中添加以下行。
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
}
答案 1 :(得分:0)
以最简单的方式,您必须将DataList
和adapter
定义为字段,并在显示对话框后,将新的object(s)
添加到DataList
中。然后致电adapter.notifyDataSetChanged()
。
它对我有用,但是有一些更好的方法。