Advertisement
Guest User

Untitled

a guest
May 29th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.47 KB | None | 0 0
  1. "package com.android.project;
  2.  
  3. import java.sql.SQLException;
  4. import android.view.ContextMenu;
  5. import android.view.ContextMenu.ContextMenuInfo;
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.app.AlertDialog.Builder;
  9. import android.app.Dialog;
  10. import android.app.ListActivity;
  11. import android.content.Context;
  12. import android.content.Intent;
  13. import android.database.Cursor;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.view.ContextMenu;
  17. import android.view.ContextMenu.ContextMenuInfo;
  18. import android.view.KeyEvent;
  19. import android.view.LayoutInflater;
  20. import android.view.MenuInflater;
  21. import android.view.MenuItem;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24. import android.widget.AdapterView;
  25. import android.widget.AdapterView.AdapterContextMenuInfo;
  26. import android.widget.AdapterView.OnItemClickListener;
  27. import android.widget.AdapterView.OnItemLongClickListener;
  28. import android.widget.Button;
  29. import android.widget.CursorAdapter;
  30. import android.widget.EditText;
  31. import android.widget.ImageButton;
  32. import android.widget.ListView;
  33. import android.widget.SimpleCursorAdapter;
  34. import android.widget.TextView;
  35. import android.widget.Toast;
  36.  
  37. public class DiaryActivity extends Activity implements OnClickListener,
  38.         OnItemClickListener {
  39.  
  40.     public static final String ROW_ID = "row_id";
  41.     static long row_passed;
  42.     ImageButton add;
  43.     ListView list;
  44.     public static int position_item;
  45.     static long id_item_clicked;
  46.     SimpleCursorAdapter myAdapter;
  47.     DataHolder myDataHolder;
  48.     EditText Text;
  49.     static String title = "";
  50.     static String story = "";
  51.  
  52.     /** Called when the activity is first created. */
  53.     @Override
  54.     public void onCreate(Bundle savedInstanceState) {
  55.         super.onCreate(savedInstanceState);
  56.         setContentView(R.layout.main);
  57.         initialiseVariables();
  58.         registerForContextMenu(list);
  59.  
  60.         add.setOnClickListener(this);
  61.     }
  62.  
  63.     private void initialiseVariables() {
  64.         // TODO Auto-generated method stub
  65.         add = (ImageButton) findViewById(R.id.addButton);
  66.         list = (ListView) findViewById(R.id.mylist);
  67.         try {
  68.             myDataHolder = new DataHolder(DiaryActivity.this);
  69.         } catch (Exception e1) {
  70.             // TODO Auto-generated catch block
  71.             e1.printStackTrace();
  72.         }
  73.         try {
  74.             myDataHolder.open();
  75.         } catch (SQLException e) {
  76.             // TODO Auto-generated catch block
  77.             e.printStackTrace();
  78.             String error = e.toString();
  79.             Dialog d = new Dialog(this);
  80.             d.setTitle("Error");
  81.             TextView tv = new TextView(this);
  82.             tv.setText(error);
  83.             d.setContentView(tv);
  84.             d.show();
  85.  
  86.         } finally {
  87.             Cursor c = myDataHolder.getAll();
  88.             c.moveToFirst();
  89.             // Log.d("Message", c.getString(1));
  90.             myAdapter = new SimpleCursorAdapter(DiaryActivity.this,
  91.                     R.layout.mystoryholder, c, new String[] {
  92.  
  93.                     DataHolder.KEY_TITLE, DataHolder.KEY_DAY,
  94.                             DataHolder.KEY_MONTH, DataHolder.KEY_YEAR },
  95.                     new int[] { R.id.titleTag, R.id.tvDate, R.id.tvMonth,
  96.                             R.id.tvYear });
  97.             list.setAdapter(myAdapter);
  98.  
  99.             list.setOnItemClickListener(this);
  100.             myDataHolder.close();
  101.         }
  102.  
  103.     }
  104.  
  105.     @Override
  106.     public void onClick(View v) {
  107.         // TODO Auto-generated method stub
  108.         switch (v.getId()) {
  109.         case R.id.addButton:
  110.             Intent goTo = new Intent(DiaryActivity.this, AddInfo.class);
  111.             startActivity(goTo);
  112.             break;
  113.  
  114.         }
  115.  
  116.     }
  117.  
  118.     @Override
  119.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  120.        
  121.         registerForContextMenu(arg1);
  122.         id_item_clicked = arg3;
  123.         position_item = arg2;
  124.         Cursor c = (Cursor) arg0.getItemAtPosition(arg2);
  125.         title = c.getString(c.getColumnIndex(DataHolder.KEY_TITLE));
  126.         story = c.getString(c.getColumnIndex(DataHolder.KEY_STORY));
  127.         Intent i = null;
  128.  
  129.         // remove this toast later
  130.         Toast.makeText(DiaryActivity.this, "TestClick", Toast.LENGTH_SHORT)
  131.                 .show();
  132.  
  133.         try {
  134.             i = new Intent(DiaryActivity.this,
  135.                     Class.forName("com.android.project.DataReadSQLite"));
  136.             i.putExtra(ROW_ID, arg3);
  137.  
  138.         } catch (ClassNotFoundException e) {
  139.             e.printStackTrace();
  140.         } finally {
  141.  
  142.             startActivity(i);
  143.  
  144.         }
  145.  
  146.     }
  147.  
  148.     /*
  149.      * @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
  150.      * int arg2, long arg3) { // TODO Auto-generated method stub
  151.      *
  152.      * return false; }
  153.      */
  154.  
  155.     @Override
  156.     public void onCreateContextMenu(ContextMenu menu, View v,
  157.             ContextMenuInfo menuInfo) {
  158.         // TODO Auto-generated method stub
  159.         super.onCreateContextMenu(menu, v, menuInfo);
  160.         menu.add("EDIT");
  161.         menu.add("READ");
  162.         menu.add("DELETE");
  163.     }
  164.  
  165.     @Override
  166.     public boolean onContextItemSelected(MenuItem item) {
  167.         // TODO Auto-generated method stub
  168.         if (item.getTitle() == "EDIT") {
  169.     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
  170.                     .getMenuInfo();
  171.  
  172.             Cursor c = (Cursor) list.getItemAtPosition(position_item);
  173.             title = c.getString(c.getColumnIndex(DataHolder.KEY_TITLE));
  174.             story = c.getString(c.getColumnIndex(DataHolder.KEY_STORY));
  175.             Intent goforedit = null;
  176.  
  177.             goforedit = new Intent(DiaryActivity.this, DataView.class);
  178.             goforedit.putExtra(ROW_ID, id_item_clicked);
  179.             startActivity(goforedit);
  180.  
  181.         } // still need to work on
  182.         if (item.getTitle() == "READ") {
  183.             // reads the data entered
  184.             // over write the date if edit button in the new activity is clicked
  185.         }
  186.         if (item.getTitle() == "DELETE") {
  187.             // delete the data entry
  188.             // deletes the corresponding entry
  189.         }
  190.         return super.onContextItemSelected(item);
  191.     }
  192.  
  193.     @Override
  194.     public void onBackPressed() {
  195.         super.onBackPressed();
  196.         finish();
  197.     }
  198.  
  199. }"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement