3

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

5
  • Nobody will read through all this code. Try to create an as short as possible case that does not work as expected. Chances are, you'll find your error yourself in doing so. Commented Jun 18, 2015 at 17:55
  • Sorry I disagree. OP has done an excellent job of providing the details needed. If you follow most of the questions, the first comment usually is to ask the OP to post the layout file, code etc... In fact, I am really impressed by OP actually pasting only the relevant code and putting screen shots of both output image and debug image. Commented Jun 18, 2015 at 17:59
  • Since you are not initializing the chartdata array and keep adding, could it be that you have more than 3 elements and the first 3 elements somehow have the wrong data? When you debug, can you check what does chartdata contain after adding the elements from Parse query? Commented Jun 18, 2015 at 18:14
  • Some suggestions. Generally, you want to put all the actual background processing logic inside the doInBackground. That means, whatever you are doing in the postExecute, you should move it to doInBackground. Also, don't use any UI elements in the background thread. You don't want to create the new adapter and set it to listview from the thread. Instead, you always return the result and have the activity that invoked this thread set it to the listview. Which also means, you may need some sort of callback interface that your activity implements and then call the method from here. Commented Jun 18, 2015 at 18:18
  • Thank you. I'll surely consider that. Commented Jun 19, 2015 at 16:37

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.