android - ListView getting displayed, but App crashes on Scrolling and clicking -
i have listview custom layout named custom_listview.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="20dp" android:padding="5dp" android:id="@+id/file_name" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="18dp" android:id="@+id/file_path"/> </linearlayout> and customadapter class:
public class customadapter extends baseadapter { arraylist<file> result; context context; private static layoutinflater inflater=null; public customadapter(activity parentactivity, arraylist<file> filelist) { // todo auto-generated constructor stub result=filelist; context=parentactivity; inflater = ( layoutinflater )context. getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return result.size(); } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } public class holder { textview file_name; textview file_path; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub if (convertview == null) { layoutinflater minflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); convertview = minflater.inflate(r.layout.custom_listview, null); } holder holder=new holder(); holder.file_name=(textview) convertview.findviewbyid(r.id.file_name); holder.file_path=(textview) convertview.findviewbyid(r.id.file_path); //holder.img=(imageview) rowview.findviewbyid(r.id.imageview1); holder.file_name.settext(result.get(position).getname()); holder.file_path.settext(result.get(position).getpath()); return convertview; } } and here onclicklistener listview:
public void onitemclick(adapterview<?> parent, view view, int position, long id) { int color = color.transparent; drawable background = parent.getchildat(position).getbackground(); if (background instanceof colordrawable) color = ((colordrawable) background).getcolor(); if(color == color.cyan) { parent.getchildat(position).setbackgroundcolor(color.transparent);} else { parent.getchildat(position).setbackgroundcolor(color.cyan); textview t; t = (textview) view.findviewbyid(r.id.file_path); files_selected.addelement(t.gettext().tostring()); } } } the app works fine when click on item on top of list. scroll down , click item, app crashes. please help. thanks!
shouldnt be;
if (convertview == null) { layoutinflater minflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); convertview = minflater.inflate(r.layout.custom_listview, null); holder holder=new holder(); holder.file_name=(textview) convertview.findviewbyid(r.id.file_name); holder.file_path=(textview) convertview.findviewbyid(r.id.file_path); //holder.img=(imageview) rowview.findviewbyid(r.id.imageview1); holder.file_name.settext(result.get(position).getname()); holder.file_path.settext(result.get(position).getpath()); }else{ viewholder = (viewholder) view.gettag(); } view.settag(viewholder);
Comments
Post a Comment