>  Q&A  >  본문

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일 전561

모든 응답(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的子view, listView.getAdapter().getItem(position);은 2의 위치를 ​​취할 때 경계를 넘지 않는다는 것을 확인한 후 객체 무효 판단을 내릴 수 있습니다.

    회신하다
    0
  • 天蓬老师

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

    footview의 클릭 이벤트로 setOnItemClickListener를 사용할 수는 없습니다.
    예를 들어 footview.setonClickListener(new OnClickListener{});

    회신하다
    0
  • 伊谢尔伦

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

    배열 첨자가 범위를 벗어났습니다. 따라서 해당 첨자는 0과 1만 가능하지만 이를 사용할 때 2를 사용했습니다. 오류는 잘못된 인덱스가 있음을 나타냅니다. 61행과 88행을 직접 확인하여 인덱스가 2인 곳이

    이라고 불리는 곳이 있는지 확인하세요.

    회신하다
    0
  • 伊谢尔伦

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

    footerLayout에서 listView를 이동하려면

    로 씁니다.

    listview.addFooterView(footerLayout, null, false);

    회신하다
    0
  • 취소회신하다