1

Topic: ListView + ViewHolder

I try to implement an output of history of a chat. For this purpose in application I use ListView and ViewHolder. The example found here http://androiddocs.ru/pattern-viewholde … listview/, yes actually it is a lot of them and all are similar. Only here at me it is impossible to deduce history of messages correctly. The source and a message sink is confused. At in an array the dataful source and the receiver are set truly.
So, here my code of the adapter.

public class MessageAdapter extends BaseAdapter {
private Activity activity;
private List <ChatBubble> messages;
LayoutInflater lInflater;
public MessageAdapter (Activity context, int resource, List <ChatBubble> objects) {
//super (context, resource, objects);
this.activity = context;
this.messages = objects;
}
@Override
public int getCount () {
return messages.size ();
}
@Override
public ChatBubble getItem (int position) {
return messages.get (position);
}
@Override
public long getItemId (int position) {
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
Log.d (LOGTAG, "getView" + position + "" + convertView);
ViewHolder holder;
int layoutResource = 0;//determined by view type
ChatBubble chatBubble = getItem (position);
if (convertView == null) {
lInflater = (LayoutInflater) activity.getSystemService (Activity. LAYOUT_INFLATER_SERVICE);
if (chatBubble.myMessage ()) {
layoutResource = R.layout.right_chat_bubble;
} else {
layoutResource = R.layout.left_chat_bubble;
}
convertView = lInflater.inflate (layoutResource, parent, false);
holder = new ViewHolder ();
holder.tvItem = (TextView) convertView.findViewById (R.id.txt_msg);
convertView.setTag (holder);
} else {
holder = (ViewHolder) convertView.getTag ();
}
holder.tvItem.setText (chatBubble.getContent ());
return convertView;
}
static class ViewHolder {
TextView tvItem;
}
}

Probably ViewHolder fulfills somehow incorrectly, or it at all does not approach for this task, I ask prompt.

2

Re: ListView + ViewHolder

nikomp wrote:

the source and a message sink

Is confused
Because View in ListView . There is any amount and on a measure , same  are filled with the actual data. And now look at the code and think that happens here

if (convertView == null) {
lInflater = (LayoutInflater) activity.getSystemService (Activity. LAYOUT_INFLATER_SERVICE);
if (chatBubble.myMessage ()) {
layoutResource = R.layout.right_chat_bubble;
} else {
layoutResource = R.layout.left_chat_bubble;
}

Depending on what messages go the first you create a certain amount  2 types (left, right) and all - further they  and new  do not form. The correct decision is or to have 2 official types  as for example is described here http://android.amberfog.com/?p=296
Or to have general-purpose  with the message at the left and on the right and depending on chatBubble.myMessage () to hide one and to show another. Well and the text in correct TextView to write down

3

Re: ListView + ViewHolder

chpasha, thanks for the answer!
Decided to make one general-purpose I twist. Changed the adapter code

public class MessageAdapter extends BaseAdapter {
private Activity activity;
private List <ChatBubble> messages;
LayoutInflater lInflater;
public MessageAdapter (Activity context, List <ChatBubble> objects) {
//super (context, resource, objects);
this.activity = context;
this.messages = objects;
lInflater = (LayoutInflater) activity.getSystemService (Activity. LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount () {
return messages.size ();
}
@Override
public ChatBubble getItem (int position) {
return messages.get (position);
}
@Override
public long getItemId (int position) {
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int layoutResource = 0;
ChatBubble chatBubble = getItem (position);
Log.d (LOGTAG, Integer.toString (position) + "_" +chatBubble.getContent () + "" + Boolean
.toString
(chatBubble.myMessage
()));
if (convertView == null) {
/*if (chatBubble.myMessage ()) {
layoutResource = R.layout.right_chat_bubble;
} else {
layoutResource = R.layout.left_right_chat_bubble;
}*/
convertView = lInflater.inflate (R.layout.left_right_chat_bubble, parent, false);
holder = new ViewHolder ();
holder.htvR = (TextView) convertView.findViewById (R.id.tvR);
holder.htvL = (TextView) convertView.findViewById (R.id.tvL);
holder.llL = (LinearLayout) convertView.findViewById (R.id.llL);
holder.llR = (LinearLayout) convertView.findViewById (R.id.llR);
if (chatBubble.myMessage ()) {
holder.llL.setVisibility (View. INVISIBLE);
} else {
holder.llR.setVisibility (View. INVISIBLE);
}
convertView.setTag (holder);
} else {
holder = (ViewHolder) convertView.getTag ();
}
if (chatBubble.myMessage ()) {
holder.htvR.setText (chatBubble.getContent ());
} else {
holder.htvL.setText (chatBubble.getContent ());
}
return convertView;
}
static class ViewHolder {
TextView htvR;
TextView htvL;
LinearLayout llR;
LinearLayout llL;
}
}

Now the sender and the receiver are truly arranged, and the order of messages is broken

4

Re: ListView + ViewHolder

Visibility all the same not correctly you do. Here it here

if (chatBubble.myMessage ()) {
holder.llL.setVisibility (View. INVISIBLE);
} else {
holder.llR.setVisibility (View. INVISIBLE);
}

Should be in the same place where the message text is installed. Once again comprehend: forms any 5-6  and they are used always and for all occasions. I.e. for each message ALL changeable values (including visibility) should be installed each time when it is required

5

Re: ListView + ViewHolder

I.e. when  , convertView! = null, i.e. you if (convertView == null) simply do not get to the unit

6

Re: ListView + ViewHolder

chpasha;
All turned out! Thanks, for detailed and an efficient answer!