更新dataBase后更新listView?

时间:2017-04-17 13:36:57

标签: android listview firebase firebase-realtime-database adapter

我有一个listView的书籍,我通过点击项目及其工作更新listview,但我有重复项目,旧项目和新项目。在我的数据库中,我只有新数据。我怎样才能修改我的列表视图?我正在使用FireBase数据库。

使用我的活动来源更新:

ctmfw

适配器:

public class ListOfMyBookActivity extends AppCompatActivity  {

//The elements are initialized here

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

        listView = (ListView) findViewById(R.id.list_book);

        feedItems = new ArrayList<Book>();
        dataBD= FirebaseDatabase.getInstance();
        dataBaseD=FirebaseDatabase.getInstance();
        updateDB=FirebaseDatabase.getInstance();
        ref = dataBD.getReference("books");
        refdelete=dataBaseD.getReference("books");
        refUpdate=updateDB.getReference("books");
        auth = FirebaseAuth.getInstance();
        mDatabase = FirebaseDatabase.getInstance().getReference("books");


            ref.orderByChild("date_creation").limitToLast(10).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {



                    List<String> list = new ArrayList<String>();
                    for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {

                        Book valueBook = dataSnap.getValue(Book.class);
                        keyId = dataSnap.getKey();

                        String userBd = valueBook.getId_user();
                        if (userBd.equalsIgnoreCase(id_user)) {
                            String titreLivreToDisplay = valueBook.getNom_livre();
                            String descLivreToDisplay = valueBook.getDesc_livre();
                            String prixLivreToDisplay = valueBook.getPrix_livre();
                            String timeToDisplay = valueBook.getDate_creation();
                            String filePathToDiplay = valueBook.getChemin_image();
                            String villeToDisplay = valueBook.getVille_livre();
                            String typeAnnToDisplat = valueBook.getType_annonce_selected();
                            String bookid = valueBook.getId_book();



                            item = new Book();
                            item.setNom_livre(titreLivreToDisplay);
                            item.setDesc_livre(descLivreToDisplay);
                            item.setPrix_livre(prixLivreToDisplay);
                            item.setDate_creation(timeToDisplay);
                            item.setChemin_image(filePathToDiplay);
                            item.setVille_livre(villeToDisplay);
                            item.setType_annonce_selected(typeAnnToDisplat);
                            item.setId_book(bookid);
                            item.setKeyIdNode(keyId);

                            feedItems.add(item);


                        }

                        Collections.reverse(feedItems);


                        listAdapter = new MyBookListAdapter(ListOfMyBookActivity.this, feedItems);

                        listView.setAdapter(listAdapter);

                        listView.setOnItemClickListener(myClickListener);

                        listAdapter.notifyDataSetChanged();


                    }}



            });


    }


    public AdapterView.OnItemClickListener myClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                                final int position, long arg3) {
            deleteItem = position;
            // The AlertDialog is created programmatically
            AlertDialog.Builder alert = new AlertDialog.Builder(arg1.getContext());

            nameBookEdtDlg.setText(nom);
            descBookEdtDlg.setText(desc);
            prixBookEdtDlg.setText(prix);

                alert.setPositiveButton("Update ",
                        new DialogInterface.OnClickListener()

                {
                    public void onClick (DialogInterface dialog,
                    int whichButton){


                    Query query = refUpdate.orderByChild("id_book").equalTo(bookID);

                    query.addListenerForSingleValueEvent(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            prixSt=prixBookEdtDlg.getText().toString();
                            descSt=descBookEdtDlg.getText().toString();
                            namSt=nameBookEdtDlg.getText().toString();


                            Map<String, Object> result = new HashMap<String, Object>();
                            result.put("nom_livre", namSt);
                            result.put("desc_livre", descSt);
                            result.put("prix_livre", prixSt);
                            result.put("date_creation", currentDateandTimeUpdate);
                            refUpdate.child(keyId).updateChildren(result);

                            listAdapter.notifyDataSetChanged();

                        } });

            AlertDialog alertDialog = alert.create();

                try{alertDialog.show();} } };}
                }

}

我的问题没有重复我尝试了提出的解决方案,但它不起作用。

5 个答案:

答案 0 :(得分:2)

不确定这是否是你的问题,但大部分的firebase代码都不是必需的......

请注意,您拨打getNom_livre(和其他人),然后使用完全相同的值拨打setNom_livre(和其他人)? 如果您已经拥有new Book,也可以制作Book valueBook = dataSnap.getValue(Book.class); // This is a Book keyId = dataSnap.getKey(); String userBd = valueBook.getId_user(); if (userBd.equalsIgnoreCase(id_user)) { String titreLivreToDisplay = valueBook.getNom_livre(); ... item = new Book(); // Why do you need a new one? item.setNom_livre(titreLivreToDisplay); ... feedItems.add(item); }

listAdapter = new MyBookListAdapter(ListOfMyBookActivity.this, feedItems);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(myClickListener);

ref.orderByChild("date_creation")...

首先,您应该在firebase方法之前移动它。无需在每次数据更新时重新创建适配器。

ref.orderByChild("date_creation")
    .limitToLast(10)
    .addValueEventListener(new ValueEventListener() {
         @Override
         public void onDataChange(DataSnapshot dataSnapshot) {
             // Remove old data
             feedItems.clear(); 

             // Add new data
             for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {

                 Book valueBook = dataSnap.getValue(Book.class);
                 keyId = dataSnap.getKey();

                  String userBd = valueBook.getId_user();
                  if (userBd.equalsIgnoreCase(id_user)) {
                      valueBook.setKeyIdNode(keyId);
                      feedItems.add(valueBook);
                  }
             }
             Collections.reverse(feedItems);
             listAdapter.notifyDataSetChanged(); // Tell adapter of new data
  

但我有重复项目,旧项目和新项目

您只需清除适配器即可删除旧数据并添加新内容

select  "Title"
from "Tickets"
where
    "TicketID" = any (one)
    or
    cardinality(one) = 0;

答案 1 :(得分:1)

在从feedItems.clear()获取更新数据之前使用firebase

试试这个:

ref.orderByChild("date_creation").limitToLast(10).addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        feedItems.clear();

        .............
        ..................
    }
});

答案 2 :(得分:0)

    feedItems.remove(position);
   // listView.invalidateViews();
     arrayAdapter.notifyDataSetChanged();

答案 3 :(得分:0)

在你的适配器中使用它

public void dataSetChanged(ArrayList<Movie>list)
    {
        this.list=list;
        this.notifyDataSetChanged();
    }

并像这样使用它 upadte你的列表数据,然后使用下面的行

customListViewAdapter.dataSetChanged(movieList);

答案 4 :(得分:-1)

用它来更新你的清单 notifyDataSetChanged()