2014-01-06 26 views
0

下面是我的适配器类的列表视图代码在游戏的Android 列表视图显示3种行:锁定 1)完成水平 2)在建 3级)级别崩溃与一个ListView适配器和ViewHolders

我试图使用回收站尽可能减少内存泄漏。 使用此代码时,我遇到了崩溃(请参阅下文)。当我向下滚动列表时发生崩溃,并且在列表中遇到锁定的级别。显示进行中的级别工作正常。 我认为我在持有人的类别上做错了事,但我不知道是什么。 感谢您收看本案。

下面的崩溃:

下面我班的
01-06 17:11:26.023: E/AndroidRuntime(2943): FATAL EXCEPTION: main 
    01-06 17:11:26.023: E/AndroidRuntime(2943): java.lang.ClassCastException: com.Adptr_List$ViewHolderInProgress cannot be cast to com.Adptr_List$ViewHolderLocked 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at com.Adptr_List.getView(Adptr_List.java:302) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.widget.AbsListView.obtainView(AbsListView.java:2255) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.widget.ListView.makeAndAddView(ListView.java:1769) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.widget.ListView.fillDown(ListView.java:672) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.widget.ListView.fillGap(ListView.java:636) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5040) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4197) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.view.Choreographer.doFrame(Choreographer.java:524) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.os.Handler.handleCallback(Handler.java:615) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.os.Handler.dispatchMessage(Handler.java:92) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.os.Looper.loop(Looper.java:137) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at java.lang.reflect.Method.invokeNative(Native Method) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at java.lang.reflect.Method.invoke(Method.java:511) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    01-06 17:11:26.023: E/AndroidRuntime(2943):  at dalvik.system.NativeStart.main(Native Method) 

代码:

public class Adptr_List extends ArrayAdapter<Child_Level> { 

Context context; 

int globalshpsfound = 0; 
int currentlvlshpsfound = 0; 

class ViewHolderComplete 
{ 
    public TextView tvtitle; 
    public TextView tvsubtitle; 
    public ProgressBar pb; 
    public TextView shpprogress; 

    public ViewHolderComplete(
      TextView tvtitle, 
      TextView tvsubtitle, 
      ProgressBar pb, 
      TextView shpprogress) { 
    }  
} 

class ViewHolderInProgress 
{ 
    public TextView tvtitle; 
    public TextView tvsubtitle; 
    public ProgressBar pb; 
    public TextView shpprogress; 
    public TextView tvPlay; 

    public ViewHolderInProgress(
      TextView tvtitle, 
      TextView tvsubtitle, 
      ProgressBar pb, 
      TextView shpprogress, 
      TextView tvPlay) { 
    } 
} 

class ViewHolderLocked 
{ 
    public TextView tvtitle; 
    public TextView tvsubtitle; 
    public TextView tvmontounlock; 

    public ViewHolderLocked(
      TextView tvtitle, 
      TextView tvsubtitle, 
      TextView tvmontounlock) { 
    } 
} 

//Initialize adapter 
public Adptr_List(Context context, int resource, List<Child_Level> items) { 
    super(context, resource, items); 

    SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); 

    globalshpsfound = sPrefs.getInt("globalshpsfound", 0);   
}  


