2

I am parsing JSON data to database,that data shows in my lisview.But in the first time it get data from my JSON and stores in db,its not upgrading if i add anything in my website,the json also will increase but its not reflect in db,the database part also not upgrading.Just its simply the display the first fetched data only.

Ginfydbadapter.java

public class GinfyDbAdapter {

    private static final String DATABASE_NAME = "test";
    private static final String DATABASE_TABLE_PROJ = "projects";
    private static final int DATABASE_VERSION = 3;
    public static final String CATEGORY_COLUMN_ID = "_id";
    public static final String CATEGORY_COLUMN_TITLE = "title";
    public static final String CATEGORY_COLUMN_CONTENT = "content";
    public static final String CATEGORY_COLUMN_COUNT = "count";


    private static final String TAG = "GinfyDbAdapter";
    private DatabaseHelper mDbHelper;
    private static SQLiteDatabase mDb;
    private final Context mCtx;





    public void saveCategoryRecord(String id, String title, String content, String count) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(CATEGORY_COLUMN_ID, id);
        contentValues.put(CATEGORY_COLUMN_TITLE, title);
        contentValues.put(CATEGORY_COLUMN_CONTENT, content);
        contentValues.put(CATEGORY_COLUMN_COUNT, count);
        mDb.insert(DATABASE_NAME, null, contentValues);
        }
    public Cursor getTimeRecordList() {
        return mDb.rawQuery("select * from " + DATABASE_NAME, null);
        }
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }



        private static final String DATABASE_CREATE_PROJ =
                "create table " + DATABASE_TABLE_PROJ + " ("
                + CATEGORY_COLUMN_ID + " integer primary key , "
                + CATEGORY_COLUMN_TITLE + " text not null, " + CATEGORY_COLUMN_CONTENT + " text not null, " + CATEGORY_COLUMN_COUNT + " integer );" ;

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String DATABASE_CREATE_PROJ = "CREATE TABLE " +  DATABASE_TABLE_PROJ + "( "
                + CATEGORY_COLUMN_ID + " integer primary key, "
                + CATEGORY_COLUMN_TITLE + " text not null, " + CATEGORY_COLUMN_CONTENT + " text not null, " + CATEGORY_COLUMN_COUNT + " integer   );" ;
                db.execSQL(DATABASE_CREATE_PROJ);     
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS"+ DATABASE_TABLE_PROJ);
        onCreate(db);
    }


}

    public void saveCategoryRecord(Category category) {

         ContentValues values = new ContentValues();
         values.put(CATEGORY_COLUMN_TITLE , category.getTitle());
         values.put(CATEGORY_COLUMN_CONTENT, category.getContent()); 
         values.put(CATEGORY_COLUMN_COUNT, category.getCount());   
         // Inserting Row
         mDb.insert(DATABASE_TABLE_PROJ, null, values);
         mDb.close(); // Closing database connection
    }

    public Cursor fetchAllProjects() {
        // TODO Auto-generated method stub
        return mDb.query(DATABASE_TABLE_PROJ, new String[] {CATEGORY_COLUMN_ID, CATEGORY_COLUMN_TITLE, CATEGORY_COLUMN_CONTENT, CATEGORY_COLUMN_COUNT }, null, null, null, null, null);
    }

    public GinfyDbAdapter(Context ctx) {
        this.mCtx = ctx;

    }

     public GinfyDbAdapter open() throws SQLException {
            mDbHelper = new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }


}

