listview with dynamic data from xmpp - android

I am working on android chat application. I am facing a problem in send and receive function using xmpp. I am able to send message from emulator to xmpp and receive message from xmpp. But i am facing issue in displaying the incoming and outgoing message in a list view. I am confused how to give the condition to set view layout.
if(message from xmpp) {
TextView textLabel = (TextView) row.findViewById(R.id.textb); // if message received dislay in left side textview
textLabel.setText(receiveddata); //receiveddata contains arraylist of incoming message
} else (message from me) {
TextView textLabel = (TextView) row.findViewById(R.id.texts); // if message sent by me dislay in right side textview
textLabel.setText(sentdata); //sentdata contains arraylist of outgoing message
}
Kindly tell me how can i do this.
Thanks

You can create an adapter class having two layout having same field. use if condition for incoming and outgoing messages. and inflate accordingly.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
entry = list.get(position);
if (convertView == null) {
if (getItemViewType(position) == 0) {
convertView = inflator.inflate(
R.layout.messages_even_list_layout, null);
} else {
convertView = inflator.inflate(
R.layout.messages_odd_list_layout, null);
}

Related

java.lang.ClassCastException, trying to have two activites using the same custom adapter

I have two activities, NewContact.java and ViewContact.java, that I want to have using the same custom adapter as the two activites are very similar.
But I am getting the following error and my app crashes:
AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: com.example.chris.tutorialspoint.ViewContact cannot be cast to com.example.chris.tutorialspoint.NewContact
I've read quite a few posts here with subjects AndroidRuntime: FATAL EXCEPTION:, java.lang.ClassCastExceptionbut I don't know how to adjust the answers to suit my needs.
Here's my getView code from my adapter. I know the problem is with the lines:
viewHolder.check.setOnCheckedChangeListener((NewContact) _c);
viewHolder.check.setOnCheckedChangeListener((ViewContact) _c);
If I delete one of the lines then the custom adapter works for the remaining activity but I want to have it working for both activities, NewContact and ViewContact.
#Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
System.out.println("getView number is :" + i + "convertView is : " + convertView);
//we're naming our convertView as view
// View view = convertView;
ViewHolder viewHolder = null;
if (convertView == null) {
//if there is nothing there (if it's null) inflate the view with the layout
LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.phone_inflate_listview, null);
viewHolder = new ViewHolder();
// So, for example, title is cast to the name id, in phone_inflate_listview,
// phone is cast to the id called no etc
viewHolder.title = (TextView) convertView.findViewById(R.id.name);
viewHolder.phone = (TextView) convertView.findViewById(R.id.no);
viewHolder.invite = (Button) convertView.findViewById(R.id.btnInvite);
viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact);
// viewHolder.check.setVisibility(View.GONE);
//remember the state of the checkbox
viewHolder.check.setOnCheckedChangeListener((NewContact) _c);
viewHolder.check.setOnCheckedChangeListener((ViewContact) _c);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// store the holder with the view
final SelectPhoneContact data = (SelectPhoneContact) arraylist.get(i);
//in the listview for contacts, set the name
viewHolder.title.setText(data.getName());
//in the listview for contacts, set the number
viewHolder.phone.setText(data.getPhone());
////*********************
//for every phone number in the MatchingContactsAsArrayList array list...
for (int number = 0; number < MatchingContactsAsArrayList.size(); number++) {
//if a phone number is in our array of matching contacts
if (MatchingContactsAsArrayList.contains(data.getPhone()))
{
//if a matching contact, no need to show the Invite button
viewHolder.invite.setVisibility(View.GONE);
System.out.println("it's a match: phoneNumberofContact is : " + data.getPhone());
//once a matching contact is found, no need to keep looping x number of time, move onto next contact
break;
}
else {
//if not a matching contact, no need to show the check box
viewHolder.check.setVisibility(View.GONE);
}
}
viewHolder.check.setChecked(data.isSelected());
viewHolder.check.setTag(data);
// Return the completed view to render on screen
return convertView;
}
You don't need to cast that Context to the specific Activity types. The setOnCheckedChangeListener() method just needs an OnCheckedChangeListener, and if both classes implement that interface, you need only one call that casts the Context to OnCheckedChangeListener.
viewHolder.check.setOnCheckedChangeListener((OnCheckedChangeListener) _c);
It might be prudent to add an instanceof check in the constructor to make sure the passed Context is indeed an OnCheckedChangeListener, which would have the benefits of failing sooner, and giving you the opportunity to perhaps throw an Exception with a more informative message.

My getView() Position is repeating In Custom List View?

I am using custom ListView .Its working fine. I am using this ListView for differentiate Read & Unread Messages I pick up message read Id which is 0 for unread message & 1 for read message.
My getView() code is following:---
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.custom_home_list, null);
holder=new ViewHolder();
holder.address=(TextView)convertView.findViewById(R.id.person_name);
holder.body=(TextView)convertView.findViewById(R.id.full_msg);
holder.date=(TextView)convertView.findViewById(R.id.msg_time);
convertView.setTag(holder);
}else
{
holder=(ViewHolder)convertView.getTag();
}
int size=mArrList.size();
if ((mArrList != null) || size > 0)
{
if(mArrList.get(position).read.equalsIgnoreCase("1")){
holder.address.setText(mArrList.get(position).address);
holder.body.setText(mArrList.get(position).body);
holder.date.setText(mArrList.get(position).date);
}else{
holder.address.setText(mArrList.get(position).address);
holder.body.setText(mArrList.get(position).body);
holder.body.setTextColor(mArrList.get(position).color);
holder.date.setText(mArrList.get(position).date);
}
}
return convertView;
}
here i use this condition for differentiate :-
if(mArrList.get(position).read.equalsIgnoreCase("1")){
}
But Position Repeat After 5 items so my condition not working.
I have lots of search for this But I am not getting it.
Please Help me .
Thanks In Advance!
Regards Deepanker
In your code you don't explicitely set the color in case of a 'read' state (the first section of your if/else).
It means that if your view is recycled and it was previously used for an 'unread' item, then the color will remain the same.
You need to explicitely say which color you want in both cases, because you cannot know what the initial color is.

