0

我需要listview幫助我正在使用一個片段。該應用程序從API讀取數據,我的代碼工作正常。我最初加載15個項目,下次我加載10個項目。但是,如果對API的請求返回少於15個項目,則不起作用。另外,當項目數量不是15或25或35的倍數時,應用程序失敗。這是因爲我在初始設置後加載了10個項目。ListView - 使用滾動加載更多項目

我需要修改我的代碼以使其可以處理任意數量的列表項。

我的代碼如下:

(ListaFragment.java) - >這裏是ListFragment

public class ListaFragment extends ListFragment implements OnScrollListener { 
    public ListaFragment(){} 
    View rootView; 

    ListAdapter customAdapter = null; 
    ListaLugares listaLugares;  

    // values to pagination 
    private View mFooterView; 
    private final int AUTOLOAD_THRESHOLD = 4; 
    private int MAXIMUM_ITEMS; 
    private Handler mHandler; 
    private boolean mIsLoading = false; 
    private boolean mMoreDataAvailable = true; 
    private boolean mWasLoading = false; 

    public int ventana = 0; 
    public int nInitial; 

    private Runnable mAddItemsRunnable = new Runnable() { 
     @Override 
     public void run() { 
      customAdapter.addMoreItems(10); 
      mIsLoading = false; 

     } 
    }; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     rootView = inflater.inflate(R.layout.fragment_lista, container, false); 

     if (container != null) { 
      container.removeAllViews(); 
     } 

     ((MainActivity) getActivity()).setBar(); 


     // read number of places of a category 
     listaLugares.readNumberOfPlaces(); 
     // set 
     setNumbersOfItems(); 
     // read the places a insert in a List 
     listaLugares.readPlaces(15, ventana, listaLugares.idCategoria); 
     //ventana = ventana + 25; 

     return rootView; 
    } 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     listaLugares = ((ListaLugares)getActivity().getApplicationContext()); 
    } 


    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     final Context context = getActivity(); 
     mHandler = new Handler(); 
     customAdapter = new ListAdapter(context, listaLugares.lista); 

     mFooterView = LayoutInflater.from(context).inflate(R.layout.loading_view, null); 
     getListView().addFooterView(mFooterView, null, false); 
     setListAdapter(customAdapter); 
     getListView().setOnScrollListener(this); 
    } 


    public void setNumbersOfItems() { 
     if (listaLugares.totalItems > 100) { 
      MAXIMUM_ITEMS = 100; 
      nInitial = 25; 
     } else { 
      MAXIMUM_ITEMS = listaLugares.totalItems; 
      nInitial = listaLugares.totalItems; 
     } 
     Log.v("NUMBER OF ITEMS", "Number: " + MAXIMUM_ITEMS + "-"+ nInitial); 
    } 



    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     //Intent myIntent = new Intent(getActivity(), DetalleActivity.class); 
     //myIntent.putExtra("param_id", appState.lista.get(position).id); 
     //getActivity().startActivity(myIntent); 
    } 


    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, 
         int visibleItemCount, int totalItemCount) { 

     if (!mIsLoading && mMoreDataAvailable) { 
      if (totalItemCount >= MAXIMUM_ITEMS) { 
       mMoreDataAvailable = false; 
       getListView().removeFooterView(mFooterView); 
      } else if (totalItemCount - AUTOLOAD_THRESHOLD <= firstVisibleItem + visibleItemCount) { 
       ventana = ventana + 10; 
       listaLugares.readPlaces(10, ventana, listaLugares.idCategoria); 
       mIsLoading = true; 
        mHandler.postDelayed(mAddItemsRunnable, 1000); 
      } 
     } 
    } 


    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // Ignore 
    } 


    @Override 
    public void onStart() { 
     super.onStart(); 
     if (mWasLoading) { 
      mWasLoading = false; 
      mIsLoading = true; 
      mHandler.postDelayed(mAddItemsRunnable, 1000); 
     } 
    } 


    @Override 
    public void onStop() { 
     super.onStop(); 
     mHandler.removeCallbacks(mAddItemsRunnable); 
     mWasLoading = mIsLoading; 
     mIsLoading = false; 
     ventana = ventana; 
    } 

    @Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
    } 

(ListAdapter.java) - >方法適配器類添加更多的項目。

public class ListAdapter extends ArrayAdapter<Lugar> { 
Context context; 
List<Lugar> values; 

ListaLugares listaLugares; 

private int mCount = 20; 


public ListAdapter(Context context, List<Lugar> values) { 
    super(context, R.layout.row, values); 
    this.context = context; 
    this.values = values; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    row = inflater.inflate(R.layout.row, parent, false); 
    TextView firstText = (TextView) row.findViewById(R.id.titulo); 
    TextView secondText = (TextView) row.findViewById(R.id.categoria); 

    firstText.setText(values.get(position).nombre); 
    secondText.setText(values.get(position).categoriaNombre); 

    return row; 
} 


public void addMoreItems(int count) { 
    mCount += count; 
    notifyDataSetChanged(); 
} 

@Override 
public int getCount() { 
    return mCount; 
} 


@Override 
public long getItemId(int position) { 
    return position; 
} 

} 
+0

請僅在相關問題中包含「相關代碼」,請參閱http://sscce.org/ – albusshin

+0

我使用這個http://benjii.me/2010/08/endless-scrolling-listview-in-android/ – njzk2

+0

好的,我已經編輯了ListAdapter中的代碼,但我無法總結ListaFragment,因爲所有代碼都很重要。感謝您的建議;) – Yeray

回答

1

我加入了以前的編輯過的版本來看一些代碼。

第一個調用起作用,因爲您在initiaItems上的值爲15,並且您在ListAdapter的構造函數上將其設置爲mCount

public ListAdapter(Context context, List<Lugar> values, int count) { 
    super(context, R.layout.row, values); 
    this.context = context; 
    this.values = values; 
    this.mCount = count; 
} 

所以當機器人呈現列表視圖,它的存取權限的功能ListAdapter.getCount(),並返回mCount(15)。

但是,當你滾動它可以打電話

public void addMoreItems(int count, int idCategoria) { 
    appState = ((ListaLugares)getContext().getApplicationContext()); 
    appState.readPlaces(15, mCount, idCategoria); 
    mCount += count; 
    notifyDataSetChanged(); 
} 

如果由appState.readPlaces返回的項目數是未知的,爲什麼要補充一個數字來mCount mCount += count;

如果appState.readPlaces返回14,count是15當列表視圖呈現它會假設有15 + 15項目,當15 + 14,所以最後一個項目將崩潰。

mCount應該從保存API調用數據的對象中獲取長度,在你的情況下,我認爲它將是appState.lista

+0

我修改了我的代碼,但無法解決我的問題。我的新代碼有問題。我遵循下一個教程:http://www.informit.com/articles/article.aspx?p=2066699&seqNum=4 – Yeray

+1

如果我們有值列表怎麼樣'返回(mCount> values.size())? values.size():mCount'而不是'return mCount' – hypd09