this is my MainActivity.java here only i mention my url of json also

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item); 

        mDbHelper=new GinfyDbAdapter(MainActivity.this);
        mDbHelper.open();
         Cursor projectsCursor = mDbHelper.fetchAllProjects();
         if(projectsCursor!=null)
           {
           fillData(projectsCursor);
           Log.i("filling", "...");
           }
           else
           {
                new GetDataAsyncTask().execute();
           }


        //lv1 =(ListView)findViewById(R.id.list);   
        //lv =(ListView)findViewById(R.id.list);



        btnGetSelected = (Button) findViewById(R.id.btnget);
        btnGetSelected.setOnClickListener(this);

        myFilter = (EditText) findViewById(R.id.myFilter);



        //praycount.setOnClickListener(this);
        //initView();
    }



    /*private void initView(){
        // show progress dialog
        dialog = ProgressDialog.show(this, "", "Loading...");
        String url = "http://www.ginfy.com/api/v1/posts.json";
        FetchDataTask task = new FetchDataTask(this);
        task.execute(url);


    }   */



    private class GetDataAsyncTask extends AsyncTask<Void, Void, Void> {
        private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);

        protected void onPreExecute() {
            Dialog.setMessage("Loading.....");
            Dialog.show();
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Dialog.dismiss();
            Cursor projectsCursor = mDbHelper.fetchAllProjects();
            if(projectsCursor!=null)
            {
            mDbHelper=new GinfyDbAdapter(MainActivity.this);
            mDbHelper.open();
            fillData(projectsCursor);
            }
        }
        @Override
        protected Void doInBackground(Void... params) {
            getData();
            return null;
        }
    }

    public void getData() {
          try
          {
      HttpClient httpclient = new DefaultHttpClient();
      httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
      HttpGet request = new HttpGet("https://ancient-caverns-4909.herokuapp.com/api/v1/posts.json");
      // HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/api/users/mbbangalore/uploads?v=2&alt=jsonc");     

      HttpResponse response = httpclient.execute(request);
      HttpEntity resEntity = response.getEntity();
      String _response=EntityUtils.toString(resEntity); // content will be consume only once
       Log.i("................",_response);
      httpclient.getConnectionManager().shutdown();
      JSONObject jsonObject = new JSONObject(_response);
      JSONArray contacts = jsonObject.getJSONArray("post");//(url);
          for(int i = 0; i < contacts.length(); i++){
              JSONObject c = contacts.getJSONObject(i);
              String id = c.getString("id");
              String title = c.getString("title");
              String  content = c.getString("content");
              String  count = c.getString("count");
              mDbHelper=new GinfyDbAdapter(MainActivity.this);
              mDbHelper.open();
              mDbHelper.saveCategoryRecord(new Category(id,title,content,count));
      }
  } catch (Exception e) {
      e.printStackTrace();
  }
    }

    @SuppressLint("NewApi")
     @SuppressWarnings("deprecation")
       private void fillData(Cursor projectsCursor) {
           //mDbHelper.open();   

           if(projectsCursor!=null)
           {
           String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE, GinfyDbAdapter.CATEGORY_COLUMN_CONTENT, GinfyDbAdapter.CATEGORY_COLUMN_COUNT};
           int[] to = new int[]{R.id.text2, R.id.text1, R.id.count};
            dataAdapter  = new SimpleCursorAdapter(
             this, R.layout.activity_row, 
             projectsCursor, 
             from, 
             to,
             0);
            setListAdapter(dataAdapter);
           }else
           {
               Log.i("...........","null");
           }
       }

My Database is not upgrading due to JSON change,it will take only one time of json data only.

Karthick M
  • 769
  • 2
  • 11
  • 29
  • everytime you get the data do you add it to your database? `mDbHelper.saveCategoryRecord(new Category(id,title,content,count))` if you get all data then it should be in db . if its db then you can retrieve it and show. check the db first for the data entries – Raghunandan Jul 09 '13 at 13:20
  • @Raghunandan no for first time it shows next time its not upgrading dude,it simply display the same data in listview, if possible check my code i sent mail to you – Karthick M Jul 09 '13 at 13:24
  • you mean to say the data in entered int the data base but the update is not getting displayed? – Raghunandan Jul 09 '13 at 13:25
  • dude thats only in first time it shows in listview from next time itself my listview is not changing @Raghunandan,pls check your mail dude – Karthick M Jul 09 '13 at 13:26
  • i mean to say the new entry (updated data) is it present in the database? – Raghunandan Jul 09 '13 at 13:26
  • how to see that one dude@Raghunandan,i can see only the data of json in the url of rest client,but in db dont know how to see @rRaghunandan – Karthick M Jul 09 '13 at 13:28
  • you can extract the db from file explorer under your package name and then view it with any db viewer suitable fro your os. – Raghunandan Jul 09 '13 at 13:31
  • @Raghunandan check your mail,in file explorer not like that is available – Karthick M Jul 09 '13 at 13:32
  • @Raghunandan are you there – Karthick M Jul 10 '13 at 11:28
  • what's happening now? – Raghunandan Jul 10 '13 at 11:35
  • @Raghunandan http://stackoverflow.com/questions/17570269/how-to-update-the-database-while-changing-in-our-server-side – Karthick M Jul 10 '13 at 12:10

0 Answers0