我的应用程序应该从网站获取一些信息(JSON格式),解析它,将其存储在SQLite数据库中,然后在列表视图中显示项目列表(从数据库中获取)。除了填充listview之外,它会执行所有操作(使用Log.v消息进行检查)。但如果我第二次运行它,则会显示列表视图。
在MainActivity中,我使用AsyncTask在解析数据后获取数据。然后我开始另一个活动来从表中读取数据(所有行)并在ListView中显示它们。
public class MainActivity extends Activity {
private MySQLiteHelper dbHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://strong-earth-32.heroku.com/stores.aspx";
dbHelper = new MySQLiteHelper(this);
db = dbHelper.getWritableDatabase();
ConnectAsyncTask connect = new ConnectAsyncTask(url);
connect.execute();
try {
connect.wait(2000);
} catch (Exception e) {
Log.v("Waiting. ",e.getStackTrace().toString());
}
Intent intent=new Intent(MainActivity.this,DisplayActivity.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void cacheData(String result) {
ContentValues cv = new ContentValues();
try {
//Tranform the string into a json object
final JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("stores");
for(int i = 0; i < jArray.length(); i++) {
try{
JSONObject oneObject = jArray.getJSONObject(i);
String logoURL = oneObject.getString("storeLogoURL");
String address = oneObject.getString("address");
cv.put(MySQLiteHelper.COLUMN_ADDRESS, address);
String phoneNr = oneObject.getString("phone");
cv.put(MySQLiteHelper.COLUMN_PHONE, phoneNr);
long row = db.insert(MySQLiteHelper.TABLE_STORES, MySQLiteHelper.COLUMN_ID, cv);
//Log.v("row= ", row + "");
cv.clear();
} catch (JSONException e) {
Log.v("Oops! ",e.getStackTrace().toString());
}
}
} catch (JSONException e) {
Log.v("ERR",e.getStackTrace().toString());
}
}
private class ConnectAsyncTask extends AsyncTask<Void, Void, String>{
private ProgressDialog progressDialog;
String url;
ConnectAsyncTask(String urlPass){
url = urlPass;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Fetching data, Please wait...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
JSONparser jParser = new JSONparser();
String json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.hide();
progressDialog.dismiss();
if (result!=null) {
Log.v("READY","READY");
cacheData(result);
}
}
}
}
这是另一项活动:
public class DisplayActivity extends Activity {
private MySQLiteHelper dbHelper;
private SQLiteDatabase db;
private ListView lv_custom;
ArrayList<Content> contents = new ArrayList<Content>();
Content content0 = new Content();
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
lv_custom = (ListView) findViewById(android.R.id.list);
dbHelper = new MySQLiteHelper(this);
db = dbHelper.getWritableDatabase();
readData(db);
lv_custom.setAdapter(new CustomListviewAdapter(this, R.layout.one_row, contents));
}
private class Content {
private String logoURL;
private String phoneNr;
private String address;
}
private class ViewHolder{
WebView webView;
TextView textViewPhone;
TextView textViewAddress;
}
private class CustomListviewAdapter extends BaseAdapter{
private int layoutResource;
private ArrayList<Content> mArrayList;
private LayoutInflater inflater;
public CustomListviewAdapter(Context mContext, int layoutResource, ArrayList<Content> mArrayList) {
this.layoutResource = layoutResource;
this.mArrayList = mArrayList;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mArrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mArrayList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
convertView = inflater.inflate(layoutResource, null);
viewHolder = new ViewHolder();
//WebView web = (WebView) findViewById(R.id.webView1);
//web.loadUrl("Your Url");
//viewHolder.webView = (WebView) findViewById(R.id.webView1);
viewHolder.textViewPhone = (TextView) convertView.findViewById(R.id.phoneNr);
viewHolder.textViewAddress = (TextView) convertView.findViewById(R.id.address);
//viewHolder.webView.loadUrl(mArrayList.get(position).logoURL);
viewHolder.textViewPhone.setText(mArrayList.get(position).phoneNr);
viewHolder.textViewAddress.setText(mArrayList.get(position).address);
//int itemHeight = height/4 - 10;
//viewHolder.webView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, itemHeight));
return convertView;
}
}
public void readData(SQLiteDatabase db) {
String[] selectionArguments = {};
c = db.rawQuery("SELECT * FROM storesList", selectionArguments);
if (c.getCount() != 0) {
try {
while (c.moveToNext()) {
//add a new store information to the ArrayList
content0 = new Content();
//content0.logoURL = logoURL;
content0.phoneNr = c.getString(2);
content0.address = c.getString(1);
contents.add(content0);
Log.v("address:", content0.address);
}
} catch (Exception e) {
Log.v("Exception e ",e.fillInStackTrace().toString());
}}
else Log.v("Cursor ", 0+"");
}
}
为什么我没有看到第一次执行应用程序的列表?我想这与在AsyncTask完成之前开始的第二个活动有关。但是,我没有解决问题的方法。此外,我没有看到ProgressDialog。任何的想法?
答案 0 :(得分:3)
为什么我没有看到第一次执行该应用程序的列表?
因为您在调用execute
AsyncTask
方法后正在启动另一个活动。要在第二个活动中显示数据,您需要在cacheData
方法中的数据库中插入数据后启动第二个Activity:
public void cacheData(String result) {
//your code for inserting data in db
....
//start second Activity here
Intent intent=new Intent(MainActivity.this,DisplayActivity.class);
startActivity(intent);
}
答案 1 :(得分:3)
尝试在MainActivity.java的onPostExecute
内调用启动第二个活动。
从MainActivity.java的onCreate()
中删除以下代码
Intent intent=new Intent(MainActivity.this,DisplayActivity.class);
startActivity(intent);
并替换onPostExecute
,如下所示
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.hide();
progressDialog.dismiss();
if (result!=null) {
Log.v("READY","READY");
cacheData(result);
Intent intent=new Intent(MainActivity.this,DisplayActivity.class);
startActivity(intent);
}
}