【发布时间】:2013-11-29 04:23:52
【问题描述】:
我的应用程序应该从网站(JSON 格式)获取一些信息,对其进行解析,将其存储在 SQLite 数据库中,然后在列表视图中显示项目列表(从数据库中获取)。除了填充列表视图之外,它执行所有操作(使用 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。任何想法?
【问题讨论】:
-
你调试getView是否第一次调用?
-
你是对的,它没有第一次调用getView。
标签: android json sqlite listview android-asynctask