mardi 22 décembre 2015

SQLite data get deleted instead of update

What's wrong with my code here? In Edit activity, there has a listView where it has been populated by data from SQLite . When the list is clicked, it will intent to EditInformation for edit.

If save button in EditInformation is clicked, it will return the updated value back to listView Edit. The problem now is when the save button get clicked, nothing was displayed in Edit activity. The data seems get deleted.

Edit

 Calendar cal;
 int month;
 BluetoothAdapter myDevice;
 String deviceName ;

  cal = Calendar.getInstance(); // get current month
  month = cal.get(Calendar.MONTH)+1;
  myDevice = BluetoothAdapter.getDefaultAdapter(); // get device name
  deviceName = myDevice.getName();
  BuildEditList(deviceName,month);



 listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() { // if list is clicked
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding listview_item_row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this listview_item_row in the database.
                ID =
                        cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                Intent intent = new Intent(getApplicationContext(), ActivityB.class);
                intent.putExtra("ID", ID);
                startActivity(intent);


            }
        });

  public void BuildEditList(String name,int month)
    {
        sqlcon.open();
        Toast.makeText(getApplicationContext(), month + "", Toast.LENGTH_LONG).show();
        Cursor cursor1=sqlcon.readData(name,month);
        String[] columns=new String[]{
                MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
        };

        int[] to=new int[]
                {
                        R.id.weather, R.id.date,R.id.status,R.id.in,R.id.out
                };

        dataAdapter = new SimpleCursorAdapter(this, R.layout.retrieve_edit,
                cursor1,
                columns,
                to,
                0);

        listViewEdit.setAdapter(dataAdapter);
    }

EditInformation

Bundle bundle = this.getArguments();
        if(getArguments()!=null)  // receive ID from Edit
        {
            ID=bundle.getLong("ID");
        }
        RetrieveInformation(ID);

        save.setOnClickListener(new View.OnClickListener() {     // if save button clicked
            @Override
            public void onClick(View v) {

                Weather11=Weather.getSelectedItem().toString();
                Status11=Status.getSelectedItem().toString();
                sqlcon.open();
                sqlcon.Update(ID, Name11, Weather11, date2, Status11);
               Intent i= new Intent(getActivity(),Edit.class);
                startActivity(i);
            }
        });

        return edit_info;

    }

 public void RetrieveInformation(long id1) 
    {
        final long id=id1;
        database=dbHelper.getWritableDatabase();
        cursor=database.rawQuery("SELECT Name, Weather, Date, Status FROM "+ MyDatabaseHelper.TABLE_INFO + " WHERE _id= ? ",
                new String[]{String.valueOf(id)},null);
        Info I=new Info();
        if(cursor!=null) {
            while (cursor.moveToNext())
            {
                Name11 =cursor.getString(cursor.getColumnIndexOrThrow(MyDatabaseHelper.Name));
                I.setName(Name11);
                Name.setText(Name11);
                 Weather11 = cursor.getString(cursor.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
                RetrieveWeather(Weather11);
                I.setWeather(Weather11);
                Status11=cursor.getString(cursor.getColumnIndexOrThrow(MyDatabaseHelper.Status));
                RetrieveStatus(Status11);
                I.setStatus(Status11);
                Date11=cursor.getString(cursor.getColumnIndexOrThrow(MyDatabaseHelper.Date));
                I.setDate(Date11);
                Date.setText(Date11);

            }
        }


    }

InfoAPI

 public void Update(long id,String name,String weather, String date, String status )
    {
        ContentValues cv=new ContentValues();
        cv.put(MyDatabaseHelper.Name,name);
        cv.put(MyDatabaseHelper.Weather, weather);
        cv.put(MyDatabaseHelper.Date, date);
        cv.put(MyDatabaseHelper.Status, status);
        database.update(MyDatabaseHelper.TABLE_INFO, cv, MyDatabaseHelper.ID + "=" + id,null);
    }

The issue now is when the save button in EditInformation is clicked, no data shown in Edit, which suppose to show the updated value in listView.

Aucun commentaire:

Enregistrer un commentaire