2014-12-03 77 views
0

我想在我的列表視圖中添加一個加載圖標作爲footerView。 但是,當我滾動和新數據添加到列表視圖 我得到ClassCastException異常和我的應用程序已接近ClassCastException當嘗試加載更多的列表視圖

我拿到的時候LoadMoreXML類是執行

誰能幫助我例外呢?

這是我的代碼

主要碎片

public class LayoutActivity extends Fragment implements OnScrollListener{ 

    ListView listview; 
    ListItemAdapter theAdapter; 
    ListItemAdapter myExistingAdapter = null; 
    String URL = "http://localhost/api/question/get_newest_except/0/0/5"; 
    ProgressDialog pDialog; 
    NodeList nodelist; 
    ArrayList<ListItemObject> data; 
    Integer counter=0; 

    public LayoutActivity() { 
     super(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootview = inflater.inflate(R.layout.layout_main, container,false); 
     listview = (ListView) rootview.findViewById(R.id.list01); 
     ProgressBar spinner = new ProgressBar(this.getActivity()); 
     listview.addFooterView(spinner); 

     new DownloadXML().execute(URL); 
     listview.setOnScrollListener(this); 
     return rootview; 
    } 
    public class DownloadXML extends AsyncTask<String, Void, Void>{ 

     public DownloadXML() { 
      super(); 
     } 

     @Override 
     protected void onPreExecute() { 

     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      data = new ArrayList<ListItemObject>(); 
      ListItemObject itemData; 

      try{ 
       for (int temp = 0; temp < nodelist.getLength(); temp++) { 
        Node nNode = nodelist.item(temp); 
        if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
         Element eElement = (Element) nNode; 
         itemData = new ListItemObject(); 

         itemData.setId(getNode("pb__question__id",eElement)); 
         itemData.setOwner(getNode("pb__question__consumer__id",eElement)); 
         if(!getNode("pb__question__consumer__id",eElement).equalsIgnoreCase("0")){ 
          itemData.setName(getNode("pb__question__consumer__name",eElement)); 
          itemData.setJob(getNode("pb__question__consumer__occupation", eElement)); 
          itemData.setProfilePic(getNode("pb__question__consumer__pp",eElement)); 
         } 
         itemData.setStatus(getNode("pb__question__title",eElement)); 
         itemData.setExtras(getNode("pb__question__topic__name", eElement)); 
         if(!getNode("att__pict",eElement).isEmpty()){ 
          itemData.setImage(getNode("att__pict", eElement)); 
         } 

         if(getNode("pb__question__type", eElement).equalsIgnoreCase("1")){ 
          itemData.setOpini(getNode("pb__question__total__opini", eElement)); 
         }else if(getNode("pb__question__type", eElement).equalsIgnoreCase("2") || getNode("pb__question__type", eElement).equalsIgnoreCase("3")){ 
          itemData.setOpini(getNode("pb__question__total__polling", eElement)); 
         }else if(getNode("pb__question__type", eElement).equalsIgnoreCase("4")){ 
          itemData.setOpini(getNode("pb__question__total__rating", eElement)); 
         } 

         itemData.setTipe(getNode("pb__question__type", eElement)); 
         itemData.setIkuti(getNode("pb__question__total__follow", eElement)); 
         itemData.setSebarkan(getNode("pb__question__total__share", eElement)); 

         data.add(itemData); 
        } 
       } 
       theAdapter = new ListItemAdapter(LayoutActivity.this.getActivity(),data); 
       listview.setAdapter(theAdapter); 

      }catch(Exception e){ 
       Toast.makeText(getActivity(), "Koneksi dengan server gagal" , Toast.LENGTH_SHORT).show(); 
      } 

     } 

     @Override 
     protected Void doInBackground(String... Url) { 
      try { 
       URL url = new URL(Url[0]); 
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       Document doc = db.parse(new InputSource(url.openStream())); 
       doc.getDocumentElement().normalize(); 
       nodelist = doc.getElementsByTagName("pb__question"); 

      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

    } 

    public class LoadMoreXML extends AsyncTask<String, Void, Void>{ 
     ArrayList<ListItemObject> data; 

     public LoadMoreXML() { 
      super(); 
     } 

     @Override 
     protected void onPreExecute() { 

     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      data = new ArrayList<ListItemObject>(); 
      ListItemObject itemData; 
       for (int temp = 0; temp < nodelist.getLength(); temp++) { 
        Node nNode = nodelist.item(temp); 
        if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
         Element eElement = (Element) nNode; 
         itemData = new ListItemObject(); 

         itemData.setId(getNode("pb__question__id",eElement)); 
         itemData.setOwner(getNode("pb__question__consumer__id",eElement)); 
         if(!getNode("pb__question__consumer__id",eElement).equalsIgnoreCase("0")){ 
          itemData.setName(getNode("pb__question__consumer__name",eElement)); 
          itemData.setJob(getNode("pb__question__consumer__occupation", eElement)); 
          itemData.setProfilePic(getNode("pb__question__consumer__pp",eElement)); 
         } 
         itemData.setStatus(getNode("pb__question__title",eElement)); 
         itemData.setExtras(getNode("pb__question__topic__name", eElement)); 
         if(!getNode("att__pict",eElement).isEmpty()){ 
          itemData.setImage(getNode("att__pict", eElement)); 
         } 

         if(getNode("pb__question__type", eElement).equalsIgnoreCase("1")){ 
          itemData.setOpini(getNode("pb__question__total__opini", eElement)); 
         }else if(getNode("pb__question__type", eElement).equalsIgnoreCase("2") || getNode("pb__question__type", eElement).equalsIgnoreCase("3")){ 
          itemData.setOpini(getNode("pb__question__total__polling", eElement)); 
         }else if(getNode("pb__question__type", eElement).equalsIgnoreCase("4")){ 
          itemData.setOpini(getNode("pb__question__total__rating", eElement)); 
         } 

         itemData.setTipe(getNode("pb__question__type", eElement)); 
         itemData.setIkuti(getNode("pb__question__total__follow", eElement)); 
         itemData.setSebarkan(getNode("pb__question__total__share", eElement)); 

         data.add(itemData); 
        } 
       } 

       myExistingAdapter = (ListItemAdapter) listview.getAdapter(); 
       myExistingAdapter.addItems(data); 
     } 

     @Override 
     protected Void doInBackground(String... Url) { 
      try { 
       URL url = new URL(Url[0]); 
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       Document doc = db.parse(new InputSource(url.openStream())); 
       doc.getDocumentElement().normalize(); 
       nodelist = doc.getElementsByTagName("pb__question"); 

      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

    } 

    private static String getNode(String sTag, Element eElement) { 
     NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes(); 
     Node nValue = (Node) nlList.item(0); 
     String result = ""; 
     if(nValue!=null){ 
      result = nValue.getNodeValue(); 
     } 
     return result; 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, 
      int visibleItemCount, int totalItemCount) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // TODO Auto-generated method stub 
     int threshold = 2; 
     int count = listview.getCount(); 
     if(scrollState == SCROLL_STATE_IDLE){ 

      if(listview.getLastVisiblePosition() >= count - threshold){ 
       counter+=5; 
       new LoadMoreXML().execute("http://localhost/api/question/get_newest_except/0/"+counter+"/5"); 
      } 
     } 
    } 
} 

這是錯誤登錄

enter image description here

謝謝

+0

它給錯誤,當我在onPostExecute方法 – cakduro 2014-12-03 07:42:24

+0

執行LoadMoreXML類我問行。不是方法 – 2014-12-03 08:02:38

回答

1

當您調用listview.addFooterView時,Android正在爲您創建一個支持頁腳的適配器。然後,當您調用setAdapter時,Android不會替換它創建的適配器,但它仍然保留您的ListItemAdapter並將大多數適配器請求委派給該對象。然後,當您將ListView的適配器轉換爲您的類時,會因爲您的適配器實際位於支持頁腳的包裝適配器內部而崩潰。那有意義嗎?

你需要做的是在onCreateView中創建你的ListItemAdapter,並將它附加到你的ListView上。請注意,只要調用addFooterView,ListView的適配器實際上是一個不同的適配器,但是你的內部仍然在新的適配器內。

public class LayoutActivity extends Fragment implements OnScrollListener{ 
    ListItemAdapter theAdapter; // don't need myExistingAdapter any more 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootview = inflater.inflate(R.layout.layout_main, container,false); 
     listview = (ListView) rootview.findViewById(R.id.list01); 

     // Create your adapter with no initial data 
     theAdapter = new ListViewAdapter(LayoutActivity.this.getActivity(), null); 
     listview.setAdapter(theAdapter); 

現在包括您的任務,僅僅將適配器直接,添加項目:

public class DownloadXML extends AsyncTask<String, Void, Void>{ 
    @Override 
    protected void onPostExecute(Void result) { 
     ... 
     theAdapter.addItems(data); 

public class LoadMoreXML extends AsyncTask<String, Void, Void>{ 
    @Override 
    protected void onPostExecute(Void result) { 
     ... 
     theAdapter.addItems(data); 
+0

我試着做你的建議。 但我得到了NullPointerException,因爲我將空值傳遞給我的適配器。 我需要通過我從我的AsyncTask類 這就是爲什麼我叫DownloadXML類中setAdapter獲得ArrayList的數據。 – cakduro 2014-12-03 07:53:22

+0

解決了! 其實,我必須創建我的內DownloadXML適配器,因爲我需要傳遞的數據,但裏面LoadMoreXML,我必須改變喜歡你有什麼建議。 謝謝,這是現在的作品:D – cakduro 2014-12-03 08:00:35

相關問題