2012-08-09 37 views
0

我有這個ClassCastException異常 我通過費奧多爾通過Commonware有ClassCastException異常使用無盡適配器

我遇到這個錯誤

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) 

它指出了這個錯誤結合lazylist和無盡

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

這是我的代碼

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" }; 
} 

如果任何人有遇到這個錯誤,請幫助

三江源這麼多

+0

取而代之的是,你可以嘗試[Never Ending List](http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/ ) – Praveenkumar 2012-08-09 10:51:26

+0

謝謝@SpK也是一個不錯的教程 – RoRo 2012-08-10 02:54:02

回答

2

你投入EndlessAdapter適配器是一個CustomArrayAdapter,不是ArrayAdapter<HashMap<String, String>>,這就是爲什麼你想投的CustomArrayAdapter時收到ClassCastException作爲您致電getWrappedAdapter()的一部分,致電ArrayAdapter<HashMap<String, String>>

+0

所以我只能用ArrayAdapter ??如何使用這些圖像 – RoRo 2012-08-09 10:48:13

+0

@RoBin:您正在使用'CustomArrayAdapter'。請一致*使用'CustomArrayAdapter',或者去除'CustomArrayAdapter'。你不能奇怪地將'CustomArrayAdapter'轉換成'ArrayAdapter >或'ArrayAdapter '或者'Integer'或者'Button'。 – CommonsWare 2012-08-09 10:51:04

+0

ahh我寫完後就明白了,對不起= D thnx – RoRo 2012-08-09 10:54:56