I'm making an android application for which I need to create a post using parse and need to display the post in the same activity. I have an activity which contains a list view, an edit text and a send button. The user writes a text and press the send button. After pressing the send button a parse object is created on parse.com, which has to be fetched and displayed in the list view. The problem I'm facing is that the object is created in the parse.com but I'm unable to view it in my activity.
Here's my code: (EditTextMessage.java)
import java.util.ArrayList;
import java.util.List;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EditTextMessage extends ListActivity {
private TextMessage mTextMessage;
private EditText contentEditText;
private String content;
private Button sendButton;
private List<TextMessage> posts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_edit_text_message);
//creates an array adapter to hold the posts
posts = new ArrayList<TextMessage>();
ArrayAdapter<TextMessage> adapter = new ArrayAdapter<TextMessage>(this,
R.layout.message_layout, posts);
setListAdapter(adapter);
Intent intent = this.getIntent();
contentEditText = (EditText) findViewById(R.id.TypeMessage);
if (intent.getExtras() != null) {
mTextMessage = new TextMessage(intent.getStringExtra("TextId"), intent.getStringExtra("TextContent"));
contentEditText.setText(mTextMessage.getContent());
}
sendButton = (Button)findViewById(R.id.SendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveNote();
}
});
//Refreshes posts
refreshPostList();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
refreshPostList();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.edit_note, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id==R.id.action_delete)
{
ParseQuery<ParseObject> mquery = ParseQuery.getQuery("Message");
mquery.getInBackground(mTextMessage.getId(), new GetCallback<ParseObject>() {
@Override
public void done(ParseObject post, ParseException e) {
// TODO Auto-generated method stub
if(e==null)
{
post.deleteInBackground();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
else {
// The save failed.
Toast.makeText(getApplicationContext(), "Failed to delete", Toast.LENGTH_SHORT).show();
Log.d(getClass().getSimpleName(), "User update error: " + e);
}
}
});
}
return super.onOptionsItemSelected(item);
}
private void saveNote() {
content = contentEditText.getText().toString();
content = content.trim();
if (!content.isEmpty()) {
// Check if post is being created or edited
if (mTextMessage == null) {
// create new post
final ParseObject post = new ParseObject("Message");
post.put("msg", content);
post.put("sender", ParseUser.getCurrentUser());
setProgressBarIndeterminateVisibility(true);
post.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Saved successfully.
mTextMessage = new TextMessage(post.getObjectId(), content);
Toast.makeText(getApplicationContext(), "Sent", Toast.LENGTH_SHORT).show();
} else {
// The save failed.
Toast.makeText(getApplicationContext(), "Failed to send", Toast.LENGTH_SHORT).show();
Log.d(getClass().getSimpleName(), "User update error: " + e);
}
}
});
}
else {
// update post
ParseQuery<ParseObject> query = ParseQuery.getQuery("Message");
// Retrieve the object by id
query.getInBackground(mTextMessage.getId(), new GetCallback<ParseObject>() {
public void done(ParseObject post, ParseException e) {
if (e == null) {
// Now let's update it with some new data.
post.put("msg", content);
setProgressBarIndeterminateVisibility(true);
post.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Saved successfully.
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
} else {
// The save failed.
Toast.makeText(getApplicationContext(), "Failed to Save", Toast.LENGTH_SHORT).show();
Log.d(getClass().getSimpleName(), "User update error: " + e);
}
}
});
}
}
});
}
}
else if (content.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(EditTextMessage.this);
builder.setMessage(R.string.edit_error_message)
.setTitle(R.string.edit_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
private void refreshPostList() {
//query gets access to posts in parse
ParseQuery<ParseObject> query = ParseQuery.getQuery("Message");
query.whereEqualTo("sender", ParseUser.getCurrentUser());
setProgressBarIndeterminateVisibility(true);
query.findInBackground(new FindCallback<ParseObject>() {
@SuppressWarnings("unchecked")
@Override
public void done(List<ParseObject> postList, ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// If there are results, update the list of posts
// and notify the adapter
posts.clear();
for (ParseObject post : postList) {
TextMessage note = new TextMessage(post.getObjectId(), post.getString("msg"));
posts.add(note);
}
((ArrayAdapter<TextMessage>) getListAdapter())
.notifyDataSetChanged();
} else {
Log.d(getClass().getSimpleName(), "Error: Some error" + e.getMessage());
}
}
});
}
}
TextMessage.java:
public class TextMessage {
private String id;
private String title;
private String content;
TextMessage(String TextId, String TextContent) {
id = TextId;
content = TextContent;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return this.getTitle();
}
}
activity_edit_text_message.xml:
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ish.noteapp.EditTextMessage" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/TypeMessage" >
</ListView>
<EditText
android:id="@+id/TypeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:layout_alignParentBottom="true"
android:hint="@string/type_your_message"
android:ems="10" />
<Button
android:id="@+id/SendButton"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/send" />
</RelativeLayout>
message_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="5dp"
android:singleLine="true"
android:textSize="20sp"
android:ellipsize="end">
</TextView>
I can see that the parse objects being created in the Message class in parse.com, but I'm either unable to fetch them properly or not able to view them.
Please let me know if more information is needed.
I'll be really grateful if anyone could help me solve this problem.
Tiada ulasan:
Catat Ulasan