5

나는 해결할 수없는 버그로 시장에 응용 프로그램을 가지고 있습니다. 오늘 나는 아래의 방법으로 그것을 추적했다.ListView는 어댑터에 항목이 추가 되더라도 비어 있습니다. 응용 프로그램 재개시 언젠가

public void updateDealList(ArrayList<Deal> deals) { 
    // first call or showing bookmakrs (also gets called other times when adapter is null) 
    if (dealListAdapter == null || showingBookmarks || !dealListAdapter.moreDealsToDownload()) { 
     dealListAdapter = new EndlessDealListAdapter(getActivity(), 
       R.layout.deal_list_item, deals); 
     setListAdapter(dealListAdapter); 
     listView = getListView(); 
     if (getActivity().findViewById(R.id.right_fragment_container)!=null){ // if tablet 
      listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
      listView.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT); 
     } 
     showingBookmarks=false; 
     Log.d("UPDATE_DEAL_LIST", "Notified list created new" + dealListAdapter.getCount()); 
     return; //PROBLEM OCCURS WHEN APP COMES IN HERE AFTER RESUMING 
    } 
    dealListAdapter.getDealListAdapter().clear(); 
    for (Deal d: deals){ 
     dealListAdapter.getDealListAdapter().add(d); 
     Log.d("UPDATE_DEAL_LIST", "added " + d.title); 
    } 
    dealListAdapter.notifyDataSetChanged(); 
    Log.d("UPDATE_DEAL_LIST", "Notified list " + dealListAdapter.getCount()); 
} 

이 방법은 인터넷에서 끌어온 거래 객체의 arraylist를 전달합니다. 응용 프로그램을 처음 열면 인터넷에서 데이터가 추출되고 위의 메서드가 호출되어 ListFragment에 대해 설정된 새 어댑터가 만들어집니다. 이 메소드는 사용자가 더 많은 거래를 요청할 때도 호출됩니다.

내가 겪고있는 문제는 목록이 가끔 거래가 들어있는 어댑터를 비어있는 것으로 생각한다는 것입니다. 이것은 장치의 메모리가 부족할 때 응용 프로그램을 다시 시작할 때 발생합니다 (응용 프로그램의 일부가 RAM에서 제거되었다고 가정 함). 이 메서드가 호출되고 dealListAdapter가 null이므로 새 것이 만들어지고 거래가 추가됩니다. 이러한 일이 발생 했음에도 불구하고 목록은 비어 있고 사용자는 앱을 다시 작동 시키려면 앱을 닫아야합니다.

아래 줄은 메소드가 호출 될 때 tat를 표시하고 if를 입력하면 21 개가 어댑터에 추가됩니다. 불행하게도이 목록은 사용자에게 비어 있습니다.

05-23 01:52:32.879: D/UPDATE_DEAL_LIST(3478): Notified list created new21 
+0

추가 코드가 도움이되는지 알려주십시오. – bencallis

+0

어댑터를 작성한 후 어댑터를 지우고 거래 배열에서 모든 항목을 추가하십시오. 이 배열을 어떻게 저장하고 있습니까? 클래스의 멤버 변수 인 경우 시스템에서 활동을 종료하면 유지되지 않습니다. 활동 상태가 일시 중지되거나 중지되었을 때 저장합니까? http://developer.android.com/guide/topics/fundamentals/activities.html#SavingActivityState –

+0

메서드의 if 본문에있는 경우 끝에 반환되므로 어댑터가 즉시 지워지지 않습니다. 어댑터는 ListFragment 클래스에 인스턴스 변수로 저장됩니다. ArrayList 자체는 자주 변경되므로 저장되지 않습니다. 현재 상태가 저장되지 않았습니다. 살해 된 후에 앱을 다시 시작하면 문제가 발생하는 것 같습니다. 안드로이드는 처음부터 앱을 시작하는 것과는 대조적으로 시스템이 시스템을 죽인 후 앱을 다시 시작하면 어떻게됩니까? – bencallis

답변

0

하나의 아이디어을, 그렇지 않으면


  • setListShown(false);
    setListAdapter(null); 할 , 활동 만 수행하면 시스템은 활동의 마지막 상태를 재 작성하려고 시도하고 onRestoreInstanceState(). 이 메소드는 앱이 종료 된 경우에는 호출되지 않으므로 둘 사이의 큰 차이점 중 하나입니다.

    ListActivity는 onRestoreInstanceState()를 재정의합니다. 그것은 계속되기 전에 List가 존재한다는 것을 보장합니다. 목록이 없으면 기본 위치에서 목록이 팽창됩니다.

    onResume()에서 contentView를 설정하면 onCreate()로 이동하려고하면 문제가 해결 될 수 있습니다.

    내가 activty의 코드를 보면 더 많은 도움을 줄 수 있습니다.

  • 0

    정말 도움이 될지 모르겠지만이 다음 방법은 자녀 기반의 목록 크기를 조정해야합니다. 난 때때로 notifyDataSetChanged()가 작동하지 않는 이유를 모르겠다. 목록은 단순히 변경되지 않지만 대신이 문제를 해결하면 내 문제가 해결된다.

    public static class Utility { 
        public static void setListViewHeightBasedOnChildren(ListView listView) { 
         ListAdapter listAdapter = listView.getAdapter(); 
         if (listAdapter == null) { 
          // pre-condition 
          return; 
         } 
    
    
         int totalHeight = 0; 
         int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); 
         for (int i = 0; i < listAdapter.getCount(); i++) { 
          View listItem = listAdapter.getView(i, null, listView); 
          listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
          totalHeight += listItem.getMeasuredHeight(); 
         } 
    
         ViewGroup.LayoutParams params = listView.getLayoutParams(); 
         int newHeight = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
         if (newHeight >= 100) 
          params.height = newHeight; 
         else 
          params.height = 100; 
         listView.setLayoutParams(params); 
         listView.requestLayout(); 
        } 
    } 
    

    희망 하시겠습니까?

    +0

    나는 이것을 시도 할 것이다. notifydatasetchange가 아닌 이것을 그냥 부르겠습니까? – bencallis

    +0

    예, 그게 내가 한 일입니다. 그것은 나를 위해 일했습니다. 그 발췌 부분을 어디에서 가져 왔는지는 알 수 없습니다. 그것은 제 일이 아닙니다. 거기에 좋은 설명이있었습니다. – caiocpricci2

    +0

    http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing/3495908#3495908 이것이 그 것처럼 보입니다. 스크롤 뷰에 목록보기를 넣으려고하지 않을 때 실제로 작동하는 이유를 실제로 볼 수 없습니다. 어쨌든 나는 그것을 시도하고 다시보고 할 것이다. – bencallis

    0

    당신은 목록 어댑터를 설정하기 전에

    • setListShown(false)를 호출하려고 다음 옵션 중 하나 이상을 시도해 볼 수 있습니다. 응용 프로그램 자체가 사망하지 않은 경우 그렇지 않으면

    • 은 (꽤 긴 샷 :

      requestLayout()