RoRo
RoRo

Reputation: 118

Having ClassCastException using endless adapter

I'm having this classcastexception I combine lazylist by Fedor and Endless by Commonware

i'm having this error

08-09 18:06:26.553: E/AndroidRuntime(314): FATAL EXCEPTION: main
08-09 18:06:26.553: E/AndroidRuntime(314): java.lang.ClassCastException:com.example.endlesscustom.EndlessSample$CustomArrayAdapter
08-09 18:06:26.553: E/AndroidRuntime(314):  at com.example.endlesscustom.EndlessSample$DemoAdapter.appendCachedData(EndlessSample.java:174)
08-09 18:06:26.553: E/AndroidRuntime(314):  at com.commonsware.cwac.endless.EndlessAdapter$AppendTask.onPostExecute(EndlessAdapter.java:247)

It pointed out this error

ArrayAdapter<HashMap<String, String>> arrAdapterNew = ArrayAdapter<HashMap<String,String>>) getWrappedAdapter();

Here is my Code

public class EndlessSample extends ListActivity {

static int LIST_SIZE;
private int mLastOffset = 0;

static final int BATCH_SIZE = 3;

ArrayList<HashMap<String, String>> countriesSub = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maina);
    init();
    Log.i("country", String.valueOf(COUNTRIES.length));
    Log.i("mstrings", String.valueOf(mStrings.length));

}

private void init() {
    LIST_SIZE = COUNTRIES.length;
    for (int i = 0; i <= BATCH_SIZE; i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("country", COUNTRIES[i]);
        map.put("pic", mStrings[i]);
        countriesSub.add(map);
    }
    setLastOffset(BATCH_SIZE);
    displayList(countriesSub);
}

private void setLastOffset(int i) {
    mLastOffset = i;
}

private int getLastOffset() {
    return mLastOffset;
}

private void displayList(ArrayList<HashMap<String, String>> countriesSub2) {
    setListAdapter(new DemoAdapter());
}

private class CustomArrayAdapter extends BaseAdapter {
    public ImageLoader imageLoader;
    private ArrayList<HashMap<String, String>> data;

    public CustomArrayAdapter(Context context, int resource,
            int textViewResourceId,
            ArrayList<HashMap<String, String>> mylist) {
        data = mylist;
        imageLoader = new ImageLoader(context);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        HashMap<String, String> song = new HashMap<String, String>();

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) EndlessSample.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row, null);
            holder = new ViewHolder();
            holder.word = (TextView) convertView
                    .findViewById(R.id.throbber);
            holder.pecture = (ImageView) convertView
                    .findViewById(R.id.imahe);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Log.i("data size", String.valueOf(data.size()));
        song = data.get(position);
        holder.word.setText(song.get("country"));
        // holder.word.setText(String.valueOf(position + 1) + ". "
        // + countriesSub.get(position));
         imageLoader.DisplayImage(song.get("pic"), holder.pecture);
        return convertView;
    }

    public class ViewHolder {
        public ImageView pecture;
        public TextView word;
    }
}

class DemoAdapter extends EndlessAdapter {
    ArrayList<HashMap<String, String>> tempList = new ArrayList<HashMap<String, String>>();

    DemoAdapter() {
        super(new CustomArrayAdapter(EndlessSample.this,
                android.R.layout.simple_list_item_1, android.R.id.text1,
                countriesSub));

    }

    @Override
    protected View getPendingView(ViewGroup parent) {
        View row = getLayoutInflater().inflate(R.layout.row, null);

        View child = row.findViewById(android.R.id.text1);
        // child.setVisibility(View.GONE);
        child = row.findViewById(R.id.throbber);
        child.setVisibility(View.VISIBLE);

        return (row);
    }

