向页面视图

时间:2016-02-29 12:41:19

标签: android listview footer indexoutofboundsexception

我有一个listview,当使用onscroll listener达到20rh listitem时会加载更多的listitems

我想添加一个页脚,说明在加载更多列表项时加载更多

当我到达第20个列表项时,应用程序崩溃说idex超出了logct

这是我的代码

logcat的

  

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  致命例外:主02-29 15:53:11.546 23342 23342 E
  AndroidRuntime
  java.lang.IndexOutOfBoundsException:索引9无效,大小为0 02-29   15:53:11.546 23342 23342 E AndroidRuntime
  在   java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在java.util.ArrayList.get(ArrayList.java:304)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在com.enlightenme.pac.FinalAdapter.getItem(FinalAdapter.java:55)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在com.enlightenme.pac.FinalAdapter.getView(FinalAdapter.java:83)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在   com.baoyz.swipemenulistview.SwipeMenuAdapter.getView(SwipeMenuAdapter.java:65)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.AbsListView.obtainView(AbsListView.java:2588)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.ListView.makeAndAddView(ListView.java:1840)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.ListView.fillDown(ListView.java:681)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.ListView.fillSpecific(ListView.java:1339)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.ListView.layoutChildren(ListView.java:1639)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.AbsListView.onLayout(AbsListView.java:2427)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.View.layout(View.java:14938)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.ViewGroup.layout(ViewGroup.java:4601)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.FrameLayout.onLayout(FrameLayout.java:448)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.View.layout(View.java:14938)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.ViewGroup.layout(ViewGroup.java:4601)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.FrameLayout.onLayout(FrameLayout.java:448)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.View.layout(View.java:14938)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.ViewGroup.layout(ViewGroup.java:4601)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1694)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1552)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.LinearLayout.onLayout(LinearLayout.java:1465)02-29   15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.View.layout(View.java:14938)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.ViewGroup.layout(ViewGroup.java:4601)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.widget.FrameLayout.onLayout(FrameLayout.java:448)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.View.layout(View.java:14938)02-29 15:53:11.546 23342   23342 E AndroidRuntime at   android.view.ViewGroup.layout(ViewGroup.java:4601)02-29 15:53:11.546   23342 23342 E AndroidRuntime at   android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2212)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2026)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1236)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5160)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:791)   02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.Choreographer.doCallbacks(Choreographer.java:591)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.view.Choreographer.doFrame(Choreographer.java:561)02-29   15:53:11.546 23342 23342 E AndroidRuntime
  在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:777)

     

02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在android.os.Handler.handleCallback(Handler.java:725)02-29   15:53:11.546 23342 23342 E AndroidRuntime
  在android.os.Handler.dispatchMessage(Handler.java:92)02-29   15:53:11.546 23342 23342 E AndroidRuntime
  在android.os.Looper.loop(Looper.java:176)02-29 15:53:11.546 23342   23342 E AndroidRuntime at   android.app.ActivityThread.main(ActivityThread.java:5365)02-29   15:53:11.546 23342 23342 E AndroidRuntime
  at java.lang.reflect.Method.invokeNative(Native Method)02-29   15:53:11.546 23342 23342 E AndroidRuntime
  at java.lang.reflect.Method.invoke(Method.java:511)02-29 15:53:11.546   23342 23342 E AndroidRuntime at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)   02-29 15:53:11.546 23342 23342 E AndroidRuntime
  在

我的活动

public class InterActivity extends Activity 
{

SwipeMenuListView listview;
//ListView listview;

View mFooterView;






@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.inter_layout);
    shrdPreference = new SharedPreference();


    //Execute RemoteDataTask AsyncTask

    new RemoteDataTask().execute();
}




private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();


        mProgressDialog = new ProgressDialog(InterActivity.this);
        mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Set progressdialog title
        //mProgressDialog.setTitle("BEGINNER CODES");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading. Please wait loading ....");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.anim.progress_handler));
        // Show progressdialog
        mProgressDialog.show();

    }


    @Override
    protected Void doInBackground(Void... params) {
        // my do in backgroung process

    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
    //  listview = (ListView) findViewById(R.id.inter_layoutListView);

        listview = (SwipeMenuListView) findViewById(R.id.inter_layoutListView);
        // Pass the results into ListViewAdapter.java
        adapter = new FinalAdapter(InterActivity.this,
                                      codelist);
         mFooterView = LayoutInflater.from(InterActivity.this).inflate(R.layout.footer_layout, null);                        

        // Binds the Adapter to the ListView
    listview.setAdapter(adapter);


        // Close the progressdialog
        mProgressDialog.dismiss();

//设置onscrll侦听器

        listview.setOnScrollListener(new OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view,
                                                 int scrollState) { // TODO Auto-generated method stub
                    int threshold = 1;
                    int count = listview.getCount();

                    if (scrollState == SCROLL_STATE_IDLE) {
                        if (listview.getLastVisiblePosition() >= count
                            - threshold) {
                            // Execute LoadMoreDataTask AsyncTask
                            new Loadmore().execute();
                        }
                    }
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                                     int visibleItemCount, int totalItemCount) {
                    // TODO Auto-generated method stub

                }

            });




    }
}





private class Loadmore extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    listview.addFooterView(mFooterView);

        }
    @Override
    protected Void doInBackground(Void... params) {
        // my load mmore data process


    }

    @Override
    protected void onPostExecute(Void result) {
        int position = listview.getLastVisiblePosition();



    adapter.notifyDataSetChanged();
        listview.removeFooterView(mFooterView);


listview.setSelectionFromTop(position, 0);


    }
}

   }    

1 个答案:

答案 0 :(得分:1)

您正在更改适配器中的数据,它的大小将变为零。正确看看Loadmore AsyncTask,它可能会改变适配器的大小