Android CursorLoader,ContentProvider太慢了
我有一个计数器应用程序,我使用过Android CursorLoader,ContentProvider太慢了,android,android-contentprovider,simplecursoradapter,android-cursorloader,Android,Android Contentprovider,Simplecursoradapter,Android Cursorloader,我有一个计数器应用程序,我使用过SimpleCursorAdapter,ListView和StartManagingCursor()。现在我想使用新的CursorLoader和Contentprovider。我编写了自己的ContentProvider,并在MainActivity上实现了它。正如你们可以看到我的应用程序上面,当我点击加号按钮和更新计数,它是滞后的。我大约每秒点击2次,但它不能处理得很快。我使用ContentResolver进行更新 以前的方法是快速启动管理游标 代码在这里,如果
SimpleCursorAdapter
,ListView
和StartManagingCursor()
。现在我想使用新的CursorLoader
和Contentprovider
。我编写了自己的ContentProvider
,并在MainActivity
上实现了它。正如你们可以看到我的应用程序上面,当我点击加号按钮和更新计数,它是滞后的。我大约每秒点击2次,但它不能处理得很快。我使用ContentResolver
进行更新
以前的方法是快速启动管理游标
代码在这里,如果我在ContentProvider
或CursorLoader
上有任何错误,请帮助我
MAIN.java
public class Main extends ActionBarActivity implements LoaderCallbacks<Cursor> {
Cursor cursor;
String[] FROM = { Database.C_NAME, Database.C_VALUE };// get values from db
int[] TO = { R.id.textMainName, R.id.textMainValue };// set values of item
SimpleCursorAdapter adapter;
boolean clickP;
boolean clickN;
boolean clickR;
String Lng;
private static final int URL_LOADER = 0;
private ListView mListView;
private ChannelAdapter mAdapter ;
ContentResolver cr ;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.list);
mAdapter =new ChannelAdapter(this, R.layout.list_item,null, FROM, TO,0);
mListView.setAdapter(mAdapter);
getSupportLoaderManager().initLoader(URL_LOADER, null, this);
registerForContextMenu(mListView);
}
@Override
protected void onResume() {
getSupportLoaderManager().restartLoader(0, null, this); }
public class ChannelAdapter extends SimpleCursorAdapter {
@SuppressWarnings("deprecation")
public ChannelAdapter(Context context, int layout, Cursor c,
String[] from, int[] to,int flag) {
super(context, layout, c, from, to, flag);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
Button bP = (Button) convertView.findViewById(R.id.buttonMainPlus);
final TextView tvName = (TextView) convertView.findViewById(R.id.textMainName);
final TextView tvValue = (TextView) convertView.findViewById(R.id.textMainValue);
// When plus button is clicked
bP.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int valuer = Integer.parseInt(tvValue.getText().toString());
ContentValues updatedValues = new ContentValues();
updatedValues.put(Database.C_VALUE,valuer+1);
//Uri rowURI =ContentUris.withAppendedId(MyContentProvider.CONTENT_URI,hoardId);
// Specify a specific row so no selection clause is required.
String where = Database.C_NAME+ " = '" + tvName.getText().toString() + "'" ;
String whereArgs[] = null;
// Get the Content Resolver.
cr = getContentResolver();
// Update the specified row.
int updatedRowCount = cr.update(MyContentProvider.CONTENT_URI, updatedValues, where, whereArgs);
}
});
return super.getView(position, convertView, parent);
}
}
@Override
public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle) {
switch (loaderID) {
case URL_LOADER:
return new CursorLoader(Main.this,MyContentProvider.CONTENT_URI,null,null,null,null);
default:
return null;
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
mAdapter.swapCursor(null);
}
}
Database.java
public class Database extends Activity {
public static final String TAG = "Database";
public static final String C_ID = BaseColumns._ID;
//Columns
public static final String C_NAME = "counterName";
public static final String C_VALUE = "counterValue";
public static final String C_ORDER = "counterOrder";
//Database name, version, table name
public static final String DB_NAME = "list.db";
public static final int DB_VERSION = 1; // If you change, it will call onUpgrade()
public static final String TABLE = "List";
Context context;
DbHelper dbHelper;
SQLiteDatabase db;
public Database(Context context) {
this.context = context;
dbHelper = new DbHelper();
}
class DbHelper extends SQLiteOpenHelper {
public DbHelper() {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = String.format("CREATE TABLE %s"
+ " (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s VARCHAR, %s INT, %s INT)",
TABLE, C_ID, C_NAME, C_VALUE, C_ORDER);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE);
onCreate(db);
}
}
public void openDb(){
db=dbHelper.getWritableDatabase();
}
public Cursor query() {
openDb();
// iterate over all rows columns on TABLE
Cursor cursor = db.query(TABLE, null, null, null, null, null, null);
return cursor;
}
}
我已经更改了
ChannelAdapter
,为了像这样膨胀列表项,我开始使用ViewHolder
,它将速度提高了大约%15-18(我从某个地方读到过),findViewById
花费大量时间来查找资源并膨胀每个项 慢的部分是int updatedRowCount=cr.update(MyContentProvider.CONTENT\u URI,updatedValues,where,wherergs),它调用内容提供程序的更新方法。我尝试将ContentProvider的更新方法更改为普通SQL查询。仍然很慢,但是如果我从MainActivity调用SQL查询,它就可以了。所以我看到LoaderCallbacks是异步工作的,一秒钟内更新屏幕两次很慢。我再次使用StartManagingCursor(),我很高兴:)让您尝试使用前面提到的AsyncQueryHandler
public class Database extends Activity {
public static final String TAG = "Database";
public static final String C_ID = BaseColumns._ID;
//Columns
public static final String C_NAME = "counterName";
public static final String C_VALUE = "counterValue";
public static final String C_ORDER = "counterOrder";
//Database name, version, table name
public static final String DB_NAME = "list.db";
public static final int DB_VERSION = 1; // If you change, it will call onUpgrade()
public static final String TABLE = "List";
Context context;
DbHelper dbHelper;
SQLiteDatabase db;
public Database(Context context) {
this.context = context;
dbHelper = new DbHelper();
}
class DbHelper extends SQLiteOpenHelper {
public DbHelper() {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = String.format("CREATE TABLE %s"
+ " (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s VARCHAR, %s INT, %s INT)",
TABLE, C_ID, C_NAME, C_VALUE, C_ORDER);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE);
onCreate(db);
}
}
public void openDb(){
db=dbHelper.getWritableDatabase();
}
public Cursor query() {
openDb();
// iterate over all rows columns on TABLE
Cursor cursor = db.query(TABLE, null, null, null, null, null, null);
return cursor;
}
}