我从Android设备获取联系人。我的设备中有300个联系人。我在AsynckTask类的doInBackground()方法中编写了联系提取过程。所以在取消进度对话框opPostExecute()方法后,在android中的recyclerview中显示联系时间太长。请给我一些解决方案。
DatabaseAdapter类:
public class DatabaseAdapter {
EmsevaHelper帮手;
public DatabaseAdapter(Context context) {
helper = new Helper(context);
}
public long insertRecord(String name, String number, String photo) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Helper.CONTACT_NAME, name);
contentValues.put(Helper.CONTACT_NUMBER, number);
contentValues.put(Helper.CONTACT_PHOTO, photo);
long id = 0;
try {
id = db.insertOrThrow(Helper.TABLE_NAME_CONTACT, null, contentValues);
} catch (Exception e) {
Log.e("InsertExce", String.valueOf(e));
}
return id;
}
public int getContactCount() {
int count=0;
SQLiteDatabase db;
String countQuery = "SELECT * FROM " + Helper.TABLE_NAME_CONTACT;
db = helper.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.rawQuery(countQuery, null);
count=cursor.getCount();
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(cursor!=null)
{
cursor.close();
}
if(db!=null && db.isOpen())
{
db.close();
}
}
return count;
}
public ArrayList<ContactData> getAllRecord() {
ArrayList<ContactData> contactData = new ArrayList<>();
String selectQuery = "SELECT * FROM " + Helper.TABLE_NAME_CONTACT;
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
contactData.add(new ContactData(cursor.getString(0), cursor.getString(1), false, cursor.getString(2), cursor.getString(3)));
} while (cursor.moveToNext());
}
cursor.close();
return contactData;
}
static class Helper extends SQLiteOpenHelper {
private Context context;
private static final String DATABASE_NAME = "database";
private static final String TABLE_NAME_CONTACT = "CONTACT";
private static final int DATABASE_VERSION = 10;
private static final String CID = "_id";
private static final String CONTACT_NAME = "cname";
private static final String CONTACT_NUMBER = "cnumber";
private static final String CONTACT_PHOTO = "cphoto";
private static final String CREATE_CONTACT_TABLE = "CREATE TABLE " + TABLE_NAME_CONTACT + "(" + CID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ CONTACT_NAME + " VARCHAR(100)," + CONTACT_NUMBER + " VARCHAR(50) UNIQUE ," + CONTACT_PHOTO + " VARCHAR(255));";
private static final String DROP_CONTACT_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME_CONTACT;
public Helper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
//creating required table.
try {
db.execSQL(CREATE_CONTACT_TABLE);
Log.e("TableCreate", "Susseccfully");
} catch (SQLException e) {
Log.e("onCreate Exce****", e + "");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//on upgrade drop older tables.
try {
db.execSQL(DROP_CONTACT_TABLE);
//create new tables.
onCreate(db);
} catch (SQLException e) {
Log.e("onUpgrade Exce****", e + "");
}
}
}
}
片段上的AsynckTask: class ContactLoader extends AsyncTask<Void,Void,Void>{
ProgressDialog pd;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=ProgressDialog.show(getContext(),"Fetching Contact!","Please wait...",true,false);
}
@Override
protected Void doInBackground(Void... params) {
contactData=databaseAdapter.getAllRecord();
contactAdapter.setContactAdapterData(contactData);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
rvContactList.setAdapter(contactAdapter);
super.onPostExecute(aVoid);
pd.cancel();
}
}
答案 0 :(得分:0)
不要一次获取所有项目,而是使用scroll listener
。基于滚动状态加载更多联系人。最初,您可以加载50
个联系人,然后在向下滚动列表时加载。继续向联系人列表添加更多联系人并通知adapter
。
您可以执行以下操作,您可以在其中跟踪各种滚动状态:
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
}
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
}
} else if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
}
} else {
}
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});