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 adapter
public 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;
}
@Override
public int getCount() {
Log.d("size of a",String.valueOf(articleList.size()+1));
return articleList!=null ? articleList.size()+1 : 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public int getViewTypeCount(){
return 2;
}
@Override
public int getItemViewType(int position){
if(position % 4 == 0){
return 1;}else{return 0;}
}
@Override
public 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;
}
@Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
}
You can see this adapter class for reference and solve that issue
public class ListAdapter extends BaseAdapter {
ArrayList listcontent;
Context context;
String heading;
Integer[] ivQuestion1={R.drawable.ch1_q1,R.drawable.ch1_q2,R.drawable.ch1_q3,R.drawable.ch1_q4,
R.drawable.ch1_q5,R.drawable.ch1_q6, R.drawable.ch1_q7};
Integer[] ivQuestion2={R.drawable.ch2_q1,R.drawable.ch2_q2,R.drawable.ch2_q3,R.drawable.ch2_q4,
R.drawable.ch2_q5,R.drawable.ch2_q6};
Integer[] ivQuestion3={R.drawable.ch3_q1,R.drawable.ch3_q2,R.drawable.ch3_q3,R.drawable.ch3_q4,
R.drawable.ch3_q5,R.drawable.ch3_q6, R.drawable.ch3_q7};
Integer[] ivQuestion4={R.drawable.ch4_q1,R.drawable.ch4_q2,R.drawable.ch4_q3,R.drawable.ch4_q4,
R.drawable.ch4_q5,R.drawable.ch4_q6};
public ListAdapter(Context context,ArrayList listcontent,String heading) {
this.context = context;
this.listcontent=listcontent;
this.heading=heading;
//listcontent = mdatabase.Get_ContactDetails(firstchar);
}
@Override
public int getCount() {
return listcontent.size();
}
@Override
public Object getItem(int position) {
return listcontent.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
class ViewHolder {
ImageView img;
ImageView btnAudio,btnVideo;
}
@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_content, null);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) view.findViewById(R.id.ivQuestion);
//viewHolder.btnAudio = (ImageView) view.findViewById(R.id.btnAudio);
viewHolder.btnVideo = (ImageView) view.findViewById(R.id.btnVideo);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if(heading.equals("Breezy_1")) {
viewHolder.img.setImageResource(ivQuestion1[position]);
}else if(heading.equals("Breezy_2")) {
viewHolder.img.setImageResource(ivQuestion2[position]);
}else if(heading.equals("Breezy_3")) {
viewHolder.img.setImageResource(ivQuestion3[position]);
}else if(heading.equals("Breezy_4")) {
viewHolder.img.setImageResource(ivQuestion4[position]);
}else{
System.out.print("error");
}
viewHolder.btnVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos=position+1;
String path="/data/data/com.pantherpublishers.breezy/files/"+heading+"/Q and A/A"+pos+".mp4";
Intent i=new Intent(context,MediaPlayerActivity.class);
i.putExtra("videoPath",path);
context.startActivity(i);
//Toast.makeText(context, "video"+position, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}