i’m making a listview ,but scrolling is too slow.
i have a ImageButton and TextView in a page.
if i click on ImageButton, i can get a photo from camera album.
if saving, image uri(camera Album) and texts are saved into database.
and then listview is getting data from database.
i can see image and text on the listview well.
but scrolling is too slow..
I use AsyncTask in this code, but as im a beginneer, i don’t know how to exactly use the AsyncTask for better scrolling
could anybody see my code and guide me?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
lv=(ListView)findViewById(R.id.listView1);
new SubClass().execute();
}
/******************CustomCursorAdapter*******************/
public class CustomCusorAdapter extends CursorAdapter{
RelativeLayout listview_relative;
public CustomCusorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
wrap = (ViewWrap) v.getTag();
for(int i=0; i<cursor.getColumnCount();i++){
}
if(cursor.getPosition()%2==1){ //if position is odd, color is something.
wrap.getRelative().setBackgroundColor(0xfff0f8ff);
}
else if(cursor.getPosition()%2==0){ //if position is even, color is something other.
wrap.getRelative().setBackgroundColor(0xfffff5ee);
}
String titleText = cursor.getString(1);
String bodyText = cursor.getString(2);
String dateText = cursor.getString(3);
imageStr = cursor.getString(4);
wrap.getTitle().setText(titleText);
wrap.getDate().setText(dateText);
wrap.getBody().setText(bodyText);
if(imageStr != null){
uri = Uri.parse(imageStr);
wrap.getImage().setImageURI(uri);
}else{
wrap.getImage().setImageResource(R.drawable.resize_camera_input_photo1);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.listview, null);
ViewWrap wrap = new ViewWrap(v);
v.setTag(wrap);
return v;
}
}//CustomCusorAdapter
/******* Sub AsyncTask***********************************************************/
class SubClass extends AsyncTask<Cursor, Cursor, Cursor>{
@Override
protected Cursor doInBackground(Cursor... params) {
mydb = new MyDatabase(getBaseContext());
db = mydb.getReadableDatabase();
cursor = db.rawQuery("select * from "+ TABLE_NAME + " order by _id desc;", null);
startManagingCursor(cursor);
return cursor;
}
@Override
protected void onPostExecute(Cursor result) {
adapter = new CustomCusorAdapter(getBaseContext(), result);
lv.setAdapter(adapter);
mydb.close();
db.close();
}
}
Since you just scroll the list after the AsyncTask has returned, I do not think that this is a problem related to the fetching of the data..
The adapter is the one that can take a set of optimizations:
Hope this helps!