既存のテーブルfirebaseを更新whille新しいテーブルが作成されたばかり

アマンBaid:

イム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();
}

これは、特定の書籍の詳細onlickが送信されると、私はそれを更新したい場所をページレイアウトブックの詳細です

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");

                }
            }
        });

    }

新しいテーブルは、第三のものと同じに作成されています

Marsad Maqsood:

このコードを使用しAdminBookdetails.java

                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();

                HashMap<String, Object> map = new HashMap<>();
                map.put("bookName", Title);
                map.put("author", Author);
                map.put("publisher", Publisher);
                map.put("link", Link);
                map.put("desc", Desc);
                map.put("id", Uid);

                databaseReference.child(Uid).updateChildren(map);  //To Update value

アップデートの使用に

databaseReference.child(Uid).updateChildren(map);

使用を追加するには

databaseReference.child(Uid).setValue(map);

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=341613&siteId=1