how to refresh custom listview using baseadapter in android

后端 未结 5 751
无人及你
无人及你 2020-12-08 23:37

sir, how can i refresh my custom listview using baseadapter. i don\'t know what to place, or where to place it in my code. please help me. thanks in advance



        
相关标签:
5条回答
  • 2020-12-09 00:05

    I solved this issue adding this function to my custom adapter

    public void newCursor(Cursor cursor)
     {
      this.cursor=cursor;
      this.notifyDataSetChanged();
     }
    

    From my main class I create a new cursor doing a re-query to database and then send to my custom adapter via this function.

    good luck

    0 讨论(0)
  • 2020-12-09 00:10

    create one custom method in BaseAdapter

    like:

     public void updateAdapter(ArrayList<Contact> arrylst) {
            this.arrylst= arrylst;
    
            //and call notifyDataSetChanged
            notifyDataSetChanged();
        }
    

    and this function call where u want to call: e.g

    adapterObject.updateAdapter(Here pass ArrayList);

    done.

    0 讨论(0)
  • 2020-12-09 00:12

    Two options: either hold onto the reference for the ArrayList that you passed into the constructor so you can modify the actual list data later (since the list isn't copied, modifying the data outside the Adapter still updates the pointer the Adapter is referencing), or rewrite the Adapter to allow the list to be reset to another object.

    In either case, after the ArrayList has changed, you must call notifyDataSetChanged() to update your ListView with the changes. This can be done inside or outside the adapter. So, for example:

    public class MyCustomBaseAdapter extends BaseAdapter {
        //TIP: Don't make this static, that's just a bad idea
        private ArrayList<Contact> searchArrayList;
    
        private LayoutInflater mInflater;
    
        public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults) {
            searchArrayList = initialResults;
            mInflater = LayoutInflater.from(context);
        }
    
        public void updateResults(ArrayList<Contact> results) {
            searchArrayList = results;
            //Triggers the list update
            notifyDataSetChanged();
        }
    
        /* ...The rest of your code that I failed to copy over... */
    }
    

    HTH

    0 讨论(0)
  • 2020-12-09 00:20

    Simply with BaseAdapter, no need to use context

    listsOfNotes.remove(listsOfNotes.get(position));
    notifyDataSetChanged();
    

    just place this code on setOnClickListner

    0 讨论(0)
  • 2020-12-09 00:23

    Thanks guys with solution above worked for me. I am calling listupdate method in every event

      public void updateResults(List<TalebeDataUser> results) {
        talebeList = results;
        //Triggers the list update
        notifyDataSetChanged();
    }
    

    and after updatng list I also refreshing my button action in every touch. For instance I have lots of buttons to click in my listview item so every touch chaging others style

        private void setColor(TalebeDataUser talebeDataUser) {
        if (talebeDataUser.isVar()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_secili);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
        } else if (talebeDataUser.isGorevli()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_secili);
            holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
        } else if (talebeDataUser.isYok()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mYok.setBackgroundResource(R.drawable.aw_secili);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
        } else if (talebeDataUser.isIzinli()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_secili);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
        } else if (talebeDataUser.isHatimde()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_secili);
        }
    
    }
    

    Just an example inside one of my button

      holder.mYok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //talebeList.remove(currentTalebe);
                setOgrenciNameByDurum(talebeList.get(i));
                talebeList.get(i).setYok(true);
                //setOgrenciNameByDurum(currentTalebe);
                talebeList.get(i).setVar(false);
                talebeList.get(i).setGorevli(false);
                talebeList.get(i).setIzinli(false);
                talebeList.get(i).setHatimde(false);
                updateResults(talebeList);
                setColor(talebeList.get(i));
                //saveCurrentTalebeOnShare(currentTalebe);
            }
        });
    

    talebeList is just of List<MyModel> talebeList

    0 讨论(0)
提交回复
热议问题