Recommended Way/Order to Read Data from a Webservice, Parse That Data and Insert It in a SQLite Db

Saving to sqlite using insert or update

Use execSQL() and not rawQuery() for raw SQL that modifies the database.

rawQuery() alone won't execute the SQL; you'd need to call one of the moveTo...() methods on the returned Cursor to execute it.

SQLite Database query for multiple table

You have to change the table name to Quiz2, etc.
This requires that the function needs to know the quiz number:

public List<Question> getQuestionSet(int quizNr, int numQ) {
...
rawQuery("SELECT * FROM Quiz" + quizNr + " ORDER BY random() LIMIT " + numQ, null);
...
}

If you had a single table, you would have to filter on the corresponding column:

public List<Question> getQuestionSet(int quizNr, int numQ) {
...
rawQuery("SELECT * FROM Quiz WHERE QuizNr = " + quizNr + " ORDER BY random() LIMIT " + numQ, null);
...
}

Creating a SQLite table row updating row again and again

A scenario:

  1. add a new record with name:"name1", telephone: "123456789" --> new record
  2. add a new record with name:"name2", telephone:"987654321" --> update the previously entered record.

If that what you want then:

  1. be sure to always insert the new record with the same id as the previously inserted one.
  2. use db.insertWithOnConflict()[ link ] to insert new records, passing the value CONFLICT_REPLACE [ link ] for the last parameter conflictAlgorithm

Sample Code

void Add_Contact(Person_Contact contact) 
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
// SINGLE_ROW_ID is a constant holding the single row id that will be used. e.g: SINGLE_ROW_ID = 1
values.put( KEY_ID, SINGLE_ROW_ID );
values.put( KEY_NAME, contact.get_name() ); // Contact Name
values.put( KEY_PH_NO, contact.get_phone_number()); // Contact Phone
// Inserting Row
db.insert( TABLE_CONTACTS, null, values );
db.insertWithOnConflict( TABLE_CONTACTS,KEY_ID,values, SQLiteDatabase.CONFLICT_REPLACE );
db.close(); // Closing database connection
}

Using String[] selectionArgs in SQLiteDatabase.query()

selectionArgs replace any question marks in the selection string.

for example:

String[] args = { "first string", "second@string.com" };
Cursor cursor = db.query("TABLE_NAME", null, "name=? AND email=?", args, null);

as for your question - you can use null

Best way to start an application efficiently

Good question !
I'll answer with a list of awesome libs.
First you should definively have a look at AndroidAnnotations
This will really simplify your work. Don't be afraid of perfs on Annotations. it's not reflexion but a compilation step. It really eases the use of shared preferences.
For http stuff , spring is pretty good and well integrated with android annotations.
I never used xml in WS, But for json mapping , i use Jackson and it's pretty fast and flexible. I think gson does the job well too.

For image loading, UIL is the most efficient and the most mature ( so, it was the case in July when i benchmarked, Picasso and Volley too).

For now i have not found the ORM saint graal, but my advice is "Does you really need a database ?" if you got a doubt backoff. if you need it, ormlite has some perfs problems. In my next project ,I'll give a try to greenDao.

Definitively use a CursorAdapter for ListView+ SQLite , instead of loading in Listview. But be sure to not access the database intensively at the same time (WS writes).

If you need to pass events like "Hey new data is available in database refresh", don't make boilerplate interfaces. Use an event bus this is awesome EventBus

How to Send All Data From SQLite to Server in Android

This is a simple problem. You are only using
use for loop to iterate through all items like,

for(int i=0; i<datamodel.size(); i++){
doNormalPostOperation(i)
}

and in doNormalPostOperation method

public void doNormalPostOperation(int i) {

// PREVIOUS CODE
@Override
protected Map<String, String> getParams() {
params.put("id", datamodel.get(i).getINSURANCE_ID() + "");

// REMAINING PARAMS WITH SAME datamodel.get(i) item

}
}

which is causing that issue and only sending first item. You will need to iterate through all items to generate parameters and send to server, or else you can combine them to one json array and modify the code server side.

How to bind validation error message from server to corresponding `EditText` on layout?

In my humble opinion you are complicating yourself work and waste time on this issue. If you have exact number of EditTexts corresponding to JSON fields then there is no point to figure out some sophisticated implementation. At the end it could make your code event more difficult to read.

You have to keep references to all your EditText views anyway, right? Alternative with HashMap does not give you any benefit. The only thing you could do is creating method for checking EditText and error value.

private void setEditTextAndValidate(EditText editText, String errorMsg) {
if(!TextUtils.isEmpty(errorMsg)){
editText.setError(errorMsg);
editText.requestFocus();
}
}

and then call

setEditTextAndValidate(mEditTextUserName, validationResponse.getUserNameError());
setEditTextAndValidate(mEditTextPassword, validationResponse.getPasswordError());

Once again, it's just my opinion that I would like to share. Maybe there are frameworks that will do it for you, but is it worth to you, I doubt.

What format does PrintDocument.getData() return?

It is always PDF for CONTENT_TYPE_DOCUMENT.

AsyncTask not calling onProgressUpdate,onPostExecute

The method signature is wrong. It should look like:

protected void onPostExecute(Void result) {
Log.i("Thread","onPosts");
}

also, I suggest you to use the @Override annotation. This way you will get a compile time error if you are trying to overriding a wrong method.

Edit:
The signature of onProgressUpdate you posted is wrong. It should be:

protected void onProgressUpdate(Integer... progress){}


Related Topics



Leave a reply



Submit