android chat ListView getView updating wierdly

i've built a chat application.
inside my chat messages Activity, i have a ListView which shows all text message.
inside each text messages i have a TextView which will by default write "Sending..." and i want it to be updated after the message is sent.
inside each ChatMessage object i have a sent and time property. if sent is true i will show in the TextView the time, and if it's false i will write "Sending" as written above.
when i'm sending a new message, i'm adding a new view to the ListView adapter, and from some reason it shows that message was sent although it didn't... can't really understand why.
this is my ArrayAdapter's getView() and holder class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ChatMessage chatMessage = chatMessagesArrayList.get(position);
ChatMessageHolder chatMessageHolder = null;
if (row == null)
{
if (chatMessage.getSenderId() == app.getFacebookCurrentUser().getId())
row = LayoutInflater.from(context).inflate(layoutResourceId, null);
else
row = LayoutInflater.from(context).inflate(R.layout.chat_green, null);
chatMessageHolder = new ChatMessageHolder();
chatMessageHolder.message = (TextView) row.findViewById(R.activity_chat.message);
chatMessageHolder.sentTime = (TextView) row.findViewById(R.activity_chat.sent_time);
chatMessageHolder.isSent = chatMessage.isSent();
row.setTag(chatMessageHolder);
}
else
{
chatMessageHolder = (ChatMessageHolder) row.getTag();
}
chatMessageHolder.message.setText(chatMessage.getMessage());
if (chatMessageHolder.isSent)
chatMessageHolder.sentTime.setText(app.getDateTime(chatMessage.getTime()));
return row;
}
private static class ChatMessageHolder
{
TextView message, sentTime;
boolean isSent = false;
}
i can't really understand why doesn't it write the time or "Sending..." according to the isSent boolean flag...
It's because the ListView recycles previous views (that's what the convertView variable is). Likely it's recycling another view that was already set to "Sent", and you're never handling the alternate case where chatMessagerHolder.isSent is false. You should avoid setting chatMessageHolder.isSent = chatMessage.isSent(); until after that first if-else block. That first if-else should just be to initialize the view or recycle the object. Also, when you check if(chatMessageHolder.isSent) you should also handle what the TextView should say if that is false (i.e. in a followup else statement) since the views are recycled.

listview dynamic message using Flags

i am working on a chat application. I am able to send and receive message normally. But when i try to integrate bubble message, i need guidance. i have 2 layouts
left layout(Display sent message by me, 2. right layout() display received messages. I am able to display message i am sending in bubble message. I am not able to display the received message. How should i go ahead.
sample code is given below:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = null;
String jid = null;
String even_color, odd_color;
SharedPreferences prefList = getSharedPreferences("PrefsFile",
MODE_PRIVATE);
even_color = prefList.getString("even_bubble_color", "pink");
odd_color = prefList.getString("odd_bubble_color", "green");
int even_color_id = getResources().getIdentifier(even_color,
"drawable", "com.teks.chilltwit"), odd_color_id = getResources()
.getIdentifier(odd_color, "drawable", "com.teks.chilltwit");
//ImageView even_view, odd_view;
// System.out.println("Timeline: Position: "+position+", Length: "+data.length);
// if(position!=data.length-1){
if (jid != null) {
row = inflater.inflate(R.layout.list_row_layout_odd, parent,
false);
TextView textLabel = (TextView) row.findViewById(R.id.text);
textLabel.setText(datav);
}
else {
row = inflater.inflate(R.layout.list_row_layout_even, parent,
false);
TextView textLabel = (TextView) row.findViewById(R.id.text);
textLabel.setText("" + messages);
}
return row;
}
}
In the above code, i need to display left layout if i send the message and else i need to display right layout if i receive message. Please guide me in this issue.
Thanks.
So, the problem here is that the jid is always not null?

android listview message among two different layouts

| have almost got my application working. Now i am in final stage. I am struck with an issue to display sent and received message in dynamic listview. Based on data sent or received i need to display the text in listview. I am able to view sent message in. I am able to receive the message from receiver but i am unable to display that in a listview.
sample code:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
String query = null;
// String even_color, odd_color;
SharedPreferences prefList = getSharedPreferences("PrefsFile",
MODE_PRIVATE);
if(query == data){
row = inflater.inflate(R.layout.list_row_layout_odd, parent,
false);
TextView textLabel = (TextView) row.findViewById(R.id.text);
textLabel.setText(data);
}
else
{
row = inflater.inflate(R.layout.list_row_layout_even, parent,
false);
TextView textLabel = (TextView) row.findViewById(R.id.text);
textLabel.setText(datas);
}
return row;
}
}
If i sent message i need to display the msg in ie(list_row_layout_odd) and if i recieve message i need to display in (list_row_layout_even) layout.
please help me in solving this issue.
Thanks in advance.

Categories

Resources