android - RecyclerView Displays only last objects in List -
i trying display data in recyclerview
. have created adapter handle recyclerview
s different activities. issue facing displaying data appropriately. far can display last elements in list. in case, last candidate each contestedoffice. have attached code both activity , adapter screenshot of output.
my adapter class
public class recycleradapter extends recyclerview.adapter<recyclerview.viewholder> { private static final string logcat = recycleradapter.class.getsimplename(); private static final int view_type_vote = 0; private static final int view_type_preview = 1; private list candidateslist; private linearlayout checkboxparent; private voteactivity voteactivity; private context context; public recycleradapter(list candidateslist) //generic list { this.candidateslist = candidateslist; } @override public int getitemviewtype(int position) { if (candidateslist.get(position) instanceof candidate) { return view_type_preview; } else { return view_type_vote; } } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { context = parent.getcontext(); if (viewtype == view_type_preview) { view preview = layoutinflater.from(parent.getcontext()) .inflate(r.layout.item_candidate_recycler, parent, false); return new candidateviewholder(preview); // view holder candidates items } else if (viewtype == view_type_vote) { view ballot = layoutinflater.from(parent.getcontext()) .inflate(r.layout.item_ballot_view, parent, false); //false return new ballotviewholder(ballot); // view holder contestedoffice items } return null; } @override public void onbindviewholder(final recyclerview.viewholder holder, final int position) { final int itemtype = getitemviewtype(position); if (itemtype == view_type_preview) { candidateviewholder.class.cast(holder).binddata((candidate) candidateslist.get(position)); } else if (itemtype == view_type_vote) { ballotviewholder.class.cast(holder).binddata((contestedoffice) candidateslist.get(position)); } } @override public int getitemcount() { log.d(logcat, " size: " + candidateslist.size()); return candidateslist.size(); } /** * viewholder class */ private class ballotviewholder extends recyclerview.viewholder implements view.onclicklistener { private appcompattextview vote_candidate_name; private appcompattextview vote_candidate_details; private appcompattextview vote_candidate_party; ballotviewholder(view view) { super(view); checkboxparent = (linearlayout) view.findviewbyid(r.id.checkbox_layout_parent); vote_candidate_name = (appcompattextview) view.findviewbyid(r.id.vote_candidate_name) vote_candidate_party = (appcompattextview) view.findviewbyid(r.id.vote_candidate_party); vote_candidate_details = (appcompattextview) view.findviewbyid(r.id.vote_candidate_details); } void binddata(contestedoffice candidate) { int length = candidate.getlist().size(); log.d(logcat, "ballotlist size:" + length); linearlayout.layoutparams params = new linearlayout.layoutparams( recyclerview.layoutparams.wrap_content, recyclerview.layoutparams.wrap_content); //params.setmargins(10, 10, 10, 10); params.gravity = gravity.end; checkbox checkbox = null; (int = 0; < length; i++) { vote_candidate_name.settext(candidate.getlist().get(i).getcandidatename()); vote_candidate_party.settext(candidate.getlist().get(i).getparty()); vote_candidate_details.settext(candidate.getlist().get(i).getdetails()); //create checkboxes each recycler view created checkbox = new checkbox(context); checkbox.settag(candidate.getlist().get(i).getcandidateid()); checkbox.setid(candidate.getlist().get(i).getcandidateid()); //checkbox.settext(r.string.checkbox_message); checkbox.settext(string.valueof(candidate.getlist().get(i).getcandidateid())); checkbox.setbackgroundcolor(color.red); log.d(logcat, "tags: " + checkbox.gettag() + " id: " + checkbox.getid()); } if (checkbox != null) { checkbox.setonclicklistener(this); } checkboxparent.addview(checkbox, params); } @override public void onclick(view v) { // view checked? boolean checked = ((checkbox) v).ischecked(); // check checkbox clicked switch (v.getid()) { case 31: if (checked) { log.d(logcat, "here:" + v.gettag().tostring()); } else break; case 30: if (checked) { log.d(logcat, "here:" + v.gettag().tostring()); } else break; } } } /** * viewholder class */ private class candidateviewholder extends recyclerview.viewholder { private appcompattextview candidate_name; private appcompattextview candidate_details; private appcompattextview candidate_party; private appcompattextview candidate_position; // private appcompatimageview candidate_image; candidateviewholder(view view) { super(view); candidate_name = (appcompattextview) view.findviewbyid(r.id.candidate_name); // candidate_image = (appcompatimageview) view.findviewbyid(r.id.candidate_image); candidate_position = (appcompattextview) view.findviewbyid(r.id.candidate_position); candidate_party = (appcompattextview) view.findviewbyid(r.id.candidate_party); candidate_details = (appcompattextview) view.findviewbyid(r.id.candidate_details); } void binddata(candidate candidate)//candidate { candidate_name.settext(candidate.getcandidatename()); candidate_position.settext(candidate.getposition()); candidate_details.settext(candidate.getdetails()); candidate_party.settext(candidate.getparty()); // candidate_image.setimagebitmap(candidate.getcandidatephoto()); } } }
my voteactivity class
public class voteactivity extends appcompatactivity implements view.onclicklistener { private static final string logcat = voteactivity.class.getsimplename(); private appcompatactivity activity = voteactivity.this; private recyclerview vote_recycler_view; private linearlayout checkboxparent; private checkbox vote_candidate_checkbox; private appcompatbutton appcompatbuttonvote; private recycleradapter recycleradapter; recyclerview.layoutmanager layoutmanager; private ballot ballot; private list <contestedoffice> ballotlist = new arraylist<>(); private list<candidate> candidatelist = new arraylist<>(); private databasehelper databasehelper; @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_vote); getsupportactionbar().settitle(""); initviews(); initobjects(); initlisteners(); } private void initlisteners() { appcompatbuttonvote.setonclicklistener(this); } private void initobjects() { recycleradapter = new recycleradapter(ballotlist); databasehelper = new databasehelper(this); layoutmanager = new linearlayoutmanager(activity); vote_recycler_view.setlayoutmanager(layoutmanager); vote_recycler_view.setitemanimator(new defaultitemanimator()); vote_recycler_view.sethasfixedsize(true); vote_recycler_view.setadapter(recycleradapter); getcandidates(); } private void getcandidates() { new asynctask<void, void, void>() { @override protected void doinbackground(void... params) { ballotlist.clear(); candidatelist.clear(); candidatelist.addall(databasehelper.getallcandidates()); createballot(); return null; } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); recycleradapter.notifydatasetchanged(); } }.execute(); } private void initviews() { checkboxparent = (linearlayout) findviewbyid(r.id.checkbox_layout_parent); vote_recycler_view = (recyclerview) findviewbyid(r.id.vote_recycler_view); //vote_candidate_checkbox = (checkbox) findviewbyid(r.id.vote_candidate_checkbox); appcompatbuttonvote = (appcompatbutton) findviewbyid(r.id.appcompatbuttonvote); } public list<contestedoffice> createballot() { int candidatelistsize = candidatelist.size(); string position; arraylist<string> positionarray = new arraylist<>(); contestedoffice office; //looping through candidates list , add positions being contested // array list. (int = 0; i< candidatelistsize; i++) { position = candidatelist.get(i).getposition(); positionarray.add(position); } //create set remove duplicate positions set<string> noduplicates = new hashset<>(); noduplicates.addall(positionarray); positionarray.clear(); positionarray.addall(noduplicates); log.d(logcat, "contested offices , candidates: " + positionarray.size() + " "+ candidatelistsize); //create contested office according size of position array //and make sure names added. int noofcontestedoffice = positionarray.size(); for(int = 0; i<noofcontestedoffice; i++) { office = new contestedoffice(positionarray.get(i)); log.d(logcat, "new contested office created:" + office.getname()); candidate c = new candidate(); for(int j = 0; j < candidatelistsize; j++) { //all seats/position being contested string candidateposition = candidatelist.get(j).getposition(); string contestedofficename = office.getname(); if(candidateposition.equals(contestedofficename)) { c = candidatelist.get(j); //add candidate contested office office.add(c); } log.d(logcat, "added candidate: "+ candidatelist.get(j) +" to: "+ office.getname()); } //add offices ballot , ballot list ballot = new ballot(); ballot.add(office); //ideally ballot ballotlist ballotlist.add(office); log.d(logcat, "office added ballotlist: " + ballotlist.size()); } return ballotlist; } }
my voteactivity xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:background="@color/colorbackground" android:orientation="vertical" tools:context=".activities.voteactivity"> <android.support.v7.widget.linearlayoutcompat android:layout_width="match_parent" android:layout_height="100dp" android:background="@color/colorprimary" android:gravity="center" android:orientation="vertical"> <android.support.v7.widget.appcompattextview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dashboard_vote_message" android:textalignment="center" android:textsize="20sp" /> <android.support.v7.widget.appcompattextview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:textalignment="center" android:id="@+id/vote_message_placeholder" android:text="@string/vote_message" /> </android.support.v7.widget.linearlayoutcompat> <include layout="@layout/fragment_ballot_layout"/> <include layout="@layout/footer_vote_button" /> </linearlayout>
recyclerview xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> <linearlayout android:id="@+id/recycler_layout_parent" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:layout_margintop="5dp"> <android.support.v7.widget.recyclerview android:layout_weight="1" android:id="@+id/vote_recycler_view" android:layout_width="match_parent" android:layout_height="0dp"/> </linearlayout> </linearlayout>
cardview xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.cardview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="15dp" xmlns:tools="http://schemas.android.com/tools" card_view:cardcornerradius="4dp"> <linearlayout android:layout_width="match_parent" android:layout_height="?android:attr/listpreferreditemheight" android:orientation="horizontal"> <android.support.v7.widget.appcompatimageview android:id="@+id/vote_candidate_image" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_vertical" android:fontfamily="sans-serif-medium" android:gravity="center" android:src = "@drawable/btn_ballot" android:textcolor="@android:color/white" android:textsize="16sp" tools:text="8.9" android:contentdescription="@string/candidate_photo" /> <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginleft="16dp" android:layout_marginstart="16dp" android:layout_weight="1" android:orientation="vertical"> <android.support.v7.widget.appcompattextview android:id="@+id/vote_candidate_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:fontfamily="sans-serif-medium" android:maxlines="1" android:textallcaps="true" android:textcolor="@color/textcolorcandidatename" android:textsize="12sp" tools:text="candidate name" /> <android.support.v7.widget.appcompattextview android:id="@+id/vote_candidate_party" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="@color/textcolorpartydetails" android:textsize="12sp" android:ellipsize="end" tools:text="party name" /> <android.support.v7.widget.appcompattextview android:id="@+id/vote_candidate_details" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxlines="2" android:textcolor="@color/textcolordetails" android:textsize="16sp" tools:text="long placeholder candidate details. 2 lines max" /> </linearlayout> <linearlayout android:id="@+id/checkbox_layout_parent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginleft="16dp" android:layout_marginstart="16dp" android:orientation="vertical"> <!--dynamic checkboxes here--> </linearlayout> </linearlayout> </android.support.v7.widget.cardview>
i think creating object of candidate every time in loop make below chnages solve problem
candidate c = new candidate(); ballot = new ballot(); for(int = 0; i<noofcontestedoffice; i++) { office = new contestedoffice(positionarray.get(i)); log.d(logcat, "new contested office created:" + office.getname()); for(int j = 0; j < candidatelistsize; j++) { //all seats/position being contested string candidateposition = candidatelist.get(j).getposition(); string contestedofficename = office.getname(); if(candidateposition.equals(contestedofficename)) { c = candidatelist.get(j); //add candidate contested office office.add(c); } log.d(logcat, "added candidate: "+ candidatelist.get(j) +" to: "+ office.getname()); } //add offices ballot , ballot list ballot.add(office); //ideally ballot ballotlist ballotlist.add(office); log.d(logcat, "office added ballotlist: " + ballotlist.size()); }
just check once , done problem.
i have update xml below
<android.support.v7.widget.recyclerview android:layout_weight="1" android:id="@+id/vote_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> </linearlayout>
check out height of recyclerview.
Comments
Post a Comment