Back to project page android_tutorial_projects.
The source code is released under:
Copyright (c) 2013, Uthcode All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redi...
If you think the Android project android_tutorial_projects listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.uthcode.testdatabaseactivity; /*from w w w .ja v a2 s .c o m*/ import android.app.ListActivity; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; import android.widget.ArrayAdapter; import java.sql.SQLException; import java.util.List; import java.util.Random; public class MainActivity extends ListActivity{ private CommentsDataSource datasource; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datasource = new CommentsDataSource(this); try { datasource.open(); } catch (SQLException e) { e.printStackTrace(); } List<Comment> values = datasource.getAllComments(); ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } public void onClick(View view) { ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter(); Comment comment = null; switch(view.getId()) { case R.id.add: String[] comments = new String[] {"Cool", "very nice ", "Hate it"}; int nextInt = new Random().nextInt(3); comment = datasource.createComment(comments[nextInt]); adapter.add(comment); break; case R.id.delete: if (getListAdapter().getCount() > 0 ) { comment = (Comment) getListAdapter().getItem(0); datasource.deleteComment(comment); adapter.remove(comment); } break; } adapter.notifyDataSetChanged(); } @Override protected void onPause() { datasource.close(); super.onPause(); } @Override protected void onResume() { try { datasource.open(); } catch (SQLException e) { e.printStackTrace(); } super.onResume(); } }