Reputation: 131
I am learning how to retrieve data stored in Parse.I am able to retrieve data from parse and bind it to simple ListView. Now I am trying to mock a simple chat app. I have two column in my parse class to store senders name and Chat message. I have made a Custom ListView to show the senders name and message as shown below
<?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="wrap_content"
android:orientation="vertical">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/UserNameTxt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textColor="@color/material_blue_grey_900"
android:textSize="15sp" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/UserMessageTxt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textColor="@color/material_blue_grey_900"
android:textSize="25sp" />
</LinearLayout>
And An Adapter Class to bind the data
public class ChatAdapter extends ArrayAdapter<Chat> {
Context context;
int layoutResourceId;
Chat data[] = null;
public ChatAdapter(Context context, int resource, Chat[] objects) {
super(context, resource, objects);
this.layoutResourceId=resource;
this.context=context;
this.data=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
ChatHolder holder=null;
if(row==null)
{
LayoutInflater inflater=((Activity)context).getLayoutInflater();
row=inflater.inflate(layoutResourceId,parent,false);
holder= new ChatHolder();
holder.UserName=(TextView)row.findViewById(R.id.UserNameTxt);
holder.UserMessage=(TextView)row.findViewById(R.id.UserMessageTxt);
row.setTag(holder);
}
else
{
holder=(ChatHolder)row.getTag();
}
Chat chat=data[position];
holder.UserName.setText(Chat.UserName);
holder.UserMessage.setText(Chat.UserMessage);
return row;
}
static class ChatHolder
{
TextView UserName;
TextView UserMessage;
}
}
And the chat class is
public class Chat {
public static String UserName;
public static String UserMessage;
public Chat()
{
super();
}
public Chat(String _UserName,String _UserMessage)
{
super();
UserName=_UserName;
UserMessage=_UserMessage;
}
public String getName()
{
return UserName;
}
}
Code for Main Activity is (I have just included the Important parts)
List<Chat> chatData=new ArrayList<Chat>();
@Override
protected Void doInBackground(Void... params) {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Chat");
try {
ob = query.find();
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Chat c;
listview = (ListView) findViewById(R.id.listView);
for (ParseObject country : ob) {
chatData.add(new Chat((String) country.get("Sender"),(String) country.get("ChatMessage")));
}
String name01=chatData.get(0).getName(); //for debugging
String name02=chatData.get(1).getName(); //for debugging
String name03=chatData.get(2).getName(); //for debugging
Chat chatArray[]=chatData.toArray(new Chat[chatData.size()]);
String name=chatArray[0].getName(); //for debugging
String name1=chatArray[1].getName(); //for debugging
String name2=chatArray[2].getName(); //for debugging
ChatAdapter cAdapter=new ChatAdapter(MainActivity.this,R.layout.listview_item,chatArray);
listview.setAdapter(cAdapter);
mProgressDialog.dismiss();
}
Screenshot of the output https://i.sstatic.net/bxnto.png
I have three rows of data in my Parse class. As an output I am getting the last inserted row only three times. Here is the screenshot of debugging Windows https://i.sstatic.net/l7CAs.png
Inside for loop I am getting the proper values from the parse but After adding it to the chatData list something wrong happens.
This is my first question here. I am extremely sorry for the big post and if I am not clear with the question. Thank You
Upvotes: 3
Views: 250
Reputation: 14731
It seems to me that static UserName
and UserMessage
in your Chat class are the cause of your problem. Change them to
public String UserName; //from public static String UserName;
public String UserMessage; //from public static String UserMessage;
and also change this part in your adapter:
holder.UserName.setText(chat.UserName); // from Chat.UserName
holder.UserMessage.setText(chat.UserMessage); // from Chat.UserMessage
Upvotes: 1