I'm working with listview in Android which has two sections using a custom adapter. I'm getting the data from a webservice.it's working if I set the size of the array in getcount() to array.size()+1, but then while fast scrolling through my list I get out of bounds exception.interested in your comms.
my view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:showIn="@layout/app_bar_main"><ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none" android:scrollingCache="false" android:animationCache="false" android:divider="@drawable/divider" android:dividerHeight="5dp" />
my class
list = (ListView) findViewById(R.id.list); adapter = new com.santeplus.santeplusmag.santeplus.ListAdapter(this,articles); list.setAdapter(adapter); MultiScrollListener scrolls = new MultiScrollListener(); scrolls.addScrollListener(new EndlessScrollListener() { @Override public boolean onLoadMore(int page, int totalItemsCount) { // Triggered only when new data needs to be appended to the list // Add whatever code is needed to append new items to your AdapterView customLoadMoreDataFromApi(page); // or customLoadMoreDataFromApi(totalItemsCount); return true; // ONLY if more data is actually being loaded; false otherwise. } }); // Append more data into the adapterpublic void customLoadMoreDataFromApi(int offset) { flag_loading = true; // getting paged data adapter.notifyDataSetChanged(); flag_loading=false; // This method probably sends out a network request and appends new data items to your adapter. // Use the offset value and add it as a parameter to your API request to retrieve paginated data. // Deserialize API response and then construct new objects to append to the adapter}
my Customadapter
public class ListAdapter extends BaseAdapter { private ArrayList<Articles> articleList ;Context context;MainActivity main;ListAdapter(MainActivity main){ this.main = main;}public ListAdapter( MainActivity main, ArrayList<Articles> mData) { this.articleList = mData; this.main = main; this.adsList = adsList;} public ArrayList<Articles> getData() { return articleList;}@Overridepublic int getCount() { Log.d("size of a",String.valueOf(articleList.size()+1)); return articleList!=null ? articleList.size()+1 : 0;} @Overridepublic Object getItem(int position) { return null;} @Overridepublic int getViewTypeCount(){ return 2;}@Overridepublic int getItemViewType(int position){ if(position % 4 == 0){ return 1;}else{return 0;}} @Overridepublic View getView(int position, View convertView, ViewGroup parent) { View v = convertView; String s="" ViewHolderItem holder = new ViewHolderItem(); int type = getItemViewType(position); if (convertView == null) { // Inflate the layout according to the view type LayoutInflater inflater = (LayoutInflater) main.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (type == 0) { // Inflate the layout with the data convertView = inflater.inflate(R.layout.cell, null); holder.title = (TextView) convertView.findViewById(R.id.title); } else { // Inflate the layout with the ad convertView = inflater.inflate(R.layout.fragment_ad, null); holder.adView = (AdView) convertView.findViewById(R.id.adView1); } convertView.setTag(holder); }else { holder = (ViewHolderItem) convertView.getTag(); } if (type == 0) { holder.text1.setText(Html.fromHtml(this.main.articles.get(position).title)); try { s = main.articles.get(position).image; Log.d("url image",s); }catch(UnsupportedEncodingException e){ e.printStackTrace(); } Picasso.with(main) .load(s) .into(holder.image); }if(type == 1) { com.google.android.gms.ads.AdRequest adRequest = new com.google.android.gms.ads.AdRequest.Builder() .build(); holder.adView.loadAd(adRequest); } return convertView;} @Overridepublic void notifyDataSetChanged(){ super.notifyDataSetChanged();}}