mardi 28 avril 2015

Cursor always returns empty

in the code given below ,when i run the app the data is getting written to the table but when i fetch the data it always gives me an empty cursor.Any help will be much appreciated

public class todo extends Activity {


        private CheckBox chkIos;
        EditText txtphoneNo;
        String isc="n";
        String phoneNo="000";
        String name="000";
    Context mcontect =this;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_todo);
             //declaration
        chkIos = (CheckBox) findViewById(R.id.checkBox);

        txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
            //get_user_name
            SharedPreferences pref = getSharedPreferences("lin", 0);
            name = pref.getString("KEY", null);
            //database_3
        DatabaseOperations3 DO  = new DatabaseOperations3(mcontect);
        Cursor CR = DO.getInformation(DO);
        Log.d("=====>", "todo");
        if (CR.moveToFirst()) {
            Log.d("=====>", "todo");

            do {
                if (name.equals(CR.getString(0))) {
                    Log.d(name, CR.getString(0));

                    phoneNo = (CR.getString(1));
                    Log.d(name, CR.getString(0));
                    Log.d(phoneNo, CR.getString(1));
                    Log.d(isc, CR.getString(2));
                    isc = (CR.getString(2));
                    txtphoneNo.setText(phoneNo);
                }
            } while (CR.moveToNext());
            check();
        }}
public void check() {
   String s="c";
    if(isc.equals(s)){
        Log.d("=====>", "check");

        chkIos.setChecked(true);

    }
}
    public void cbonclick(View v) {

        if (chkIos.isChecked()) {

        isc = "c";
        }
}
    public void xxx(View v) {
        phoneNo = txtphoneNo.getText().toString();
        DatabaseOperations3 DO  = new DatabaseOperations3(mcontect);

        DO.putInformation(DO,name,phoneNo,isc);
        Log.d("=====>", "Database_3_7");
        Intent intenti = new Intent(this, ProxAlertActivity.class);
        startActivity(intenti);
    }


            protected void sendSMSMessage() {
            Log.d("Send SMS", "************");

            String message = "Reached";
                Log.d("=====>", "message");

              if (isc.equals("c")&&phoneNo!="000") {
                Log.d("=====>", "eqto");

                try {
                    Log.d("=====>", "try");

                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNo, null, message, null, null);
                    Toast.makeText(getApplicationContext(), "SMS sent.",
                            Toast.LENGTH_LONG).show();

                } catch (Exception e) {
                    Log.d("=====>", "excep");

                    Toast.makeText(getApplicationContext(),
                            "SMS faild, please try again.",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        }
    }

The Database of my app is given below

public class DatabaseOperations3 extends SQLiteOpenHelper {
     static final int database_version = 1;
     static final String USER_NAME = "user_name";
     static final String USER_PHONE = "user_ph";
     static final String USER_SELECT = "user_select";
     static final String DATABSE_NAME = "phone_info.db";
     static final String TABLE_NAME = "ph_info";
    public DatabaseOperations3(Context context) {
        super(context, DATABSE_NAME, null, database_version);
        Log.d("Database Operations","Database Created");
    }

@Override
    public void onCreate(SQLiteDatabase sdb) {
        Log.d("Database Operations3","Table Created");
        sdb.execSQL("CREATE TABLE " + TABLE_NAME +
                "( "  + USER_NAME + " text, " +
                USER_PHONE + " text, " + USER_SELECT + " text)");

    }

       public void onUpgrade(SQLiteDatabase sq,int arg1,int arg2) {
        Log.d("DatabaseHelper", "onUpgrade");
        sq.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(sq);

    }
    public void putInformation(DatabaseOperations3 dop,String name,String ph,String isc)
    {
        SQLiteDatabase SQ = dop.getWritableDatabase();
        SQ.rawQuery("DELETE FROM " + TABLE_NAME + " WHERE " + USER_NAME + " = ?", new String[]{name});
        ContentValues cv = new ContentValues();
        cv.put(USER_NAME,name);
        cv.put(USER_PHONE,ph);
        cv.put(USER_SELECT,isc);
        Log.d(name,ph);

    }
    public Cursor getInformation(DatabaseOperations3 dop)
    {
        Log.d("DatabaseHelper", "getinfo");

        SQLiteDatabase SQ = dop.getReadableDatabase();
        String[] columns = {USER_NAME, USER_PHONE,USER_SELECT};
        Cursor CR = SQ.query(TABLE_NAME,columns,null, null, null, null, null);
        return CR;
 }
}

Aucun commentaire:

Enregistrer un commentaire