androidlistviewbaseadapterandroid-viewholdercustom-lists

My application closes while scrolling down custom ListView


I'm using custom ListView and Adapter for load image and description into ListView. All the data loading from MySQL database. Then I try to scroll my list then automatically close my application. How can I solve this close problem?

Adapter class

public class ItemAdapter extends BaseAdapter {
private Context mContext;
private List<Item> mItemList;

public ItemAdapter(Context mContext, List<Item> mItemList) {
    this.mContext = mContext;
    this.mItemList = mItemList;
}

@Override
public int getCount() {
    return mItemList.size();
}

@Override
public Object getItem(int position) {
    return mItemList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if (convertView == null) {
        convertView = View.inflate(mContext, R.layout.item_item, null);
        holder = new ItemAdapter.ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ItemAdapter.ViewHolder) convertView.getTag();
    }

    holder.name.setText(mItemList.get(position).getItemName());
    holder.desc.setText(mItemList.get(position).getItemDesc());
    Picasso.with(mContext).load(mItemList.get(position).getImgUrl()).into(holder.image);
    convertView.setTag(mItemList.get(position).getItemId());

    return convertView;
}

private static class ViewHolder {
    private TextView name;
    private TextView desc;
    private ImageView image;

    public ViewHolder(View v) {
        name = (TextView) v.findViewById(R.id.item_name);
        desc = (TextView) v.findViewById(R.id.item_description);
        image = (ImageView) v.findViewById(R.id.img_item);
    }
}
}

Item custom layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:background="#ffffff"
android:scrollbars="vertical">

<android.support.v7.widget.CardView
    android:id="@+id/item_cardview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:elevation="3dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="4dp"
    card_view:cardCornerRadius="1dp"
    app:cardCornerRadius="6dp"
    app:cardElevation="3dp"
    app:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/img_item"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:src="@drawable/dap_launcher"
            android:layout_marginLeft="15dp"
            android:clickable="true"/>

        <TextView
            android:id="@+id/item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img_item"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp"
            android:textStyle="bold"
            android:text="Item Name"
            android:textSize="15dp"
            android:layout_marginTop="2dp"/>

        <TextView
            android:id="@+id/item_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/item_name"
            android:layout_marginTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp"
            android:text="Item Description"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

</LinearLayout>

List layout

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview_item"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:dividerHeight="10dp"
    android:divider="#d1d1d1">
</ListView>

</RelativeLayout>

Logcat

07-13 15:37:43.853 25410-25410/com.example.swserver03.dap D/AndroidRuntime: Shutting down VM
07-13 15:37:43.855 25410-25410/com.example.swserver03.dap E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.swserver03.dap, PID: 25410
    java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.swserver03.dap.Adapter.ItemAdapter$ViewHolder
        at com.example.swserver03.dap.Adapter.ItemAdapter.getView(ItemAdapter.java:47)
        at android.widget.AbsListView.obtainView(AbsListView.java:2428)
        at android.widget.ListView.makeAndAddView(ListView.java:2083)
        at android.widget.ListView.fillDown(ListView.java:793)
        at android.widget.ListView.fillGap(ListView.java:757)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5520)
        at android.widget.ListView.trackMotionScroll(ListView.java:1991)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:5052)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:981)
        at android.view.Choreographer.doCallbacks(Choreographer.java:790)
        at android.view.Choreographer.doFrame(Choreographer.java:718)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:967)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:101)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7425)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
07-13 15:37:43.868 25410-25410/com.example.swserver03.dap I/Process: Sending signal. PID: 25410 SIG: 9

Solution

  • I find some simple way to resolve the problem. I added setId to my getView. Then I can use getId for my setOnItemClickListener

    getView() in Adapter

    convertView.setId(mItemList.get(position).getItemId());
    convertView.setTag(holder);
    

    setOnItemClickListener() in Item class

    lvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(),"Item ID : " + view.getId(),Toast.LENGTH_SHORT).show();                
            }
        });