我正在为android开发一个应用程序,它通过php脚本连接外部MySQL数据库来获取数据。问题是数据量太大,我在尝试加载数据时遇到了outofmemoryexception。我使用分页方法一次显示10个项目 - 当用户滚动到listview的底部时,接下来显示10个项目。这里的问题是加载速度很慢。我需要让应用程序响应。我应该使用什么方法?我使用的代码如下 -
public class ItemActivity extends Activity {
private String str = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode
// is
// most
// commonly
// used
// to
// catch
// accidental
// disk
// or
// network
// access
// on
// the
// application's
// main
// thread
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
setTitle("Kurd Shopping");
final int id=getIntent().getExtras().getInt("id");
try{
ArrayList<NameValuePair> postParameters=new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("id",String.valueOf(id)));
postParameters.add(new BasicNameValuePair("startid", "0"));
str=CustomHttpClient.executeHttpPost("http://kurdshopping.net/apj/adlist.php", postParameters);
JSONArray array=new JSONArray(str);
final Item[] item_data=new Item[array.length()];
for(int i=0;i<item_data.length;i++){
JSONObject jdata=array.getJSONObject(i);
String path="http://kurdshopping.net/thumbs/"+jdata.getString("name");
item_data[i]=new Item(jdata.getInt("id"), path, jdata.getString("title")+"\nPrice: "+jdata.getString("price"));
}
final ItemAdapter adapter=new ItemAdapter(this, R.layout.listview_item_row, item_data);
final ListView listView=(ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Toast.makeText(ItemActivity.this, item_data[position].id, Toast.LENGTH_SHORT).show();
}
});
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastVisibleitem=firstVisibleItem+visibleItemCount;
if(lastVisibleitem==totalItemCount){
ArrayList<NameValuePair> postParameters=new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("id",String.valueOf(id)));
String s=String.valueOf(item_data[lastVisibleitem-1].id);
postParameters.add(new BasicNameValuePair("startid", s));
try {
str=CustomHttpClient.executeHttpPost("http://kurdshopping.net/apj/adlist.php", postParameters);
JSONArray array=new JSONArray(str);
for(int i=0;i<item_data.length;i++){
JSONObject jdata=array.getJSONObject(i);
String path="http://kurdshopping.net/thumbs/"+jdata.getString("name");
item_data[i]=new Item(jdata.getInt("id"), path, jdata.getString("title")+"\nPrice: "+jdata.getString("price"));
}
adapter.notifyDataSetChanged();
listView.setSelection(0);
}catch(JSONException j){
Log.e("ItemActivity", j.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(lastVisibleitem==totalItemCount){
}
}
});
}catch(NotFoundException n){
Log.e("ItemActivity", n.getMessage());
}catch(JSONException j){
Log.e("ItemActivity", j.getMessage());
}catch (Exception e) {
Log.e("ItemActivity", e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.item, menu);
return true;
}
}
任何帮助或建议将不胜感激。提前谢谢。