Made a custom list view that includes an Image, two text views and a checkbox. I could identify the checkbox being checked/ unchecked, but the problem with this design is very slow scrolling. Will this get faster if I test it into the real device.
public class ListViewActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ArrayAdapter<Model> compositeAdapter = new CompositeAdapter(this,
getModel());
setListAdapter(compositeAdapter);
ListView lv = getListView();
lv.setFastScrollEnabled(true);
}
}
private class CompositeAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public CompositeAdapter(Activity context, List<Model> list) {
super(context, R.layout.listviewactivitylayout, list);
this.context = context;
this.list = list;
}
private class ViewHolder{
protected TextView textView1;
protected TextView textView2;
protected ImageView imageView;
protected CheckBox ckBox;
}
public View getView (int position, View convertView, ViewGroup parent) {
View view = null;
final ViewHolder vh;
if(convertView==null){
LayoutInflater li = context.getLayoutInflater();
view = li.inflate(R.layout.listviewactivitylayout, null);
vh = new ViewHolder();
vh.ckBox =(CheckBox) view.findViewById(R.id.list_view_layout_checkbox);
vh.imageView = (ImageView) view.findViewById(R.id.listView_image_view);
vh.textView1 = (TextView)view.findViewById(R.id.list_view_layout_text_view1);
vh.textView2= (TextView)view.findViewById(R.id.list_view_text_view2);
view.setTag(vh);
}
else{
view= convertView;
vh = (ViewHolder)view.getTag();
}
/**
** sending text and images to the each of the list view
**
*/
You are loading images. that needs to be handled in a seperate thread and just placed on an imageView when it is decoded.
here is a excelent project that will help you do that.
Lazy load images
EDIT
by looking at your code there are few things i am noticing:
First: there isnt any place you are asigning a picture. (use the lazy loading adapter for that)
Second: your checkbox selections will get messed up. you need to keep track of the checked position with state and redraw in getView (hashmap or even better a sparse array for that)
Third is to get rid of the helper view you are using. you don’t need it. (it is market as
View viewin your code).So, if
convertView==nulljust useconvertView=inflate...and return that one at the end.And Fourth as a useful tip: move the adapter class to a new class java file. you are already keeping the activity reference and the list, you can reduce the amount of code in your activity.