我将通过 JSON 从服务器获取一些数据,并将它们存储在我的 sqlite 数据库中。
当我使用下面的代码时,在第一次时, Toast 消息显示我的表格行零。
但是当我运行 second 时间的应用程序时,它会显示当前的数字(JSON数据计数)。
此外,在第二时间,我删除了他们将数据存储到 sqlite 数据库的行并运行应用程序,以确保在第一个> strong>时间数据存储在数据库中。
请让我知道我哪里错了......
AboutActivity:
public class AboutActivity extends ActionBarActivity implements OnClickListener, OnItemClickListener {
DBHandler dbh;
private static final String TAG_IMAGES = "images";
private static final String TAG_GUID = "GUID";
private static final String TAG_IMG_LINK = "img_link";
private static final String TAG_IMG_DESC = "img_desc";
private static final String TAG_URL = "http://xxx/x.php";
private JSONArray jArray = null;
private JSONParser jParser;
private JSONObject jObject;
private ProgressDialog pDialog;
String[] GUID;
String[] imgLink;
String[] imgDesc;
String[] imgPath;
AboutSlideShowModel model;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
dbh = new DBHandler(AboutActivity.this);
getAllDataFromServer myClass = new getAllDataFromServer();
myClass.execute();
Toast.makeText(AboutActivity.this, dbh.getCount() + "", 5000).show();
}
private class getAllDataFromServer extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
pDialog = new ProgressDialog(AboutActivity.this);
pDialog.setMessage("Be patient ...");
pDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params)
{
jParser = new JSONParser();
jObject = jParser.getJSONFromUrl(TAG_URL);
try
{
jArray = jObject.getJSONArray(TAG_IMAGES);
GUID = new String[jArray.length()];
imgLink = new String[jArray.length()];
imgPath = new String[jArray.length()];
imgDesc = new String[jArray.length()];
for(int i=0; i<jArray.length(); i++)
{
JSONObject tempJson = jArray.getJSONObject(i);
GUID[i] = tempJson.getString(TAG_GUID);
imgLink[i] = tempJson.getString(TAG_IMG_LINK);
imgDesc[i] = tempJson.getString(TAG_IMG_DESC);
new SaveImageIntoStorage(AboutActivity.this, imgLink[i], GUID[i] + ".jpg", "SlideShow").saveImage();
imgPath[i] = Environment.getExternalStorageDirectory() + "/Slideshow/" + GUID[i] + ".jpg";
model = new AboutSlideShowModel(GUID[i], imgLink[i], imgPath[i], imgDesc[i]);
dbh.insertSingleAboutSlideShow(model);
}
}
catch(JSONException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
pDialog.dismiss();
super.onPostExecute(result);
}
}
}
AboutSlideShowModel:
public class AboutSlideShowModel
{
public int ID;
public String GUID;
public String IMG_LINK;
public String IMG_PATH;
public String Description;
public AboutSlideShowModel() {}
public AboutSlideShowModel(int ID, String GUID, String IMG_LINK, String IMG_PATH, String Description)
{
this.GUID = GUID;
this.IMG_LINK = IMG_LINK;
this.IMG_PATH = IMG_PATH;
this.Description = Description;
this.ID = ID;
}
public AboutSlideShowModel(String GUID, String IMG_LINK, String IMG_PATH, String Description)
{
this.GUID = GUID;
this.IMG_LINK = IMG_LINK;
this.IMG_PATH = IMG_PATH;
this.Description = Description;
}
public void setID(int ID)
{
this.ID = ID;
}
public void setGUID(String GUID)
{
this.GUID = GUID;
}
public void setIMG_LINK(String IMG_LINK)
{
this.IMG_LINK = IMG_LINK;
}
public void setIMG_PATH(String IMG_PATH)
{
this.IMG_PATH = IMG_PATH;
}
public void setDescription(String Description)
{
this.Description = Description;
}
public int getID()
{
return this.ID;
}
public String getGUID()
{
return this.GUID;
}
public String getIMG_LINK()
{
return this.IMG_LINK;
}
public String getIMG_PATH()
{
return this.IMG_PATH;
}
public String getDescription()
{
return this.Description;
}
}
DBHandler:
public class DBHandler extends SQLiteOpenHelper
{
private static final String DB_NAME = "db";
private static final int DB_VERSION = 1;
private static final String KEY_ID = "id";
private static final String KEY_GUID = "guid";
private static final String KEY_IMAGE_PATH = "image_path";
private static final String KEY_IMAGE_LINK = "image_link";
private static final String KEY_DESCRIPTION = "description";
private static final String TBL_ABOUT_SLIDESHOW = "tbl_about_slideshow";
private static final String CREATE_TBL_ABOUT_SLIDESHOW = "CREATE TABLE IF NOT EXISTS "
+ TBL_ABOUT_SLIDESHOW + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_GUID + " TEXT,"
+ KEY_IMAGE_LINK + " TEXT,"
+ KEY_IMAGE_PATH + " TEXT,"
+ KEY_DESCRIPTION + " TEXT" + ")";
public DBHandler(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_TBL_ABOUT_SLIDESHOW);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TBL_ABOUT_SLIDESHOW);
onCreate(db);
}
public void insertSingleAboutSlideShow(AboutSlideShowModel model)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_GUID, model.getGUID());
values.put(KEY_IMAGE_LINK, model.getIMG_LINK());
values.put(KEY_IMAGE_PATH, model.getIMG_PATH());
values.put(KEY_DESCRIPTION, model.getDescription());
// Inserting Row
db.insert(TBL_ABOUT_SLIDESHOW, null, values);
db.close(); // Closing database connection
}
public int getCount()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + TBL_ABOUT_SLIDESHOW, null);
if(c.getCount() <= 0)
return 0;
return c.getCount();
}
}
答案 0 :(得分:1)
您的任务正在异步执行,因此当您执行toast
时,AsyncTask
尚未检索到数据。如果您使用onPostExecute
方法检查数据库,则可能会看到您要查找的结果。