2011-05-26 77 views
0

在列表視圖中滾動幾秒鐘後,應用程序崩潰。帶有兩種不同佈局的列表視圖

String[] names = new String[] {"Cook","2/Person/15/5","Cashier","2/Person/15/5"}; 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 


     ViewHolder holder; 
     View rowView = convertView; 
     LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     String[] info = names.get(position).split("/"); 

     if (rowView == null) { 

       if (info.length != 1) { 
        rowView = inflater.inflate(R.layout.employee, null, true); 
        holder = new ViewHolder(); 
        holder.name = (TextView) rowView.findViewById(R.id.name); 
        holder.money= (TextView) rowView.findViewById(R.id.money); 
        holder.rating = (RatingBar) rowView.findViewById(R.id.rating); 
        rowView.setTag(holder); 
       }else{ 
        rowView = inflater.inflate(R.layout.lable, null, true); 
        holder = new ViewHolder(); 
        holder.lable = (TextView) rowView.findViewById(R.id.name); 

        rowView.setTag(holder); 
       } 


      } 

     holder = (ViewHolder) rowView.getTag(); 


     if (holder.lable==null) { 
      holder.name.setText(info[1]); 
      holder.money.setText(info[2]); 
      holder.rating.setRating(Float.valueOf("1")); 
     }else{ 
      holder.lable.setText(names.get(position)); 
     } 

     //} 



     return rowView; 
    } 
+0

發佈您的錯誤日誌。 – kcoppock 2011-05-26 21:34:06

+0

使用Logcat查看確切的錯誤消息:'adb logcat'或者日食中的DDMS透視圖 – 2011-05-26 21:35:32

+0

它表示爲nullPointerexeption @ holder.lable.setText(names.get(position)); – Michael 2011-05-26 22:01:44

回答

0

Android在ddms透視圖中有一個名爲logcat的調試工具,您可以在其中找到日誌。你將能夠完全弄清代碼崩潰的行號。

convertView主要用於所有列表項均一致的情況。因爲它有助於回收意見。這裏我們不確定回收視圖是R.layout.employee還是R.layout.lable。這可能會導致問題。

這就是猜測。 logcat將幫助您縮小問題範圍。

0

看的第一項"Cook"

String[] names = new String[] {"Cook","2/Person/15/5","Cashier","2/Person/15/5"}; 

你這樣做:

String[] info = names.get(position).split("/"); 

然後:

holder.name.setText(info[1]); 
holder.money.setText(info[2]); 

我沒有考慮到logcat中參見ArrayIndexOutOfBoundsException

+0

對不起,我在第一行添加了幫助人們理解代碼的方法。它應該是一個包含字符串的數組適配器。 – Michael 2011-05-26 21:59:17

+0

好的,但你沒有檢查數據,我的意思是'info [1]'和'info [2]'。 – pawelzieba 2011-05-26 22:05:45

+0

怎麼不檢查? – Michael 2011-05-26 22:09:05

3

由於您正在使用Viewholder模式和回收視圖,最終會出現混亂。 這是因爲你在列表中有兩種不同類型的視圖,當你得到一個convertview時,它將是其中的一個視圖。

想象的數據集A和佈局/觀看,並且對相同B B

(-B-意味着它不顯示在屏幕上還) 因此,如果您的初始列表是AA,BB,BB,AA ,-B-,-B-,然後滾動將數據B帶入列表中。然後,您將得到一個帶佈局a或佈局b的convertview,因此無法可靠地使用convertview。

我做一些搜索找到一種方法,以支持在ListView,並在同一時間使用convertView/viewHolder模式不同的看法..到目前爲止,我發現這一個很有趣:https://github.com/commonsguy/cwac-merge#readme

你想要做的是重寫getItemViewType(position)。 這裏是一個例子。你當然有你的dataTypes而不是MoreResultsLoader和YPContact。

@Override 
public int getItemViewType(int position) { 

    if (resultsList.get(position) instanceof MoreResultsLoader) { 
     return VIEW_TYPE_MORE_RESULTS_LOADER; 
    } 
    if (resultsList.get(position) instanceof YPContact) { 
     YPContact ypCon = (YPContact) resultsList.get(position); 
     if(checkForGold(ypCon)) 
      return VIEW_TYPE_YPCONTACT_GOLD; 
     else 
      return VIEW_TYPE_YPCONTACT_REG; 
    } 
    } 

然後在getView,您需要檢查哪些視圖類型你是在處理和膨脹/用正確的ViewHolder類填充。

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

    View ourView = convertView; 
    int itemViewType = getItemViewType(position); 
    switch (itemViewType) { 
    case VIEW_TYPE_MORE_RESULTS_LOADER: 
     MoreResultsViewHolder moreResVH = null; 
     if (ourView == null) { 

      ourView = layoutInflator.inflate(R.layout.load_more_items, 
        null, true); 
      moreResVH = new MoreResultsViewHolder(ourView); 
      ourView.setTag(moreResVH); 

     } else { 
      moreResVH = (MoreResultsViewHolder) ourView.getTag(); 
     } 
     if (moreResVH != null) { 
      moreResVH.populate((MoreResultsLoader) resultsList 
        .get(position), context); 
     } 
     return ourView; 
    case VIEW_TYPE_YPCONTACT_GOLD: 
     ContactViewHolderGold contactVHGold = null; 
     if (ourView == null) { 

      ourView = layoutInflator.inflate(
        R.layout.yp_search_result_list_item_gold, null, true); 
      contactVHGold = new ContactViewHolderGold(ourView); 
      ourView.setTag(contactVHGold); 

     } else { 
      contactVHGold = (ContactViewHolderGold) ourView.getTag(); 
     } 
     if (contactVHGold != null) { 
      final YPContact ypContact = (YPContact) resultsList 
        .get(position); 
      contactVHGold.populate(ypContact, mCurrentLocation, 
        ypSearchResultsActivity); 
      // moreResVH.populate(
      // (MoreResultsLoader)resultsList.get(position),context); 
     } 
     return ourView; 
+0

我不明白爲什麼不接受! – 2013-08-01 10:09:43