Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 25.17 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. I have again a Nullpointerexeption from seekbar in android [closed]
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/add_hw_dialog"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:orientation="vertical"
  7.     android:padding="10dp" >
  8.  
  9.  
  10.     <TextView
  11.         android:id="@+id/txt_Pruefung"
  12.         android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content"
  14.         android:text="Prüfungsname" />
  15.  
  16.     <EditText
  17.         android:id="@+id/insert_markname"
  18.         android:layout_width="fill_parent"
  19.         android:layout_height="wrap_content" />
  20.  
  21.     <RelativeLayout
  22.         android:layout_width="wrap_content"
  23.         android:layout_height="wrap_content" >
  24.  
  25.         <TextView
  26.             android:id="@+id/txt_gewicht"
  27.             android:layout_width="wrap_content"
  28.             android:layout_height="wrap_content"
  29.             android:text="Gewicht: " />
  30.  
  31.         <TextView
  32.             android:id="@+id/txt_weight_change"
  33.             android:layout_width="wrap_content"
  34.             android:layout_height="wrap_content"
  35.             android:layout_alignParentRight="true"
  36.             android:layout_alignParentTop="true"/>
  37.     </RelativeLayout>
  38.  
  39.     <SeekBar
  40.         android:id="@+id/seekBar_wertung"
  41.         android:layout_width="fill_parent"
  42.         android:layout_height="wrap_content" />
  43.  
  44.     <TextView
  45.         android:id="@+id/TextView02"
  46.         android:layout_width="wrap_content"
  47.         android:layout_height="wrap_content"
  48.         android:text="Note" />
  49.  
  50.     <EditText
  51.         android:id="@+id/insert_mark"
  52.         android:layout_width="fill_parent"
  53.         android:layout_height="wrap_content"
  54.         android:inputType="numberDecimal" >
  55.         <requestFocus />
  56.     </EditText>
  57.  
  58.     <TextView
  59.         android:id="@+id/textView2"
  60.         android:layout_width="wrap_content"
  61.         android:layout_height="wrap_content"
  62.         android:text="Datum" />
  63.  
  64.     <DatePicker
  65.         android:id="@+id/date_maturity_mark"
  66.         android:layout_width="wrap_content"
  67.         android:layout_height="75dp" />
  68.  
  69.     <LinearLayout
  70.         android:layout_width="fill_parent"
  71.         android:layout_height="wrap_content"
  72.         android:layout_marginTop="15dip"
  73.         android:orientation="horizontal" >
  74.  
  75.         <Button
  76.             android:id="@+id/cmd_add_mark"
  77.             android:layout_width="wrap_content"
  78.             android:layout_height="wrap_content"
  79.             android:text="Note hinzufügen" />
  80.  
  81.         <Button
  82.             android:id="@+id/cmd_close_popup"
  83.             android:layout_width="wrap_content"
  84.             android:layout_height="wrap_content"
  85.             android:text="@string/close" />
  86.     </LinearLayout>
  87. </LinearLayout>
  88.        
  89. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  90.     android:id="@+id/add_hw_dialog"
  91.     android:layout_width="fill_parent"
  92.     android:layout_height="fill_parent"
  93.     android:orientation="vertical"
  94.     android:padding="10dp" >
  95.  
  96.  
  97.     <TextView
  98.         android:id="@+id/txt_Pruefung"
  99.         android:layout_width="wrap_content"
  100.         android:layout_height="wrap_content"
  101.         android:text="Prüfungsname" />
  102.  
  103.     <EditText
  104.         android:id="@+id/insert_markname"
  105.         android:layout_width="fill_parent"
  106.         android:layout_height="wrap_content" />
  107.  
  108.     <RelativeLayout
  109.         android:layout_width="wrap_content"
  110.         android:layout_height="wrap_content" >
  111.  
  112.         <TextView
  113.             android:id="@+id/txt_gewicht"
  114.             android:layout_width="wrap_content"
  115.             android:layout_height="wrap_content"
  116.             android:text="Gewicht: " />
  117.  
  118.         <TextView
  119.             android:id="@+id/txt_weight_change"
  120.             android:layout_width="wrap_content"
  121.             android:layout_height="wrap_content"
  122.             android:layout_alignParentRight="true"
  123.             android:layout_alignParentTop="true"/>
  124.     </RelativeLayout>
  125.  
  126.     <SeekBar
  127.         android:id="@+id/seekBar_wertung"
  128.         android:layout_width="fill_parent"
  129.         android:layout_height="wrap_content" />
  130.  
  131.     <TextView
  132.         android:id="@+id/TextView02"
  133.         android:layout_width="wrap_content"
  134.         android:layout_height="wrap_content"
  135.         android:text="Note" />
  136.  
  137.     <EditText
  138.         android:id="@+id/insert_mark"
  139.         android:layout_width="fill_parent"
  140.         android:layout_height="wrap_content"
  141.         android:inputType="numberDecimal" >
  142.         <requestFocus />
  143.     </EditText>
  144.  
  145.     <TextView
  146.         android:id="@+id/textView2"
  147.         android:layout_width="wrap_content"
  148.         android:layout_height="wrap_content"
  149.         android:text="Datum" />
  150.  
  151.     <DatePicker
  152.         android:id="@+id/date_maturity_mark"
  153.         android:layout_width="wrap_content"
  154.         android:layout_height="75dp" />
  155.  
  156.     <LinearLayout
  157.         android:layout_width="fill_parent"
  158.         android:layout_height="wrap_content"
  159.         android:layout_marginTop="15dip"
  160.         android:orientation="horizontal" >
  161.  
  162.         <Button
  163.             android:id="@+id/cmd_add_mark"
  164.             android:layout_width="wrap_content"
  165.             android:layout_height="wrap_content"
  166.             android:text="Note hinzufügen" />
  167.  
  168.         <Button
  169.             android:id="@+id/cmd_close_popup"
  170.             android:layout_width="wrap_content"
  171.             android:layout_height="wrap_content"
  172.             android:text="@string/close" />
  173.     </LinearLayout>
  174. </LinearLayout>
  175.        
  176. static final int ADD_MARK_DIALOG = 0;
  177. static final int EDIT_MARK_DIALOG = 1;
  178. final int CONTEXT_MENU_DELETE_ITEM =1;
  179. final int CONTEXT_MENU_EDIT_ITEM =2;
  180. SeekBar seekBar_wertung, seek_edit_gewicht, seek_gewicht, seek_gewichtt;
  181.  
  182. TextView gewicht, txt_durchschnitt;
  183.  
  184. String mark, gewicht_mark, name, id_edit, teststring, txt_edit_mark_name, txt_edit_mark_mark, txt_edit_weigh_trim;
  185. EditText insert_markname, edit_mark_name, insert_note, edit_mark_mark;
  186. Button cmd_add_mark, cmd_close_popup, cmd_save_mark, cmd_close_mark;
  187. int progress, gewicht_mark_int;
  188.  
  189. OnSeekBarChangeListener yourSeekBarListener = new OnSeekBarChangeListener() {
  190.  
  191. public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
  192.     gewicht.setText(progress+"%");
  193. }
  194.  
  195. public void onStartTrackingTouch(SeekBar seekBar) {
  196. }
  197.  
  198. public void onStopTrackingTouch(SeekBar seekBar) {
  199.     seekBar.setSecondaryProgress(seekBar.getProgress());
  200. }
  201.  
  202. };
  203.  
  204. protected Dialog onCreateDialog(int id) {
  205.  Dialog dialog = null;;
  206.     switch(id) {
  207.     case ADD_MARK_DIALOG:
  208.      dialog = new Dialog(mark.this);
  209.  
  210.      dialog.setContentView(R.layout.dialog_context_mark);
  211.      dialog.setTitle("Note hinzufügen");
  212.  
  213.      insert_markname = (EditText)dialog.findViewById(R.id.insert_markname);
  214.      insert_note    = (EditText)dialog.findViewById(R.id.insert_mark);
  215.      cmd_add_mark = (Button)dialog.findViewById(R.id.cmd_add_mark);
  216.      cmd_close_popup = (Button)dialog.findViewById(R.id.cmd_close_popup);
  217.      date_pick = (DatePicker)dialog.findViewById(R.id.date_maturity_mark);
  218.      seek_gewicht = (SeekBar)dialog.findViewById(R.id.seekBar_wertung);
  219.      seek_gewicht.setOnSeekBarChangeListener(yourSeekBarListener);
  220.  
  221.      gewicht = (TextView)dialog.findViewById(R.id.txt_weight_change);
  222.      cmd_add_mark.setOnClickListener(add_mark);
  223.      cmd_close_popup.setOnClickListener(close_popup);
  224.  
  225.  
  226.         break;
  227.     case EDIT_MARK_DIALOG:      
  228.         dialog = new Dialog(mark.this);
  229.  
  230.         dialog.setContentView(R.layout.dialog_context_mark_edit);
  231.         dialog.setTitle("Note bearbeiten");
  232.  
  233.         edit_mark_name = (EditText)dialog.findViewById(R.id.edit_mark_name);
  234.         edit_mark_mark = (EditText)dialog.findViewById(R.id.edit_mark_note);
  235.         cmd_save_mark = (Button)dialog.findViewById(R.id.cmd_save_mark);
  236.         cmd_close_mark = (Button)dialog.findViewById(R.id.cmd_close_mark);
  237.         date_pick_edit = (DatePicker)dialog.findViewById(R.id.date_maturity_mark_edit);
  238.         seek_gewichtt = (SeekBar)dialog.findViewById(R.id.seek_edit_gewicht);
  239.         seek_gewichtt.setOnSeekBarChangeListener(yourSeekBarListener);
  240.  
  241.         edit_mark_name.setText(name);
  242.         edit_mark_mark.setText(mark);
  243.  
  244.         seek_gewichtt.setProgress(gewicht_mark_int);
  245.  
  246.         gewicht = (TextView)dialog.findViewById(R.id.gewicht);
  247.         gewicht.setText(""+gewicht_mark);
  248.         cmd_save_mark.setOnClickListener(save_mark);
  249.         cmd_close_mark.setOnClickListener(close_popup_mark);
  250.  
  251.            break;
  252.     }
  253.  
  254.     return dialog;
  255. }
  256.        
  257. 02-08 09:44:00.582: D/dalvikvm(7476): GC_EXTERNAL_ALLOC freed 87K, 48% free 2903K/5511K, external 4228K/4230K, paused 21ms
  258. 02-08 09:44:00.617: D/AndroidRuntime(7476): Shutting down VM
  259. 02-08 09:44:00.617: W/dalvikvm(7476): threadid=1: thread exiting with uncaught exception (group=0x4022f568)
  260. 02-08 09:44:00.625: E/AndroidRuntime(7476): FATAL EXCEPTION: main
  261. 02-08 09:44:00.625: E/AndroidRuntime(7476): java.lang.NullPointerException
  262. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at test.marco.notenha.mark$1.onProgressChanged(mark.java:83)
  263. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.SeekBar.onProgressRefresh(SeekBar.java:90)
  264. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:544)
  265. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:523)
  266. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.ProgressBar.refreshProgress(ProgressBar.java:558)
  267. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.ProgressBar.setProgress(ProgressBar.java:607)
  268. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.ProgressBar.setProgress(ProgressBar.java:588)
  269. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at test.marco.notenha.mark.onCreateDialog(mark.java:359)
  270. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.app.Activity.onCreateDialog(Activity.java:2506)
  271. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.app.Activity.createDialog(Activity.java:885)
  272. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.app.Activity.showDialog(Activity.java:2581)
  273. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.app.Activity.showDialog(Activity.java:2548)
  274. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at test.marco.notenha.mark.onContextItemSelected(mark.java:257)
  275. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.app.Activity.onMenuItemSelected(Activity.java:2233)
  276. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2888)
  277. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
  278. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:857)
  279. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:138)
  280. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at com.android.internal.app.AlertController$AlertParams$5.onItemClick(AlertController.java:928)
  281. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
  282. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.ListView.performItemClick(ListView.java:3535)
  283. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1827)
  284. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.os.Handler.handleCallback(Handler.java:587)
  285. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.os.Handler.dispatchMessage(Handler.java:92)
  286. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.os.Looper.loop(Looper.java:130)
  287. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at android.app.ActivityThread.main(ActivityThread.java:3703)
  288. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at java.lang.reflect.Method.invokeNative(Native Method)
  289. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at java.lang.reflect.Method.invoke(Method.java:507)
  290. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  291. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  292. 02-08 09:44:00.625: E/AndroidRuntime(7476):     at dalvik.system.NativeStart.main(Native Method)
  293.        
  294. public class mark extends ListActivity{
  295.     /** Called when the activity is first created. */
  296.  
  297.     SQLiteDatabase db;
  298.     private dbHelper mDbHelper;
  299.     static final int ADD_MARK_DIALOG = 0;
  300.     static final int EDIT_MARK_DIALOG = 1;
  301.     final int CONTEXT_MENU_DELETE_ITEM =2;
  302.     final int CONTEXT_MENU_EDIT_ITEM =3;
  303.     SeekBar seekBar_wertung, seek_edit_gewicht, seek_gewicht, seek_gewichtt;
  304.     TextView gewicht, gewicht_test, txt_durchschnitt;
  305.  
  306.     float zahl1, zahl2, result;
  307.     String mark, gewicht_mark, name, id_edit, teststring, txt_edit_mark_name, txt_edit_mark_mark, txt_edit_weigh_trim;
  308.     EditText insert_markname, edit_mark_name, insert_note, edit_mark_mark;
  309.     Button cmd_add_mark, cmd_close_popup, cmd_save_mark, cmd_close_mark;
  310.     DatePicker date_pick, date_pick_edit;
  311.     int progress, gewicht_mark_int;
  312.  
  313.     public void onCreate(Bundle savedInstanceState) {
  314.         super.onCreate(savedInstanceState);
  315.         setContentView(R.layout.mark);
  316.  
  317.         //Create Database if necessary
  318.         db = openOrCreateDatabase("test_database.db",
  319.                 SQLiteDatabase.CREATE_IF_NECESSARY, null);
  320.  
  321.         db.setVersion(1);
  322.         db.setLocale(Locale.getDefault());
  323.         db.setLockingEnabled(true);
  324.  
  325.         //Create table tbl_homework if not exists
  326.         final String CREATE_TABLE_HW =
  327.                 "CREATE TABLE IF NOT EXISTS tbl_marks ("
  328.                 + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
  329.                 + "name TEXT NOT NULL,"
  330.                 + "subid INTEGER NOT NULL,"
  331.                 + "mark FLOAT NOT NULL,"
  332.                 + "gewicht INTEGER NOT NULL,"
  333.                 + "datum TEXT NOT NULL);";
  334.  
  335.                 db.execSQL(CREATE_TABLE_HW);
  336.                 mDbHelper = new dbHelper(this);
  337.                 mDbHelper.db = db;
  338.                 press_cmd_back();            
  339.                 fillData();
  340.                 registerForContextMenu(getListView());
  341.  
  342.     }
  343.  
  344.     OnSeekBarChangeListener yourSeekBarListener = new OnSeekBarChangeListener() {
  345.  
  346.     public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
  347.         gewicht.setText(progress+"%");
  348.     }
  349.  
  350.     public void onStartTrackingTouch(SeekBar seekBar) {
  351.     }
  352.  
  353.     public void onStopTrackingTouch(SeekBar seekBar) {
  354.         seekBar.setSecondaryProgress(seekBar.getProgress());
  355.     }
  356.  
  357.     };
  358.  
  359.     private void press_cmd_back(){
  360.         Button cmd_back = (Button)findViewById(R.id.cmd_back);
  361.         cmd_back.setOnClickListener(new View.OnClickListener() {
  362.  
  363.             public void onClick(View v) {
  364.                 Intent Intent_main = new Intent(mark.this, subjects.class);
  365.                 startActivity(Intent_main);
  366.             }
  367.         });
  368.     }
  369.  
  370.     @Override
  371.     public boolean onCreateOptionsMenu(Menu menu) {
  372.         MenuInflater inflater = getMenuInflater();
  373.         inflater.inflate(R.menu.menu_mark, menu);
  374.         return true;
  375.     }
  376.  
  377.     @Override
  378.     public boolean onOptionsItemSelected(MenuItem item) {
  379.         switch (item.getItemId()) {
  380.             case R.id.add_mark:    
  381.                 showDialog(ADD_MARK_DIALOG);
  382.                 break;
  383.         }
  384.         return true;
  385.     }
  386.  
  387.     private Button.OnClickListener add_mark = new Button.OnClickListener(){
  388.         public void onClick(View arg0) {
  389.             mDbHelper.open_database_rw();
  390.             String txt_insert_markname = insert_markname.getText().toString();
  391.             String txt_note_mark = insert_note.getText().toString();
  392.             String date_picker_message = date_pick.getDayOfMonth() + "/" + (date_pick.getMonth()+1) + "/" + date_pick.getYear();
  393.  
  394.             Bundle extras = getIntent().getExtras();
  395.             String txt_sub_id = extras.getString("IDFach");
  396.  
  397.             int seek_gewicht_progress = seek_gewicht.getProgress();
  398.  
  399.             if(txt_insert_markname.equals("")){doMessage("Bitte einen Namen eintragen!");}
  400.             if(txt_note_mark.equals("")){doMessage("Bitte eine Note eintragen!");}
  401.  
  402.             float test = Float.parseFloat(txt_note_mark);
  403.             if(test < 1){
  404.                 doMessage("Ungültige Note");
  405.             }
  406.  
  407.             if(test > 6){
  408.                 doMessage("Ungültige Note");
  409.             }
  410.  
  411.                 final String INSERT_MARK = "INSERT INTO tbl_marks ('name', 'subid', 'gewicht', 'mark', 'datum') VALUES ('"+txt_insert_markname+"', '"+txt_sub_id+"', '"+seek_gewicht_progress+"', '"+txt_note_mark+"', '"+date_picker_message+"')";
  412.                 db.execSQL(INSERT_MARK);
  413.                 insert_note.setText("");
  414.                 dismissDialog(ADD_MARK_DIALOG);
  415.                 fillData();
  416.  
  417.  
  418.             }
  419.  
  420.     };
  421.  
  422.     public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
  423.  
  424.         menu.add(Menu.NONE, CONTEXT_MENU_DELETE_ITEM, Menu.NONE, "Löschen");
  425.         menu.add(Menu.NONE, CONTEXT_MENU_EDIT_ITEM, Menu.NONE, "Bearbeiten");
  426.        }
  427.  
  428.     private Button.OnClickListener close_popup = new Button.OnClickListener(){
  429.         public void onClick(View arg0) {
  430.             dismissDialog(ADD_MARK_DIALOG);
  431.         }
  432.     };
  433.  
  434.  
  435.     private void doMessage(String message) {
  436.         AlertDialog alertDialog = new AlertDialog.Builder(mark.this).create();
  437.         alertDialog.setTitle("Fehler:");
  438.         alertDialog.setMessage(message);
  439.  
  440.         alertDialog.setButton("Erneut versuchen", new DialogInterface.OnClickListener() {
  441.         public void onClick(DialogInterface dialog, int which) {
  442.  
  443.         } });
  444.         alertDialog.show();
  445.     }
  446.  
  447.     private void fillData() {
  448.  
  449.         Bundle extras = getIntent().getExtras();
  450.         String txt_sub_id = extras.getString("IDFach");
  451.         int test = Integer.parseInt(txt_sub_id);
  452.  
  453.         Cursor mCursor = db.rawQuery("SELECT _id, subid, name, mark, gewicht, datum FROM tbl_marks WHERE subid = '"+test+"';", null);
  454.         startManagingCursor(mCursor);
  455.         if (mCursor != null && mCursor.moveToFirst()) {
  456.              int intMark = mCursor.getColumnIndex("mark");
  457.              int intWeight = mCursor.getColumnIndex("gewicht");
  458.               do {
  459.  
  460.                   String[] from = new String[] { dbHelper.KEY_NAME_MARKS, dbHelper.KEY_MARK_MARKS, dbHelper.KEY_GEWICHT_MARKS, dbHelper.KEY_DATUM_MARKS};
  461.                   int[] to = new int[] {R.id.txt_marks_row, R.id.txt_note, R.id.txt_gewicht, R.id.txt_datum};
  462.  
  463.                   float weight = mCursor.getFloat(intWeight);
  464.  
  465.                   float mark = mCursor.getFloat(intMark);
  466.  
  467.                   zahl1 = zahl1 + (weight * mark);
  468.                   zahl2 = zahl2 + weight;
  469.                   SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.show_marks, mCursor, from, to);
  470.                   setListAdapter(notes);
  471.               } while (mCursor.moveToNext());
  472.  
  473.               txt_durchschnitt = (TextView)findViewById(R.id.txt_durchschnitt);
  474.               result = zahl1 / zahl2;
  475.               float result_neu = (float) (Math.round(result * 100) / 100.0);
  476.  
  477.               txt_durchschnitt.setText(""+result_neu);
  478.         }
  479.     }
  480.  
  481.     @Override
  482.     public boolean onContextItemSelected(MenuItem item) {
  483.  
  484.          AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
  485.          long id = getListAdapter().getItemId(info.position); /*what item was selected is ListView*/
  486.  
  487.  
  488.          switch (item.getItemId()) {
  489.                  case CONTEXT_MENU_DELETE_ITEM:
  490.                      mDbHelper.open_database_rw();
  491.                      String id_delete = Long.toString(id);
  492.                      try{
  493.                          delItem(id_delete);
  494.                          Toast.makeText(this, "Gelöscht!", Toast.LENGTH_SHORT).show();
  495.                          fillData();
  496.                      }
  497.                      catch(SQLiteException e){
  498.                          Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show();
  499.                      }
  500.                      return(true);
  501.  
  502.                  case CONTEXT_MENU_EDIT_ITEM:
  503.                      try{
  504.                          mDbHelper.open_database_rw();
  505.                          id_edit = Long.toString(id);
  506.  
  507.                          Cursor cursor = db.query("tbl_marks", new String[] {"name", "mark", "gewicht"},
  508.                                     "_id="+id_edit, null, null, null, null);
  509.                          startManagingCursor(cursor);
  510.  
  511.                          cursor.moveToFirst();
  512.  
  513.                          int name_mark = cursor.getColumnIndex("name");
  514.                          name = cursor.getString(name_mark);
  515.                          int mark_mark = cursor.getColumnIndex("mark");
  516.                          mark = cursor.getString(mark_mark);
  517.                          int mark_gewicht_int = cursor.getColumnIndex("gewicht");
  518.                          gewicht_mark = cursor.getString(mark_gewicht_int);
  519.                          gewicht_mark_int = Integer.parseInt(gewicht_mark);
  520.                          removeDialog(EDIT_MARK_DIALOG);
  521.                          showDialog(EDIT_MARK_DIALOG);
  522.  
  523.                          registerForContextMenu(getListView());
  524.                          fillData();
  525.                      }
  526.                      catch(SQLiteException e){
  527.                          Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show();
  528.                      }
  529.                      return(true);
  530.          }
  531.      return(super.onOptionsItemSelected(item));
  532.    }
  533.  
  534.     public void delItem(String item){
  535.         db.delete("tbl_marks", "_id="+item, null);
  536.     }
  537.  
  538.  
  539.  
  540.  
  541.  
  542.     private Button.OnClickListener save_mark = new Button.OnClickListener(){
  543.         public void onClick(View arg0) {
  544.  
  545.             String txt_edit_name = edit_mark_name.getText().toString();
  546.             String date_picker_message_edit = date_pick_edit.getDayOfMonth() + "/" + (date_pick_edit.getMonth()+1) + "/" + date_pick_edit.getYear();
  547.             String txt_edit_mark = edit_mark_mark.getText().toString();
  548.             int txt_edit_weight_int = seek_gewichtt.getProgress();
  549.             String txt_edit_weight = ""+txt_edit_weight_int;
  550.  
  551.             txt_edit_mark_mark = txt_edit_mark.trim();
  552.             txt_edit_mark_name = txt_edit_name.trim();
  553.             txt_edit_weigh_trim = txt_edit_weight.trim();
  554.  
  555.             ContentValues args = new ContentValues();
  556.             args.put("name", txt_edit_mark_name);
  557.  
  558.             ContentValues args_date = new ContentValues();
  559.             args_date.put("datum", date_picker_message_edit);
  560.  
  561.             ContentValues args_mark = new ContentValues();
  562.             args_mark.put("mark", txt_edit_mark_mark);
  563.  
  564.             ContentValues args_weight = new ContentValues();
  565.             args_weight.put("gewicht", txt_edit_weigh_trim);
  566.  
  567.             db.update("tbl_marks", args, "_id="+id_edit, null);
  568.             db.update("tbl_marks", args_date, "_id="+id_edit, null);
  569.             db.update("tbl_marks", args_mark, "_id="+id_edit, null);
  570.             db.update("tbl_marks", args_weight, "_id="+id_edit, null);
  571.             dismissDialog(EDIT_MARK_DIALOG);
  572.             fillData();
  573.         }
  574.     };
  575.  
  576.     private Button.OnClickListener close_popup_mark = new Button.OnClickListener(){
  577.         public void onClick(View arg0) {
  578.             dismissDialog(EDIT_MARK_DIALOG);
  579.         }
  580.     };
  581.  
  582.  
  583.     protected Dialog onCreateDialog(int id) {
  584.      Dialog dialog = null;
  585.         switch(id) {
  586.         case ADD_MARK_DIALOG:
  587.          dialog = new Dialog(mark.this);
  588.  
  589.          dialog.setContentView(R.layout.dialog_context_mark);
  590.          dialog.setTitle("Note hinzufügen");
  591.  
  592.          insert_markname = (EditText)dialog.findViewById(R.id.insert_markname);
  593.          insert_note    = (EditText)dialog.findViewById(R.id.insert_mark);
  594.          cmd_add_mark = (Button)dialog.findViewById(R.id.cmd_add_mark);
  595.          cmd_close_popup = (Button)dialog.findViewById(R.id.cmd_close_popup);
  596.          date_pick = (DatePicker)dialog.findViewById(R.id.date_maturity_mark);
  597.          seek_gewicht = (SeekBar)dialog.findViewById(R.id.seekBar_wertung);
  598.          seek_gewicht.setOnSeekBarChangeListener(yourSeekBarListener);
  599.  
  600.          gewicht = (TextView)dialog.findViewById(R.id.txt_weight_change);
  601.          cmd_add_mark.setOnClickListener(add_mark);
  602.          cmd_close_popup.setOnClickListener(close_popup);
  603.  
  604.  
  605.             break;
  606.         case EDIT_MARK_DIALOG:      
  607.             dialog = new Dialog(mark.this);
  608.  
  609.             dialog.setContentView(R.layout.dialog_context_mark_edit);
  610.             dialog.setTitle("Note bearbeiten");
  611.  
  612.             edit_mark_name = (EditText)dialog.findViewById(R.id.edit_mark_name);
  613.             edit_mark_mark = (EditText)dialog.findViewById(R.id.edit_mark_note);
  614.             cmd_save_mark = (Button)dialog.findViewById(R.id.cmd_save_mark);
  615.             cmd_close_mark = (Button)dialog.findViewById(R.id.cmd_close_mark);
  616.             date_pick_edit = (DatePicker)dialog.findViewById(R.id.date_maturity_mark_edit);
  617.             seek_gewichtt = (SeekBar)dialog.findViewById(R.id.seek_edit_gewicht);
  618.             seek_gewichtt.setOnSeekBarChangeListener(yourSeekBarListener);
  619.  
  620.             edit_mark_name.setText(name);
  621.             edit_mark_mark.setText(mark);
  622.  
  623.             seek_gewichtt.setProgress(gewicht_mark_int);
  624.  
  625.             gewicht = (TextView)dialog.findViewById(R.id.gewicht);
  626.             gewicht.setText(""+gewicht_mark);
  627.             cmd_save_mark.setOnClickListener(save_mark);
  628.             cmd_close_mark.setOnClickListener(close_popup_mark);
  629.  
  630.                break;
  631.         }
  632.  
  633.         return dialog;
  634.     }
  635.  
  636. }