Android Listview repeat the items starting a specific item - android

At beginning, the list shows three items and when I scroll down, it creates the fourth and fifth item, but the sixth and next ones are not being created. These views mix the information with the first five items and they are repeated until the application crash with a ClassCastException.
The reason is simple, each item has a different layout and type, and I have a different ViewHolder for each one. So as the views are not being created, the ViewHolders are the same as the first five items, and when the list reach one that has a different ViewHolder it crash (it's luck that it happens at the twelfth item). I need to discover why the items are being mixing with the first ones.
This is the code of the adapter, I think it's enough:
public class PostsListAdapter extends BaseAdapter {
private FacebookPost[] posts;
private LayoutInflater mInflater;
public PostsListAdapter (Context ctx, FacebookPost[] user_posts) {
mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
posts = user_posts;
}
#Override
public int getCount() {
return posts.length;
}
#Override
public Object getItem(int position) {
return posts[position];
}
#Override
public long getItemId(int position) {
return position;
}
private abstract static class ViewHolder {
TextView fromName;
TextView arrow;
TextView toName;
TextView message;
TextView attribution;
}
private static class VideoViewHolder extends ViewHolder {
TextView name;
TextView caption;
TextView description;
ImageView icon;
}
private static class PhotoViewHolder extends ViewHolder {
}
private static class LinkViewHolder extends ViewHolder {
}
private static class StatusViewHolder extends ViewHolder {
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Log.d("POSITION",""+position);
if(convertView == null) {
switch(posts[position].getType()) {
case FacebookPost.VIDEO:
Log.d(""+position,"VIDEO");
convertView = mInflater.inflate(R.layout.post_list_item_video, parent, false);
holder = new VideoViewHolder();
holder.fromName = (TextView)convertView.findViewById(R.id.post_list_item_video_from_name);
holder.arrow = (TextView)convertView.findViewById(R.id.post_list_item_video_arrow);
holder.toName = (TextView)convertView.findViewById(R.id.post_list_item_video_to_name);
holder.message = (TextView)convertView.findViewById(R.id.post_list_item_video_message);
((VideoViewHolder)holder).name = (TextView)convertView.findViewById(R.id.post_list_item_video_name);
((VideoViewHolder)holder).caption = (TextView)convertView.findViewById(R.id.post_list_item_video_caption);
((VideoViewHolder)holder).description = (TextView)convertView.findViewById(R.id.post_list_item_video_description);
break;
case FacebookPost.LINK:
Log.d(""+position,"LINK");
convertView = mInflater.inflate(R.layout.post_list_item_link, parent, false);
holder = new LinkViewHolder();
holder.fromName = (TextView)convertView.findViewById(R.id.post_list_item_link_from_name);
holder.arrow = (TextView)convertView.findViewById(R.id.post_list_item_link_arrow);
holder.toName = (TextView)convertView.findViewById(R.id.post_list_item_link_to_name);
holder.message = (TextView)convertView.findViewById(R.id.post_list_item_link_message);
break;
case FacebookPost.STATUS:
Log.d(""+position,"STATUS");
convertView = mInflater.inflate(R.layout.post_list_item_status, parent, false);
holder = new StatusViewHolder();
holder.fromName = (TextView)convertView.findViewById(R.id.post_list_item_status_from_name);
holder.arrow = (TextView)convertView.findViewById(R.id.post_list_item_status_arrow);
holder.toName = (TextView)convertView.findViewById(R.id.post_list_item_status_to_name);
holder.message = (TextView)convertView.findViewById(R.id.post_list_item_status_message);
break;
case FacebookPost.PHOTO:
Log.d(""+position,"PHOTO");
convertView = mInflater.inflate(R.layout.post_list_item_photo, parent, false);
holder = new PhotoViewHolder();
holder.fromName = (TextView)convertView.findViewById(R.id.post_list_item_photo_from_name);
holder.arrow = (TextView)convertView.findViewById(R.id.post_list_item_photo_arrow);
holder.toName = (TextView)convertView.findViewById(R.id.post_list_item_photo_to_name);
holder.message = (TextView)convertView.findViewById(R.id.post_list_item_photo_message);
break;
default:
holder=null;
break;
}
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
Spanned text = Html.fromHtml(posts[position].getFrom().getName());
holder.fromName.setText(text);
if(posts[position].getTo() != null)
text = Html.fromHtml(posts[position].getTo()[0].getName());
else
text=null;
if(text==null) {
holder.arrow.setVisibility(View.GONE);
holder.toName.setVisibility(View.GONE);
} else
holder.toName.setText(text);
text = Html.fromHtml(posts[position].getMessage());
holder.message.setText(text);
switch(posts[position].getType()) {
case FacebookPost.VIDEO:
text = Html.fromHtml(((FacebookVideoPost)posts[position]).getCaption());
Log.d("CAST: "+position,holder.getClass().getName());
((VideoViewHolder)holder).caption.setText(text);
text = Html.fromHtml(((FacebookVideoPost)posts[position]).getName());
((VideoViewHolder)holder).name.setText(text);
text = Html.fromHtml(((FacebookVideoPost)posts[position]).getDescription());
((VideoViewHolder)holder).description.setText(text);
break;
case FacebookPost.LINK:
break;
case FacebookPost.STATUS:
Log.d("CAST: "+position,holder.getClass().getName());
break;
case FacebookPost.PHOTO:
break;
}
return convertView;
}
}

I guess you are missing getViewTypeCount

Related

ListView with section headers repeats items when scrolling

I have finally had some luck getting section headers to appear by creating a switch block in my getView(). In doing so I have created a problem with my adapter because now my ListView repeats the top items over and over again. I have found other people with a similar problems, but they were solved by adding convertView.setTag(holder); I already have this, so I believe my problem is something with the way I have set up my switch block. Maybe a syntax problem that is causing things not to line up correctly.
Any help would be appreciated. Here is my adapter:
public class PlayerAdapter extends BaseAdapter {
private Context mContext;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<Player> mPlayers = new ArrayList<>();
private TreeSet<Integer> sectionHeader = new TreeSet<>();
public PlayerAdapter(Context context, ArrayList<Player> players) {
mContext = context;
mPlayers = players;
}
public void addItem(final Player player) {
mPlayers.add(player);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final Player player) {
mPlayers.add(player);
sectionHeader.add(mPlayers.size() -1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return mPlayers.size();
}
#Override
public Object getItem(int position) {
return mPlayers.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public boolean isEnabled(int position) {
int rowType = getItemViewType(position);
if(rowType == TYPE_SEPARATOR) {
return false;
}
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
int rowType = getItemViewType(position);
if (convertView == null) {
Player player = mPlayers.get(position);
switch (rowType){
case TYPE_ITEM:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_list_item_layout, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.playerNameTextView);
holder.playerValueTextView = (TextView) convertView.findViewById(R.id.playerValueTextView);
holder.remainingCapTextView = (TextView) convertView.findViewById(R.id.remainingCapTextView);
holder.username = (TextView) convertView.findViewById(R.id.opponentUsername);
holder.status = (TextView) convertView.findViewById(R.id.statusTextView);
holder.vsTeamAbbrev = (TextView) convertView.findViewById(R.id.vsTeam);
holder.resultsTextView = (TextView) convertView.findViewById(R.id.resultsTextView);
DecimalFormat formatter = new DecimalFormat("$#,###");
holder.playerNameTextView.setText(player.getName());
holder.playerValueTextView.setText(formatter.format(Double.parseDouble(player.getValue())));
holder.remainingCapTextView.setText(formatter.format(Double.parseDouble(player.getCap())));
holder.username.setText(player.getUsername());
holder.status.setText(player.getStatus());
holder.vsTeamAbbrev.setText(player.getVsTeamAbbrev());
holder.resultsTextView.setText(player.getResultsTextView());
if (player.isMatchMade()) {
holder.status.setVisibility(View.VISIBLE);
}
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_section_header, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.lastPlayer);
holder.lastPlayerDate = (TextView) convertView.findViewById(R.id.lastPlayerDate);
holder.playerNameTextView.setText(player.getName());
holder.lastPlayerDate.setText(player.getLastPlayerDate());
convertView.setTag(holder);
break;
}
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private static class ViewHolder {
//item views
TextView playerValueTextView;
TextView playerNameTextView;
TextView remainingCapTextView;
TextView username;
TextView status;
TextView vsTeamAbbrev;
TextView resultsTextView;
//section header views
TextView lastPlayer;
TextView lastPlayerDate;
}
}
This turned out to be a pretty simple mistake. I had to move the code that populates my views down below the else clause. This way if convert view is not null, it can still gettag() and populate the data.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
Player player = mPlayers.get(position);
int rowType = getItemViewType(position);
if (convertView == null) {
switch (rowType){
case TYPE_ITEM:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_list_item_layout, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.playerNameTextView);
holder.playerValueTextView = (TextView) convertView.findViewById(R.id.playerValueTextView);
holder.remainingCapTextView = (TextView) convertView.findViewById(R.id.remainingCapTextView);
holder.username = (TextView) convertView.findViewById(R.id.opponentUsername);
holder.status = (TextView) convertView.findViewById(R.id.statusTextView);
holder.vsTeamAbbrev = (TextView) convertView.findViewById(R.id.vsTeam);
holder.resultsTextView = (TextView) convertView.findViewById(R.id.resultsTextView);
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_section_header, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.lastPlayer);
holder.lastPlayerDate = (TextView) convertView.findViewById(R.id.lastPlayerDate);
convertView.setTag(holder);
break;
}
//convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (rowType){
case TYPE_ITEM:
DecimalFormat formatter = new DecimalFormat("$#,###");
holder.playerNameTextView.setText(player.getName());
holder.playerValueTextView.setText(formatter.format(Double.parseDouble(player.getValue())));
holder.remainingCapTextView.setText(formatter.format(Double.parseDouble(player.getCap())));
holder.username.setText(player.getUsername());
holder.status.setText(player.getStatus());
holder.vsTeamAbbrev.setText(player.getVsTeamAbbrev());
holder.resultsTextView.setText(player.getResultsTextView());
if (player.isMatchMade()) {
holder.status.setVisibility(View.VISIBLE);
}
break;
case TYPE_SEPARATOR:
holder.playerNameTextView.setText(player.getName());
holder.lastPlayerDate.setText(player.getLastPlayerDate());
break;
}
return convertView;
}
private static class ViewHolder {
//item views
TextView playerValueTextView;
TextView playerNameTextView;
TextView remainingCapTextView;
TextView username;
TextView status;
TextView vsTeamAbbrev;
TextView resultsTextView;
//section header views
TextView lastPlayer;
TextView lastPlayerDate;
}
}

Android Listview ArrayAdapter with two layouts

I am quite new to Android. In the following class the data is retrieved from the database and displayed in a ListView which has two different layouts.
Though it works as expected, the problem is that scrolling is not smooth because the textviews are assigned again and again. I couldn't figure out how to have them assigned only once. Please somebody help me out with this.
Thanks in advance. My apology for the code, I know it looks bad.
public class FragmentVerses extends ListFragment {
Typeface font;
ViewHolder viewHolder = new ViewHolder();
ViewHolderHeader viewHolderHeader = new ViewHolderHeader();
DatabaseHelper db;
public List<VersesModel> verses;
public List<ChapterModel> chapterName;
ArrayAdapter<VersesModel> adapter;
public FragmentVerses() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.verses_fragment, container, false);
db = new DatabaseHelper(getActivity());
try {
db.createDatabase();
} catch (IOException e) {
Toast.makeText(getActivity(), "Error Creating Database", Toast.LENGTH_LONG)
.show();
}
verses = db.getVerses(" WHERE " + getActivity().getIntent().getStringExtra(MainActivity.CONDITION));
chapterName = db.getChapter();
adapter = new MyListAdapter();
setListAdapter(adapter);
return view;
}
private class MyListAdapter extends ArrayAdapter<VersesModel> {
public MyListAdapter() {
super(getActivity(), R.layout.verses_custom_list, verses);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
VersesModel currentVerse = verses.get(position);
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.verses_custom_list, parent, false);
font = Typeface.createFromAsset(convertView.getContext().getAssets(), "Quran_Taha.ttf");
viewHolder.textView = (TextViewEx) convertView.findViewById(R.id.textView_Verse);
viewHolder.textViewTranslation = (TextView) convertView.findViewById(R.id.textView_VerseTranslation);
viewHolder.nView = (TextView) convertView.findViewById(R.id.textView_verseNumber);
viewHolder.textView.setTypeface(font);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.versesImageView);
convertView.setTag(viewHolder);
} else {
if (currentVerse.getVerseNumber() != 0) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.verses_custom_list, parent, false);
viewHolder.textView = (TextViewEx) convertView.findViewById(R.id.textView_Verse);
viewHolder.nView = (TextView) convertView.findViewById(R.id.textView_verseNumber);
viewHolder.textViewTranslation = (TextView) convertView.findViewById(R.id.textView_VerseTranslation);
viewHolder.textView.setTypeface(font);
viewHolder.textView.setText(currentVerse.getVerseText() + "", true);
viewHolder.textViewTranslation.setText(currentVerse.getVerseTranslation());
viewHolder.nView.setText(currentVerse.getVerseNumber() + "");
convertView.setTag(viewHolder);
} else {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.verses_custom_list_header, parent, false);
ChapterModel chapterModel = chapterName.get(currentVerse.getChapterNumber() - 1);
if (viewHolderHeader.textViewChapter == null) viewHolderHeader.textViewBismillah = (TextView) convertView.findViewById(R.id.textView_Verse_Bismillah);
viewHolderHeader.textViewChapter = (TextView) convertView.findViewById(R.id.textView_Verse_ChapterName);
viewHolderHeader.textViewChapter.setText("سورة " + chapterModel.getChapterText());
viewHolderHeader.textViewBismillah.setTypeface(font);
viewHolderHeader.textViewChapter.setTypeface(font);
} else {
viewHolderHeader = (ViewHolderHeader) convertView.getTag();
}
convertView.setTag(viewHolderHeader);
}
}
return convertView;
}
}
I know this post is old, but just for future reference...
In this case you should use the ViewHolder pattern.
If you want to use 2 layouts just create two ViewHolder's and switch between then in the getView method.
Pretty similar to the accepted answer but with better performance.
Declare the view types.
private final int VIEW_TYPE_EXAMPLE = 0;
private final int VIEW_TYPE_EXAMPLE_TWO = 1;
Return as many types as you declared above.
#Override
public int getViewTypeCount() {
return 2;
}
Switch when returning the viewType when the item is at position X. in this case I only change the type when the item is the first on the list.
#Override
public int getItemViewType(int position) {
return (position == 0) ? VIEW_TYPE_EXAMPLE : VIEW_TYPE_EXAMPLE_TWO;
}
Create the view holders matching your layouts. They will hold your data.
class SecondViewHolder {
TextView mDate;
TextView mDescription;
TextView mObservations;
public SecondViewHolder(View view) {
mDate = (TextView) view.findViewById(R.id.txt_date);
mDescription = (TextView) view.findViewById(R.id.txt_description);
mObservations = (TextView) view.findViewById(R.id.txt_observations);
}
}
class FirstViewHolder {
ImageView mPhoto;
TextView mName;
TextView mAge;
public FirstViewHolder(View view) {
mPatientPhoto = (ImageView)view.findViewById(R.id.img_photo);
mPatientName = (TextView)view.findViewById(R.id.txt_name);
mPatientAge = (TextView)view.findViewById(R.id.txt_age);
}
}
Switch between then in the getView method.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int viewType = getItemViewType(position);
switch (viewType) {
case VIEW_TYPE_EXAMPLE: {
FirstViewHolder firstViewHolder = null;
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_example, parent, false);
firstViewHolder = new FirstViewHolder(convertView);
convertView.setTag(firstViewHolder);
}
else firstViewHolder = (FirstViewHolder)convertView.getTag();
firstViewHolder.mName.setText("Your name");
firstViewHolder.mAge.setText("20 years old");
break;
}
case VIEW_TYPE_EXAMPLE_TWO: {
SecondViewHolder holder = null;
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_example_two, parent, false);
holder = new SecondViewHolder(convertView);
convertView.setTag(holder);
}
else holder = (SecondViewHolder)convertView.getTag();
holder.mDate.setText("01/01/2016");
holder.mDescription.setText("Description");
holder.mObservations.setText("Obs");
break;
}
}
return convertView;
}
But I can not ignore the fact that in this specific question, you should use the CursorAdapter because you are querying from a database.
You also should not do the access to the database directly.
Should create a Loader instead (does the async task but not tied to the activity).
And if you want to follow best practices and save some trouble later on, create the ContentProvider to manage your SQLite database.
But that's just too much code for me to put in this answer :/
Hope this helps someone.
Android's adapter provide a way to use multiple layouts in a single adapter.
First, tell your adapter how many layouts you need:
public int getViewTypeCount()
{
return 2;
}
Then, gives some logic to tell which layout should be used for the current item:
public int getItemViewType(int position)
{
if (verses.get(position).getVerseNumber() != 0)
{
return 0;
}
return 1;
}
Finally, in your build the appropriate view:
public View getView(int position, View convertView, ViewGroup parent)
{
if (this.getItemViewType(position) == 0)
{
// TODO Build the appropriate view
return view;
}
// TODO Build the appropriate other view
return view;
}
There are two many things to changes have a look at code below it will give you idea. You dont have to inflate layout everytime and no need to call findViewById everytime as well look at sample code below
ViewHolder holder;
if ((convertView == null)) {
convertView = layoutInflater
.inflate(R.layout.list_item,
viewGroup, false);
holder = new ViewHolder();
holder.itemImage = (ImageView) convertView
.findViewById(R.id.logo);
holder.itemName = ((TextView) convertView
.findViewById(R.id.title_product_search));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//assign text and images to controls after this
holder.itemName.setText("text");
imageLoader.displayImage(item.imageUrl, holder.itemImage,
options);
Here is what i did.
private class MyListAdapter extends ArrayAdapter<VersesModel> {
public MyListAdapter() {
super(getActivity(), R.layout.verses_custom_list, verses);
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
if (verses.get(position).getVerseNumber() != 0) {
return 0;
}
return 1;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
VersesModel currentVerse = verses.get(position);
if (convertView == null) {
viewHolder = new ViewHolder();
switch (getItemViewType(position)) {
case 0:
convertView = getActivity().getLayoutInflater().inflate(
R.layout.verses_custom_list, parent, false);
viewHolder.textView = (TextViewEx) convertView.findViewById(R.id.textView_Verse);
viewHolder.nView = (TextView) convertView.findViewById(R.id.textView_verseNumber);
viewHolder.textViewTranslation = (TextView) convertView.findViewById(R.id.textView_VerseTranslation);
font = Typeface.createFromAsset(convertView.getContext().getAssets(), "Quran_Taha.ttf");
viewHolder.textView.setTypeface(font);
Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
break;
case 1:
convertView = getActivity().getLayoutInflater().inflate(
R.layout.verses_custom_list_header, parent, false);
chapterModel = chapterName.get(currentVerse.getChapterNumber() - 1);
viewHolder.textView = (TextViewEx) convertView.findViewById(R.id.textView_Verse_Bismillah);
viewHolder.nView = (TextView) convertView.findViewById(R.id.textView_Verse_ChapterName);
font = Typeface.createFromAsset(convertView.getContext().getAssets(), "Quran_Taha.ttf");
viewHolder.textView.setTypeface(font);
viewHolder.nView.setTypeface(font);
break;
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
switch (getItemViewType(position)) {
case 0:
viewHolder.textView.setText(currentVerse.getVerseText() + "", true);
viewHolder.textViewTranslation.setText(currentVerse.getVerseTranslation());
viewHolder.nView.setText(currentVerse.getVerseNumber() + "");
break;
case 1:
viewHolder.nView.setText("سورة " + chapterModel.getChapterText());
break;
}
return convertView;
}
}
The toast displays 3 times when the list loads and the scrolling is slower at the beginning.

Android, dynamic Listview layout changing on scroll

I have a conversation mode in my application where I wish to load one layout for one user and another layout for the other. It need not always be alternating hence I cannot use a simple "%2" to achieve it.
Based on a flag I am assigning a dynamic layout, which works. My problem is that as I scroll the layouts get distorted as in, conversation of user_1 will get layout_2 or conversation of user_2 will get layout_1, absolutely random.
I did something similar to an answer I saw here:
https://stackoverflow.com/a/16774696/4810718
There were a few posts about randomized data. That is not my issue, the order of the list items does not change however the layout get's randomly applied. I read that the items in view + 1 are kept in temporary memory, regarding this another thing I noticed was: as I keep adding items such that the scrollbar comes into picture when I add a second item outside the visibility it tends to get the layout of the topmost item (first item) visible. Scrolling would later give me seemingly randomized results.
public class ConversationAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private ArrayList<ConversationContent> objects;
ImageView user;
static int ind = 0;
private class ViewHolder
{
TextView textView1;
TextView textView2;
TextView textView3;
}
public ConversationAdapter(Context context, ArrayList<ConversationContent> objects)
{
inflater = LayoutInflater.from(context);
this.objects = objects;
}
public int getCount()
{
return objects.size();
}
public ConversationContent getItem(int position)
{
return objects.get(position);
}
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView == null)
{
holder = new ViewHolder();
if (Main_Page3.convFlag == 1)
{
convertView = inflater.inflate(R.layout.conversation_item_1, null);
}
else
{
convertView = inflater.inflate(R.layout.conversation_item_2, null);
}
holder.textView1 = (TextView) convertView.findViewById(R.id.trans);
holder.textView1.setTypeface(Main_Activity.fontC);
holder.textView2 = (TextView) convertView.findViewById(R.id.lang);
holder.textView2.setTypeface(Main_Activity.fontC);
holder.textView3 = (TextView) convertView.findViewById(R.id.user);
holder.textView3.setTypeface(Main_Activity.fontC);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.textView1.setText(objects.get(position).getTranslatedText());
holder.textView2.setText(objects.get(position).getTranslationString());
SpannableString originalTextString = new SpannableString("\n" + objects.get(position).getOriginalText());
originalTextString.setSpan(new RelativeSizeSpan(0.5f), 0, originalTextString.length(), 0);
holder.textView1.append(originalTextString);
holder.textView3.setText(objects.get(position).getUser());
return convertView;
}
}
So, that's the code I've written. A possible solution I thought of was if I used an array of views and loaded them accordingly, it may work? I'm really not really sure how I should be going about doing this - I'm still pretty new to Android.
I've searched a bit but could not get a helpful solution. Please direct me to a helpful solution you find or, a working answer would be most appreciable. Thank you.
I think the best way to achieve what you want is to put the flag to determine which layout to use on your ConversationContent object, then override getViewTypeCount() and getItemViewType(int position) something like this:
#Override
int getViewTypeCount() {
return 2;
}
#Override
int getItemViewType(int position) {
if (objects.get(position).isReply()) { //isReply can be whatever you want to determine whether to change layout
return 1;
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView == null)
{
holder = new ViewHolder();
if (getItemViewType(position) == 1)
{
convertView = inflater.inflate(R.layout.conversation_item_1, null);
}
else
{
convertView = inflater.inflate(R.layout.conversation_item_2, null);
}
holder.textView1 = (TextView) convertView.findViewById(R.id.trans);
holder.textView1.setTypeface(Main_Activity.fontC);
holder.textView2 = (TextView) convertView.findViewById(R.id.lang);
holder.textView2.setTypeface(Main_Activity.fontC);
holder.textView3 = (TextView) convertView.findViewById(R.id.user);
holder.textView3.setTypeface(Main_Activity.fontC);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.textView1.setText(objects.get(position).getTranslatedText());
holder.textView2.setText(objects.get(position).getTranslationString());
SpannableString originalTextString = new SpannableString("\n" + objects.get(position).getOriginalText());
originalTextString.setSpan(new RelativeSizeSpan(0.5f), 0, originalTextString.length(), 0);
holder.textView1.append(originalTextString);
holder.textView3.setText(objects.get(position).getUser());
return convertView;
}
For listview adapter, if you want to show different layout,
like conversion mode. you would better override the following two methods:
//set your layout type here
public int getItemViewType(int position)
{
return 0;
}
//the layout count in your adapter
public int getViewTypeCount()
{
return 0;
}
Here is an example you can refer to:
public class ChatMessageAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
private List<ChatMessage> mDatas;
public ChatMessageAdapter(Context context, List<ChatMessage> mDatas)
{
mInflater = LayoutInflater.from(context);
this.mDatas = mDatas;
}
#Override
public int getCount()
{
return mDatas.size();
}
#Override
public Object getItem(int position)
{
return mDatas.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public int getItemViewType(int position)
{
ChatMessage chatMessage = mDatas.get(position);
if (chatMessage.getType() == Type.INCOMING)
{
return 0;
}
return 1;
}
#Override
public int getViewTypeCount()
{
return 2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ChatMessage chatMessage = mDatas.get(position);
ViewHolder viewHolder = null;
if (convertView == null)
{
if (getItemViewType(position) == 0)
{
convertView = mInflater.inflate(R.layout.item_from_msg, parent,
false);
viewHolder = new ViewHolder();
viewHolder.mDate = (TextView) convertView
.findViewById(R.id.id_form_msg_date);
viewHolder.mMsg = (TextView) convertView
.findViewById(R.id.id_from_msg_info);
} else
{
convertView = mInflater.inflate(R.layout.item_to_msg, parent,
false);
viewHolder = new ViewHolder();
viewHolder.mDate = (TextView) convertView
.findViewById(R.id.id_to_msg_date);
viewHolder.mMsg = (TextView) convertView
.findViewById(R.id.id_to_msg_info);
}
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
//set data here
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
viewHolder.mDate.setText(df.format(chatMessage.getDate()));
viewHolder.mMsg.setText(chatMessage.getMsg());
return convertView;
}
private final class ViewHolder
{
TextView mDate;
TextView mMsg;
}
}

AQuery - Item position in listview is wrong

I wrote a custom ArrayAdapter to show my listview items.
My items have many types. if the type is text then i show a TextView and if the type is Image or Video i show an ImageView (for video the thumbnail).
This is the code.
public class MessageAdapter extends ArrayAdapter<NMessage> {
public static final int ALL_MESSAGES = 0;
public static final int MEDIA_ONLY = 1;
Context context;
DatabaseActions db;
ArrayList<NMessage> items;
AQuery aq;
Profile myProfile = ProfilesSingleton.getInstance().getCurrentProfile();
LayoutInflater inflater;
int type;
public MessageAdapter(Context context, int resource, ArrayList<NMessage> objects) {
super(context, resource, objects);
this.context = context;
items = objects;
aq = new AQuery(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.type = type;
db = new DatabaseActions();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final NMessage msg = items.get(position);
ViewHolder holder = new ViewHolder();
int type = getItemViewType(position);
if (row == null) {
if (type == 1) {
row = inflater.inflate(R.layout.out_message, parent, false);
} else if (type == 0) {
row = inflater.inflate(R.layout.in_message, parent, false);
}
holder.avatar = (ImageView) row.findViewById(R.id.avatar);
holder.profileName = (TextView) row.findViewById(R.id.profile_name);
holder.text = (TextView) row.findViewById(R.id.msg_body);
holder.image = (ImageView) row.findViewById(R.id.image_message);
holder.video = (ImageView) row.findViewById(R.id.video_message);
holder.time = (TextView) row.findViewById(R.id.msg_date);
holder.location = (ImageView) row.findViewById(R.id.location_message);
holder.audioController = (RelativeLayout) row.findViewById(R.id.audioContainer);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.video.setVisibility(View.GONE);
holder.image.setVisibility(View.GONE);
holder.text.setVisibility(View.GONE);
holder.audioController.setVisibility(View.GONE);
holder.location.setVisibility(View.GONE);
AQuery rowAq = aq.recycle(row);
holder.text.setText(msg.getText());
holder.time.setText(Util.dateParser((Long.parseLong(msg.getTime()))));
if(!msg.isSent()) {
rowAq.id(holder.avatar).image(Util.URL_USERS_PHOTOS+msg.getPhoto_path(), false, true, 50, R.drawable.avatar_profile);
holder.profileName.setText(msg.getName());
} else {
rowAq.id(holder.avatar).image(myProfile.getFullPhotoPath(), false, true, 50, R.drawable.avatar_profile);
holder.profileName.setText("you");
}
switch (msg.getType()){
case NMessage.TEXT:
holder.text.setVisibility(View.VISIBLE);
break;
case NMessage.IMAGE:
rowAq.id(holder.image).image(msg.getText(), false, true, 100, R.drawable.ic_photo).visible();
break;
case NMessage.VIDEO:
String url = msg.getText().replace(msg.getText().substring(msg.getText().lastIndexOf(".")), ".jpg");
rowAq.id(holder.video).image(url, false, true, 100, R.drawable.ic_video).visible();
break;
case NMessage.LOCATION:
holder.location.setVisibility(View.VISIBLE);
break;
case NMessage.AUDIO:
holder.audioController.setVisibility(View.VISIBLE);
break;
}
return row;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
NMessage message = items.get(position);
if (message.isSent()) {
return 1;
} else
return 0;
}
private class ViewHolder {
MediaController controller;
RelativeLayout audioController;
ImageView avatar,
image,
video,
location;
TextView text;
TextView time;
TextView profileName;
}
}
The problem is that some views are not shown in the right place. when i scroll the listview and scroll back they are arranged properly.
This is before i scroll. (when the listview is first populated)
This is after i scroll and scroll back. the imageview is gone and the right type (text) is shown.
I searched everywhere but cannot seem to find the answer even in AQuery's wiki page.
As the this blog post explains, Android does not actually guarantee that getView will receive the correct type of view.

Android listview row changing values

I have a ListView that contains 3 types of elements. Rows and QueueRows that are a bit complicated sets of subcontrols, and HeaderRow containing only textView.
What I do is adding HeaderRow, few QueueRows, headerRow and few Rows.
QueueRow needs to be updated every second. That is why I wrote a code that calls notifyDataSetChanged on adapter. It updates queuerow every second, but there is also a problem - My two headers switch places for half a second.
After every 0,5s they swap. Do you have any ideas how to prevent them?
That is my code:
class BuildingsAdapter extends ArrayAdapter<BaseRow> {
private static Bitmap SOURCE_BITMAP;
private MainActivity mainActivity_;
private Handler handler_;
private Runnable runnable_;
public BuildingsAdapter(MainActivity mainActivity) {
super(mainActivity, R.layout.row);
this.mainActivity_ = mainActivity;
SOURCE_BITMAP = BitmapFactory.decodeResource(
mainActivity_.getResources(), R.drawable.images);
handler_ = new Handler();
runnable_ = new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
if (hasQueueItems())
handler_.postDelayed(runnable_, 1000);
}
};
}
#Override
public void add(BaseRow object) {
if (!hasQueueItems() && object.getClass().equals(QueueRow.class))
handler_.postDelayed(runnable_, 1000);
super.add(object);
}
private boolean hasQueueItems() {
for (int i = 0; i < getCount(); ++i) {
if (getItem(i).getClass().equals(QueueRow.class))
return true;
}
return false;
}
#Override
public int getItemViewType(int position) {
return getItem(position).getType();
}
#Override
public int getViewTypeCount() {
return 3;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View row = convertView;
switch (type) {
case BaseRow.QUEUE_ROW: {
QueueRowViewHolder holder = new QueueRowViewHolder();
//whatever
return convertView;
}
case BaseRow.ROW: {
RowViewHolder holder = new RowViewHolder();
//whatever
return convertView;
}
case BaseRow.HEADER_ROW: {
HeaderViewHolder holder = new HeaderViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.header_row, parent, false);
HeaderRow headerRow = (HeaderRow) getItem(position);
holder.nameTextView_ = (TextView) row
.findViewById(R.id.categoryNameTextView);
holder.nameTextView_.setText(headerRow.getName());
row.setTag(holder);
} else {
holder = (HeaderViewHolder) row.getTag();
}
return row;
}
}
return null;
}
static class QueueRowViewHolder {
ImageView qItemImageView_;
TextView qItemNameTextView_;
ProgressBar qProgressBar_;
TextView qLevelTextView_;
TextView qTimeTextView_;
int position_;
}
static class RowViewHolder {
ImageView itemImageView_;
TextView nameTextView_;
TextView levelTextView_;
TextView woodTextView_;
TextView ironTextView_;
TextView stoneTextView_;
TextView goldTextView_;
Button actionButton_;
TextView timeTextView_;
}
static class HeaderViewHolder {
TextView nameTextView_;
}
Ok! I've found it out - I didn't set the values of HeaderRow ;)

Categories

Resources