я пытаюсь обновить книги в Firebase, но всякий раз, когда я нажимаю кнопку обновить, создается новая таблица, это страница поиска AdminSearch. java
public class AdminSearch extends AppCompatActivity {
EditText searchbarr;
RecyclerView recyclerVieww;
DatabaseReference referencee;
ArrayList<String> BookNameLists;
ArrayList<String> AuthorNameLists;
ArrayList<String> PicLists;
ArrayList<String> PublisherLists;
ArrayList<String> LinkLists;
ArrayList<String> DescriptionLists;
ArrayList<String>UidList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adminsearch);
searchbarr = (EditText) findViewById(R.id.searchbar);
referencee = FirebaseDatabase.getInstance().getReference();
referencee.keepSynced(true);
recyclerVieww = (RecyclerView) findViewById(R.id.rv);
recyclerVieww.setHasFixedSize(true);
recyclerVieww.setLayoutManager(new LinearLayoutManager(this));
recyclerVieww.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
BookNameLists = new ArrayList<>();
PublisherLists = new ArrayList<>();
AuthorNameLists = new ArrayList<>();
LinkLists = new ArrayList<>();
PicLists = new ArrayList<>();
DescriptionLists=new ArrayList<>();
UidList=new ArrayList<>();
searchbarr.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
setAdapter(s.toString());
}
else{
BookNameLists.clear();
AuthorNameLists.clear();
PicLists.clear();
PublisherLists.clear();
DescriptionLists.clear();
UidList.clear();
}
}
private void setAdapter(final String searchedString) {
referencee.child("books").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
BookNameLists.clear();
AuthorNameLists.clear();
PicLists.clear();
PublisherLists.clear();
DescriptionLists.clear();
UidList.clear();
int counter=0;
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
String uid = snapshot.getKey();
String desc = snapshot.child("Desc").getValue(String.class);
String bookname = snapshot.child("bookname").getValue(String.class);
String author = snapshot.child("author").getValue(String.class);
String image = snapshot.child("image").getValue(String.class);
String publisher = snapshot.child("Publisher").getValue(String.class);
String link=snapshot.child("link").getValue(String.class);
String decscription=snapshot.child("Desc").getValue(String.class);
try {
if ((bookname.toLowerCase().contains(searchedString.toLowerCase())) || (author.toLowerCase().contains(searchedString.toLowerCase()))) {
UidList.add(uid);
BookNameLists.add(bookname);
AuthorNameLists.add(author);
PublisherLists.add(publisher);
PicLists.add(image);
DescriptionLists.add(decscription);
LinkLists.add(link);
counter++;
}
if(BookNameLists.isEmpty() && AuthorNameLists.isEmpty()){
Toast.makeText(getApplicationContext(),"not found",Toast.LENGTH_LONG).show();
}
}
catch (Exception e){
}
if(counter==15){
break;
}
AdminSearchAdapter adminSearchAdapter = new AdminSearchAdapter(AdminSearch.this, BookNameLists, AuthorNameLists, PicLists, PublisherLists,DescriptionLists,LinkLists,UidList);
recyclerVieww.setAdapter(adminSearchAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
класс адаптера для страницы поиска AdminSearchAdapter. java
public class AdminSearchAdapter extends RecyclerView.Adapter<AdminSearchAdapter.EditViewHolder> {
Context c;
ArrayList<String> BookNameLists;
ArrayList<String> AuthorNameLists;
ArrayList<String> PicLists;
ArrayList<String> PublisherLists;
ArrayList<String>DescriptionLists;
ArrayList<String>LinkLists;
LinearLayout booklayoutt;
ArrayList<String>UidList;
FirebaseDatabase db;
DatabaseReference referencee;
public String key;
class EditViewHolder extends RecyclerView.ViewHolder{
ImageView bookimages;
TextView booknamess, authornamess,publisherss;
public EditViewHolder(@NonNull View itemView) {
super(itemView);
bookimages = itemView.findViewById(R.id.Bookimg);
booknamess = itemView.findViewById(R.id.BookName);
authornamess = itemView.findViewById(R.id.AuthorName);
publisherss = itemView.findViewById(R.id.Publications);
booklayoutt=itemView.findViewById(R.id.LinLayout);
referencee = FirebaseDatabase.getInstance().getReference();
referencee.keepSynced(true);
}
}
public AdminSearchAdapter(Context c1, ArrayList<String> bookNameLists, ArrayList<String> authorNameLists, ArrayList<String> picLists, ArrayList<String> publisherLists, ArrayList<String> descriptionLists, ArrayList<String> linkLists,ArrayList<String> uidList) {
c = c1;
BookNameLists = bookNameLists;
AuthorNameLists = authorNameLists;
PicLists = picLists;
PublisherLists=publisherLists;
DescriptionLists=descriptionLists;
LinkLists=linkLists;
UidList=uidList;
}
@NonNull
@Override
public AdminSearchAdapter.EditViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(c).inflate(R.layout.activity_search_layout,parent,false);
return new AdminSearchAdapter.EditViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final EditViewHolder holder, final int position) {
holder.booknamess.setText(BookNameLists.get(position));
holder.authornamess.setText(AuthorNameLists.get(position));
holder.publisherss.setText(PublisherLists.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(c, AdminBookdetails.class);
i.putExtra("uid",UidList.get(position));
i.putExtra("booknamess",BookNameLists.get(position));
i.putExtra("Images",PicLists.get(position));
i.putExtra("author_names",AuthorNameLists.get(position));
i.putExtra("publisher_names",PublisherLists.get(position));
i.putExtra("descriptions",DescriptionLists.get(position));
i.putExtra("links",LinkLists.get(position));
c.startActivity(i);
}
});
Glide.with(c).asBitmap().load(PicLists.get(position)).placeholder(R.mipmap.ic_launcher_round).into(holder.bookimages);
}
@Override
public int getItemCount() {
return BookNameLists.size();
}
это страница макета сведений о книге, на которую отправляются заданные c сведения о книгах и где я хочу обновить
AdminBookdetails. java
public class AdminBookdetails extends AdminSearch {
EditText titles,authors,pubs,linkss,descss,bookid;
Bookdeets bookdeets;
String key;
Button update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adminbooksdetails);
titles = (EditText) findViewById(R.id.bknames);
authors = (EditText) findViewById(R.id.anames);
pubs = (EditText) findViewById(R.id.pnames);
linkss = (EditText) findViewById(R.id.bklinks);
descss = (EditText) findViewById(R.id.bkdescriptions);
update=(Button)findViewById(R.id.updatebtn);
bookid=(EditText)findViewById(R.id.uid);
bookdeets=new Bookdeets();
final DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference().child("books");
String uid = getIntent().getStringExtra("uid");
bookid.setText(uid,TextView.BufferType.EDITABLE);
String Bname = getIntent().getStringExtra("booknamess");
titles.setText(Bname,TextView.BufferType.EDITABLE);
String Author = getIntent().getStringExtra("author_names");
authors.setText(Author,TextView.BufferType.EDITABLE);
String publisher = getIntent().getStringExtra("publisher_names");
pubs.setText(publisher,TextView.BufferType.EDITABLE);
final String Link = getIntent().getStringExtra("links");
linkss.setText(Link,TextView.BufferType.EDITABLE);
String Desc = getIntent().getStringExtra("descriptions");
descss.setText(Desc);
descss.setMovementMethod(new ScrollingMovementMethod());
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Title = titles.getText().toString().trim();
String Author = authors.getText().toString().trim();
String Publisher = pubs.getText().toString().trim();
String Link = linkss.getText().toString().trim();
String Desc = descss.getText().toString().trim();
String Uid=bookid.getText().toString().trim();
Toast.makeText(getApplicationContext(),""+Title,Toast.LENGTH_SHORT).show();
bookdeets.setBookname(titles.getText().toString().trim());
bookdeets.setAuthor(authors.getText().toString().trim());
bookdeets.setPublisher(pubs.getText().toString().trim());
bookdeets.setLink(linkss.getText().toString().trim());
bookdeets.setDesc(descss.getText().toString().trim());
bookdeets.setId(bookid.getText().toString().trim());
databaseReference.push().setValue(bookdeets);
if(TextUtils.isEmpty(Title) ||TextUtils.isEmpty(Author)||TextUtils.isEmpty(Publisher)||TextUtils.isEmpty(Link)||TextUtils.isEmpty(Desc)){
titles.setError("Field cant be empty");
}
}
});
}