Firstly i used it this way after trying viewholder and didn't work so i
wanted every layout for both send and receive to be separated so when flag
true and i'm receiving it should update only current item with the name and
FB profile picture of sender and keep previous rows without updating ,here
is all the code


public class MarkChat extends Activity {

// private ArrayList<Chat> chatList = new ArrayList<Chat>();
private ArrayList<String> messages = new ArrayList<String>();
 static ArrayList<String> Name = new ArrayList<String>();

EditText mSendText;
Context context;
ConversationListAdapter adapter;
private static Handler mHandler = new Handler();
ListView list;
String friendJid;
Message message;
static String Messname;
static String messagfrom;
String friendName;
Bitmap img;
String MYNamen = "Me";
int num=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bubbles);
list = (ListView) findViewById(R.id.chatlist);

// /getting buddy info
Bundle b = getIntent().getExtras();
friendJid = b.getString("username");
friendName = b.getString("BuddyNAME");
Log.d("friendName", "" + friendName);
if (getIntent().hasExtra("byteArray")) {
img = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"), 0, getIntent()
.getByteArrayExtra("byteArray").length);
}

// some where in code
Name.add(friendName);
Name.add(MYNamen);
adapter = new ConversationListAdapter(getApplicationContext(),
 num, messages);

mSendText = (EditText) findViewById(R.id.chat_input_text);

// when I send/receive msg. -----------------START-------------
if (Main.conn != null) {

// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
Main.conn.addPacketListener(new PacketListener() {

public void processPacket(Packet p) {

if (message.getBody() != null) {
adapter.flag = true;
messages.add(message.getBody());
Log.d("AsmackRecieve", "" + adapter.flag);

// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {

list.setAdapter(adapter);
//adapter.notifyDataSetChanged();
}
});
}

}
}, filter);

adapter.flag = false;
}

// Set a listener to send a chat text message
Button send = (Button) this.findViewById(R.id.chat_send_button);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

String text = mSendText.getText().toString();

adapter.flag = false;
Log.i("XMPPClient", "Sending text [" + text + "] ");
message = new Message(friendJid, Message.Type.chat);
message.setBody(text);
Main.conn.sendPacket(message);
messages.add(text);

list.setAdapter(adapter);
//adapter.notifyDataSetChanged();
}
});

// when I send/receive msg. ---------------END---------------

}
}

class ConversationListAdapter extends ArrayAdapter<String> {

Context con;
LayoutInflater mInflater;
ArrayList<String> groups;
ArrayList<String> groupsMessage;
boolean flag = false;
TextView title, titleOdd, body, bodyOdd;

public ConversationListAdapter(Context context, int textViewResourceId,
ArrayList<String> objects) {
super(context, textViewResourceId, objects);
this.con = context;
mInflater = LayoutInflater.from(context);
this.groupsMessage=objects;
 }

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder h;
View v = null;

if (flag) {

if (convertView == null) {
// //for incoming
h = new ViewHolder();
v = mInflater.inflate(R.layout.evenbubble, null);

title = (TextView) v.findViewById(R.id.BuddyTitle);
body = (TextView) v.findViewById(R.id.BuddyMessage);
v.setTag(h);
} else {
v = convertView;
}
Log.d("FLAG", "" + flag);
body.setText(groupsMessage.get(position));

title.setText(MarkChat.Name.get(0));

} else {

// /for outcoming
if (convertView == null) {
h = new ViewHolder();
v = mInflater.inflate(R.layout.oddbubble, null);

titleOdd = (TextView) v.findViewById(R.id.BuddyTitle);
bodyOdd = (TextView) v.findViewById(R.id.BuddyMessage);
v.setTag(h);
} else {

v = convertView;
}
Log.d("FLAG", "" + flag);
bodyOdd.setText(groupsMessage.get(position));

titleOdd.setText(MarkChat.Name.get(1));
}
return v;
}

}


2012/3/29 Justin Anderson <[email protected]>

> Good catch Kostya... I didn't notice the two different layouts until you
> mentioned it.
>
>
> Thanks,
> Justin Anderson
> MagouyaWare Developer
> http://sites.google.com/site/magouyaware
>
>
> 2012/3/29 Kostya Vasilyev <[email protected]>
>
>> Note that there are two layouts being used here:
>>
>> R.layout.evenbubble
>> R.layout.oddbubble
>>
>> Without overriding getViewTypeCount / getItemViewType in the adapter and
>> returning the right item type...
>>
>> .. the list view will mix up those two layouts when recycling, which
>> would appear as a list of "send" bubbles or a list of "receive" bubbles.
>>
>> The view type presumably should be based on "flag", which in turn, as was
>> already noted, presumably should depend on the item's position, and a data
>> structure lookup to determine if it's a "send" bubble or a "receive" bubble.
>>
>> -- K
>>
>> 30 марта 2012 г. 0:49 пользователь Justin Anderson <[email protected]
>> > написал:
>>
>> That its what I was thinking as well...
>>> On Mar 29, 2012 2:16 PM, "Nadeem Hasan" <[email protected]> wrote:
>>>
>>>> Justin,
>>>>
>>>> You are right. In that case, I think the culprit is the "flag". It is
>>>> supposed to be true for incoming and false for outgoing. But, it remains
>>>> static while listview is building its items. I think this flag should be in
>>>> the adapter along with message.
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Android Developers" group.
>>>> To post to this group, send email to
>>>> [email protected]
>>>> To unsubscribe from this group, send email to
>>>> [email protected]
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/android-developers?hl=en
>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Android Developers" group.
>>> To post to this group, send email to [email protected]
>>> To unsubscribe from this group, send email to
>>> [email protected]
>>> For more options, visit this group at
>>> http://groups.google.com/group/android-developers?hl=en
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>>
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to