在我将 getcount 中的数组大小设置为数组大小 +1 之前,listview android 中没有显示任何数据

No data displayed in listview android till i set the array size in getcount to array's size +1

所以我在 android 中使用列表视图,其中有两个部分使用自定义适配器。 我正在从网络服务获取数据。 如果我将 getcount() 中的数组大小设置为 array.size()+1,它就会工作 但是当快速滚动我的列表时,我得到了越界异常。 对您的通讯感兴趣。

我的观点

<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"
    />

我的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
}

我的自定义适配器

 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();
}

}

在您的适配器 class 中,在 articles.size() + 1 个位置,您没有任何对象,因为您的文章数组列表大小仅为 articles.size()。 在定义适配器之前在数组列表中添加额外的项目。

您可以查看此适配器 class 以供参考并解决该问题

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;
}
}