android - Why correct alphabet is not shown in TextView? -
i'm making android app implementing custom listview of names. want list displayed in lexicographic order , on change of first character in name,the corresponding character should displayed before names starting character begin.but not happening correctly.i can't understand reason. secondly, whatever list generated,on fast scrolling first letters disappear , i'm left sorted list without first alphabet markings. please help.. in advance. snapshots: before scrolling after scrolling multiple times
row_item_speaker.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" android:orientation="vertical"> <textview android:id="@+id/tv_first_alphabet" android:paddingtop="10dp" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="a" android:textstyle="bold" android:textcolor="@color/colorprimary"/> <view android:id="@+id/horizontalline" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorprimary"/> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <com.pkmmte.view.circularimageview android:id="@+id/civ_speaker_photo" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_speakers" app:border="true" app:border_color="#eeeeee" app:border_width="2dp" android:paddingleft="5dp" android:paddingstart="5dp" app:shadow="true" /> <textview android:id="@+id/tv_speaker_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="@dimen/heading_size" android:textcolor="#000" android:textstyle="bold" android:paddingleft="10dp" android:paddingstart="10dp" android:layout_toendof="@id/civ_speaker_photo" android:layout_torightof="@id/civ_speaker_photo" android:layout_aligntop="@id/civ_user_pic" android:paddingtop="8dp"/> <textview android:id="@+id/tv_speaker_position" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@id/civ_speaker_photo" android:layout_toendof="@id/civ_speaker_photo" android:layout_below="@id/tv_speaker_name" android:textsize="@dimen/subheading_size" android:textcolor="#777" android:ellipsize="end" android:singleline="true" android:paddingleft="10dp" android:paddingstart="10dp" /> </relativelayout> </linearlayout>
speakeradapter.java
package com.test.rajat.a10times.adapter; import android.content.context; import android.graphics.color; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; import com.pkmmte.view.circularimageview; import com.squareup.picasso.picasso; import com.test.rajat.a10times.model.speakermodel; import com.test.rajat.a10times.r; import java.util.arraylist; import java.util.collections; import java.util.comparator; /** * created rajat on 1/20/2016. */ public class speakersadapter extends baseadapter { private context context; private arraylist<speakermodel> speakerslist; public speakersadapter(context context, arraylist<speakermodel> speakerslist) { this.context = context; this.speakerslist = speakerslist; sortedlist(); } private arraylist<speakermodel> sortedlist(){ collections.sort(speakerslist, new comparator<speakermodel>() { @override public int compare(speakermodel s1, speakermodel s2) { return s1.getname().comparetoignorecase(s2.getname()); } }); return speakerslist; } @override public int getcount() { return speakerslist.size(); } @override public object getitem(int position) { return speakerslist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if(convertview==null) { layoutinflater inflater=(layoutinflater) context.getsystemservice(context.layout_inflater_service); convertview=inflater.inflate(r.layout.row_item_speaker,null); } textview name=(textview) convertview.findviewbyid(r.id.tv_speaker_name); textview speaker_position=(textview) convertview.findviewbyid(r.id.tv_speaker_position); textview first_alphabet=(textview) convertview.findviewbyid(r.id.tv_first_alphabet); view horizontalline= convertview.findviewbyid(r.id.horizontalline); if(setfirstalphabet(position)){ first_alphabet.settext((speakerslist.get(position).getname().subsequence(0, 1).tostring())); } else{ first_alphabet.setvisibility(view.gone); horizontalline.setbackgroundcolor(color.parsecolor("#d3d3d3")); } circularimageview image=(circularimageview) convertview.findviewbyid(r.id.civ_speaker_photo); picasso.with(context).load(speakerslist.get(position).getpic_url()).placeholder(r.drawable.ic_person).error(r.drawable.ic_person).into(image); name.settext(speakerslist.get(position).getname()); speaker_position.settext(speakerslist.get(position).getposition()); return convertview; } private boolean setfirstalphabet(int position){ string currentfirstalphabet=speakerslist.get(position).getname().subsequence(0,1).tostring(); string nextfirstalphabet=speakerslist.get(position+1).getname().subsequence(0,1).tostring(); if(position<speakerslist.size()-1) { if ((currentfirstalphabet.comparetoignorecase(nextfirstalphabet) == 0)) { log.d("tag3",speakerslist.get(position).getname().subsequence(0,1).tostring()+speakerslist.get(position+1).getname().subsequence(0,1).tostring()); return false; } } return true; } }
Comments
Post a Comment