ホームページ  >  に質問  >  本文

android - Error: java.lang.IndexOutOfBoundsException: Invalid index 2

How to remove the seperator line in footerLayout? I have a footerLayout below the listView, used to display the totalAmount as shown below. If I click the seperator line in footerLayout, my app crashed.

My MainActivity

AllAdapter obj = new AllAdapter(getApplication(), search, listview,imageView,text,button);
footerLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.under_listview, null);
totalAmount = (TextView) footerLayout.findViewById(R.id.amount);

LogCat error

 java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
            at java.util.ArrayList.get(ArrayList.java:304)
            at com.example.tony.monthlyexpenses.adapter.AllAdapter.getItem(AllAdapter.java:61)
            at com.example.tony.monthlyexpenses.QuickExpenses$1.onItemClick(QuickExpenses.java:88)
            at android.widget.AdapterView.performItemClick(AdapterView.java:301)

The error pointed to listView onClickListener

 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
                mClickedPosition = position;
                Expenses o = (Expenses) obj.getItem(position);
                String day = o.getDate();
            }
        });

AllAdapter

 public Expenses getItem(int position) {
        return search.get(position);
    }

The footerLayout is supposed to be displayed outside the listView, not inside. How can I get rid of this ?

I also have activity_main.xml, AllAdapter class, all_adapter.xml for ListView and also under_listview.xml for the footerLayout.

activity_main

AllAdapter

under_listview

How to move the footerLayout out from the ListView ?

I add android:footerpidersEnabled="false" now become like this

But still clickable !!!

谁知道问题出在哪?

footerLayout被按时如何不出现灰色?

PHP中文网PHP中文网2742日前559

全員に返信(4)返信します

  • ringa_lee

    ringa_lee2017-04-17 18:03:43

    は非常に単純ですが、間違いが発生しやすい問題です。footer を追加すると、listview item の量は 3 になりますが、adapterviewcount は実際には 3 にはなりません。フッターをクリックすると、obj.getItem(2) が実行されます。これは配列の範囲外の例外である必要があります。 对于添加了header或footer的listview、アイテムを取得する正しい方法は

    である必要があります リーリー

    header或footer属于AdapterView的子viewlistView.getAdapter().getItem(position); は、2 の位置を取るときに境界を越えないことを保証し、オブジェクトを null 判定します。

    返事
    0
  • 天蓬老师

    天蓬老师2017-04-17 18:03:43

    footview のクリックイベントとして setOnItemClickListener を使用することはできません。
    例: footview.setonClickListener(new OnClickListener{}); 頑張ってください。

    返事
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 18:03:43

    配列の添字が範囲外です。配列のサイズは 2 なので、対応する添字は 0 と 1 のみですが、使用時に 2 が使用されたことが示されています。インデックスが 2 であることを示しています。 61 行目と 88 行目を自分で調べて、インデックスが 2 である場所があるかどうかを確認してください。これは

    と呼ばれます。

    返事
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 18:03:43

    footerLayoutlistView から移動することは

    と書きます。

    listview.addFooterView(footerLayout, null, false);

    返事
    0
  • キャンセル返事