    @Override
    protected boolean cacheInBackground() {
        tempList.clear();
        int lastOffset = getLastOffset();
        if (lastOffset < LIST_SIZE) {
            int limit = lastOffset + BATCH_SIZE;
            for (int i = (lastOffset + 1); (i <= limit && i < LIST_SIZE); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("country", COUNTRIES[i]);
                map.put("pic", mStrings[i]);
                tempList.add(map);
            }
            setLastOffset(limit);

            if (limit < LIST_SIZE) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    protected void appendCachedData() {

        @SuppressWarnings("unchecked")
        ArrayAdapter<HashMap<String, String>> arrAdapterNew = (ArrayAdapter<HashMap<String, String>>) getWrappedAdapter();
        int listLen = tempList.size();
        for (int i = 0; i < listLen; i++) {
            arrAdapterNew.add(tempList.get(i));
        }
    }
}

static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania",
        "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla",
        "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia",
        "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
        "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
        "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina",
        "Botswana", "Bouvet Island", "Brazil",
        "British Indian Ocean Territory", "British Virgin Islands",
        "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cote d'Ivoire",
        "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",
        "Central African Republic", "Chad", "Chile", "China",
        "Christmas Island", "Cocos (Keeling) Islands", "Colombia",
        "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia",
        "Cuba", "Cyprus", "Czech Republic",
        "Democratic Republic of the Congo", "Denmark", "Djibouti",
        "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt",
        "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
        "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji",
        "Finland", "Former Yugoslav Republic of Macedonia", "France",
        "French Guiana", "French Polynesia", "French Southern Territories",
        "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
        "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",
        "Guinea", "Guinea-Bissau", "Guyana", "Haiti",
        "Heard Island and McDonald Islands", "Honduras", "Hong Kong",
        "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq",
        "Ireland", "Israel", "Italy", "Jamaica" };

private String[] mStrings = {
        "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png",
        "http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
        "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
        "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
        "http://a1.twimg.com/profile_images/97470808/icon_normal.png",
        "http://a3.twimg.com/profile_images/511790713/AG.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
        "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
        "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
        "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
        "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
        "http://a1.twimg.com/profile_images/655119538/andbook.png",
        "http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
        "http://a1.twimg.com/profile_images/74724754/android_logo_normal.png",
        "http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
        "http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
        "http://a3.twimg.com/profile_images/548410609/icon_8_73.png",
        "http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
        "http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
        "http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android_normal.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
        "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
        "http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
        "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
        "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
        "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
        "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
        "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300.jpg",
        "http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
        "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
        "http://a3.twimg.com/profile_images/121630227/Droid.jpg",
        "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
        "http://a1.twimg.com/profile_images/97470808/icon_normal.png",
        "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man.png",
        "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
        "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
        "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
        "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter_normal.png",
        "http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
        "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
        "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
        "http://a1.twimg.com/profile_images/97470808/icon.png",
        "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
        "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
        "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
        "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
        "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
        "http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
        "http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
        "http://a1.twimg.com/profile_images/74724754/android_logo.png",
        "http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
        "http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
        "http://a3.twimg.com/profile_images/548410609/icon_8_73_normal.png",
        "http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
        "http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
        "http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
        "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
        "http://a1.twimg.com/profile_images/655119538/andbook.png",
        "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
        "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
        "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
        "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
        "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
        "http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
        "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
        "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
        "http://a1.twimg.com/profile_images/957149154/twitterhalf.jpg",
        "http://a1.twimg.com/profile_images/97470808/icon_normal.png",
        "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
        "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
        "http://a1.twimg.com/profile_images/349012784/android_logo_small.jpg",
        "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
        "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
        "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png" };
}

if anyone had encounter this error please help

thankyou so much

Upvotes: 0

Views: 360

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

The adapter you are putting into EndlessAdapter is a CustomArrayAdapter, not an ArrayAdapter<HashMap<String, String>>, which is why you get a ClassCastException when trying to cast the CustomArrayAdapter to ArrayAdapter<HashMap<String, String>> as part of your call to getWrappedAdapter().

Upvotes: 2

Related Questions