@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{    
    SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); 

    final Child_Level l = getItem(position); 

    currentlvlshpsfound = sPrefs.getInt("foundshps_lvl_"+l.lvlid, 0); 
    globalshpsfound = sPrefs.getInt("globalshpsfound", 0); 

    // COMPLETE 
    if (sPrefs.getInt("foundshps_lvl_"+l.lvlid, 0) == l.shpamount) { 

     ViewHolderComplete holdercomplete; 
     View resultview; 
     View myconvertview; 

     if(convertView==null){ 

      ViewGroup viewGroup = (ViewGroup)LayoutInflater.from(getContext()).inflate(R.layout.inflt_lvl_complete, null);        

      //using the ViewHolder pattern to reduce lookups 
      holdercomplete = new ViewHolderComplete(
        (TextView)viewGroup.findViewById(R.id.tvLevel_title), 
        (TextView)viewGroup.findViewById(R.id.tvLevel_subtitle), 
        (ProgressBar) viewGroup.findViewById(R.id.pbLevel_progress), 
        (TextView) viewGroup.findViewById(R.id.tvLevel_progress)); 

      viewGroup.setTag(holdercomplete); 
      resultview = viewGroup; 
     } 

     else{ 
      holdercomplete = (ViewHolderComplete) convertView.getTag(); 
      resultview=convertView; 
     } 


     // ...here iam setting text and images 

     return resultview; 
     } 

    // IN PROGRESS 
    else if (sPrefs.getInt("globalshpsfound", 0)>=l.shptounlock || 
      (sPrefs.getBoolean("bought_lvl_"+l.lvlid, false)==true)){ 

     ViewHolderInProgress holderinprogress; 
     View resultview; 
     View myconvertview; 

     if(convertView==null){ 

      ViewGroup viewGroup = (ViewGroup)LayoutInflater.from(getContext()).inflate(R.layout.inflt_lvl_inprogress, null); 


      //using the ViewHolder pattern to reduce lookups 
      holderinprogress = new ViewHolderInProgress(
        (TextView)viewGroup.findViewById(R.id.tvLevel_title), 
        (TextView)viewGroup.findViewById(R.id.tvLevel_subtitle), 
        (ProgressBar) viewGroup.findViewById(R.id.pbLevel_progress), 
        (TextView) viewGroup.findViewById(R.id.tvLevel_progress), 
        (TextView) viewGroup.findViewById(R.id.tvLevel_play)); 


      viewGroup.setTag(holderinprogress); 
      resultview = viewGroup; 
     } 

     else { 
      holderinprogress = (ViewHolderInProgress) convertView.getTag(); 
      resultview=convertView; 
     } 

     // ...here iam setting text and images 

     return resultview; 
     } 



    // LOCKED 
    else { 
     ViewHolderLocked holderlocked; 
     View resultview; 
     View myconvertview; 

     if(convertView==null){ 

      ViewGroup viewGroup = (ViewGroup)LayoutInflater.from(getContext()).inflate(R.layout.inflt_lvl_locked, null);     

      //using the ViewHolder pattern to reduce lookups 
      holderlocked = new ViewHolderLocked(
        (TextView)viewGroup.findViewById(R.id.tvLevel_title), 
        (TextView)viewGroup.findViewById(R.id.tvLevel_subtitle), 
        (TextView)viewGroup.findViewById(R.id.tvLevel_montounlock)); 
      //holderlocked = new ViewHolder(); 


      viewGroup.setTag(holderlocked); 
      resultview = viewGroup; 
     } 

     else{ 
      holderlocked = (ViewHolderLocked) convertView.getTag(); 
      resultview=convertView; 
     } 

     // ...here iam setting text and images 

     return resultview; 
    } 
} 

}

回答

0

好吧,我发现使用getitemviewtype的解决方案:

public int getItemViewType(int position) { 


    SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); 

    if (sPrefs.getInt(somedata stored in sharedpref) 
     return 0; 

    else if (somedata stored in sharedpref) 
     return 1; 

    else 
     return 2; 
} 

显然pb是由sharedprefs数据和子视图之间的混合引起的适配器的每一行。 出于某种原因,在getview中混合它们并不起作用,但用getitemviewtype将它们分开工作。

0

最好是只使用单一ViewHolder。在某些情况下,请将某些字段留空,但请保持相同。 不要从同一个函数返回不同类型的ViewHolder。

+0

感谢您的回答;不幸的是,它不工作,特定于锁定级别的布局不会膨胀。相反,总是膨胀的布局正在进行中。 – Don

1

您需要实现的方法getViewTypeCount和返回不同的细胞类型的数量,你的情况3.

详细答案在这里:

Android ListView with different layouts for each row

+0

感谢您的回答,不幸的是,这是行不通的,添加@Override public int getViewTypeCount(){ return 3; } – Don

+0

你还实现了getItemViewType()吗? – Andros

+0

我没有昨天,但我今天早上做了,确实使用getitemviewtype帮助找到解决方案。我已经详细解答了一个自动答案,否则我会接受你的答案。感谢您的帮助,祝您有美好的一天 – Don