First of all I am new to android programming so please go easy of me and thanks in advance. So I have a simple, two activity android blog app. I create blogs in one activity and display them in the other. For the start each blog has the same title and author, but user can insert the content. When I insert content everything seems to be fine. However when I'm loading a blog title displays fine and content stays empty, can anyone help me?
This is loading code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button loadButton = (Button) findViewById(R.id.loadButton);
final TextView titleDisplay = (TextView) findViewById(R.id.titleLoadDisplay);
final TextView contentDisplay = (TextView) findViewById(R.id.contentLoadDisplay);
BlogDbHelper dbHelper = new BlogDbHelper(getApplicationContext());
final SQLiteDatabase db = dbHelper.getReadableDatabase();
//What I will use after the query
final String[] projection = {
BlogContract.BlogEntry.BLOG_TITLE,
BlogContract.BlogEntry.BLOG_CONTENT
};
if (loadButton != null) {
loadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = db.query(
BlogContract.BlogEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
null
);
res.moveToFirst();
if (titleDisplay != null) {
titleDisplay.setText(res.getString(res.getColumnIndex(BlogContract.BlogEntry.BLOG_TITLE)));
}
if (contentDisplay != null) {
contentDisplay.setText(res.getString(res.getColumnIndex(BlogContract.BlogEntry.BLOG_CONTENT)));
}
}
});
}
// blogList.setAdapter(adapter);
}
This is uploading code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_blog);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
final EditText blogContentInput = (EditText) findViewById(R.id.blogContent);
String blogContent = null;
if (blogContentInput != null) {
blogContent = String.valueOf(blogContentInput.getText());
}else{
Log.e("Blog Content","BLog content not instantiated");
}
BlogDbHelper dbHelper = new BlogDbHelper(getApplicationContext());
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (fab != null) {
final String finalBlogContent = blogContent;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Content", "Blog content " + String.valueOf(blogContentInput.getText()));
ContentValues values = new ContentValues();
values.put(BlogContract.BlogEntry.BLOG_TITLE, "Practice_02");
values.put(BlogContract.BlogEntry.BLOG_AUTHOR, "MarkoVlaic");
values.put(BlogContract.BlogEntry.BLOG_CONTENT, String.valueOf(blogContentInput.getText()));
long newRowId;
newRowId = db.insertWithOnConflict(BlogContract.BlogEntry.TABLE_NAME, null, values,SQLiteDatabase.CONFLICT_REPLACE);
blogContentInput.setText("");
Snackbar.make(view, "You uploaded a blog", Snackbar.LENGTH_SHORT).show();
/*Intent intent = new Intent(view.getContext(),MainActivity.class);
startActivity(intent);*/
}
});
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Aucun commentaire:
Enregistrer un commentaire