Android 在DoinBackground中加载SMS速度太慢
嗨,我正在开发一个android应用程序,我正在尝试从内置的短信应用程序加载短信。加载速度太慢,几乎需要10-15秒才能加载所有短信。我是老丁,在发短信 背景如下Android 在DoinBackground中加载SMS速度太慢,android,android-asynctask,progressdialog,android-contentresolver,Android,Android Asynctask,Progressdialog,Android Contentresolver,嗨,我正在开发一个android应用程序,我正在尝试从内置的短信应用程序加载短信。加载速度太慢,几乎需要10-15秒才能加载所有短信。我是老丁,在发短信 背景如下 private class MyBackgroundTask extends AsyncTask<Context, Integer, Boolean> { @Override protected void onPreExecute() { pDialog = new Progres
private class MyBackgroundTask extends AsyncTask<Context, Integer, Boolean>
{
@Override
protected void onPreExecute()
{
pDialog = new ProgressDialog(Myapp.this);
pDialog.setMessage("Loading ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Boolean doInBackground(Context... params)
{
//fetching values from built-in message
getSMS();
return true;
}
@Override
public void onPostExecute(Boolean success)
{
pDialog.dismiss();
runOnUiThread(new Runnable()
{
public void run()
{
adapter = new SmsAdapter(EaseSms.this, listOfmessages);
smslist.setAdapter(adapter);
}
});
}
}
是否可以在PreExecute上加载大约10条短信,并在doInBackground中继续加载其他短信?我试过类似的方法。但它不起作用。有谁能指导我解决这个缓慢加载的问题吗
请帮忙
谢谢 尝试使用游标适配器,而不是先读取所有短信,然后再显示它们。游标适配器根据需要加载数据,或者您可以说是按需加载,这将解决您的问题 进一步阐述
public class SimpleCursorAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
/**This is your constructor, Just pass it the cursor which you got from querying
* SMS database
*
* @param context
* @param c
*/
public SimpleCursorAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
/**
* Write here code for binding you view to data form cursor
*
* Just like any other adapter, fetch all the columns details you want
* and set those values properly
*/
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
/**
* Just inflate your child view layout and return it to system
*/
View v = mLayoutInflater.inflate(R.layout.<YOUR_VIEW_NAME>, parent, false);
return v;
}
}
公共类SimpleCursorAdapter扩展了CursorAdapter{
私人停车场平面布置;
私有上下文;
/**这是您的构造函数,只需将查询得到的游标传递给它
*短信数据库
*
*@param上下文
*@param c
*/
公共SimpleCursorAdapter(上下文,游标c){
超级(上下文,c);
mContext=上下文;
mLayoutInflater=LayoutInflater.from(上下文);
}
@凌驾
公共void bindView(视图、上下文上下文、光标){
/**
*在此处编写将视图绑定到数据表单游标的代码
*
*就像其他适配器一样,获取所需的所有列详细信息
*并正确设置这些值
*/
}
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
/**
*只需膨胀子视图布局并将其返回到系统
*/
视图v=MLAYOUTINGER.充气(右布局,主视图,假);
返回v;
}
}
就是这样,一旦你完成了上面提到的事情,从你的活动中得到一个光标
从sms db中,创建此适配器的一个实例,就像任何其他适配器一样,将其设置为列表视图 对不起,我正在休假,无法回复您。你能用游标适配器加载SMS引导我浏览任何链接或项目吗?它非常简单,易于实现,就像其他适配器一样,只是数据源被更改为数据库。您可以查看以下链接,了解有关使用它们的mre信息。好啊试试看。如果你有任何样本项目或短信游标适配器的链接请分享
public class SimpleCursorAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
/**This is your constructor, Just pass it the cursor which you got from querying
* SMS database
*
* @param context
* @param c
*/
public SimpleCursorAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
/**
* Write here code for binding you view to data form cursor
*
* Just like any other adapter, fetch all the columns details you want
* and set those values properly
*/
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
/**
* Just inflate your child view layout and return it to system
*/
View v = mLayoutInflater.inflate(R.layout.<YOUR_VIEW_NAME>, parent, false);
return v;
}
}