错误:java.lang.IndexOutOfBoundsException:索引2无效,大小为2

时间:2017-01-05 06:56:20

标签: java android listview

如何删除footerLayout中的分隔线?我在footerLayout下面有listView,用于显示totalAmount,如下所示。如果我点击footerLayout中的分隔线,我的应用就会崩溃。

enter image description here

我的主要活动

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错误

 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)

错误指向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);
    }

footerLayout应该显示在listView之外,而不是在里面。我怎么能摆脱这个?

对于activity_main.xml我也有AllAdapterall_adapter.xml类,ListViewunder_listview.xml也有footerLayout

activity_main

AllAdapter

under_listview

  

如何从ListView中移出footerLayout?

我添加android:footerDividersEnabled="false"现在变得像这样

enter image description here

但仍然可点击!!!

enter image description here

4 个答案:

答案 0 :(得分:2)

  

footerLayout应该显示在listView之外,而不是   内部。

实际上,页脚也是ListView的一部分,它总计了列表中的项目数。从来没有,有一些方法可以忽略页脚视图上的点击事件。

一种选择是使用以下方式添加视图:
addFooterView(footerLayout);
用途:
addFooterView(footerLayout, null, false);

第三个参数指定是否页脚视图should be selectable

另一个选择是当position参数大于适配器数据集的大小时忽略单击:

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

答案 1 :(得分:1)

尝试在列表视图中设置以下行。

            android:footerDividersEnabled="false"

答案 2 :(得分:1)

我更改了此代码,现在工作正常。

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

答案 3 :(得分:0)

如果您不熟悉自定义ListViewAdapters,只需一个快速教程。

我会像这样对象Expense

class Expense {

  Date date = new Date();
  Double spendMoney = 5.0;
  Bitmap image;
  ...

  /** Continue with POJO **/
}

expenses_list_item.xml布局。 (如果你需要帮助,我可以稍后再添加......)


在我的MainActivity中,我会有一个ArrayList<Expense> expensesList 我的自定义 ArrayListAdapter看起来像这样:

class expenseListAdapter extends ArrayAdapter<Expense> {

    expenseListAdapter() {
        super(context, R.layout.expenses_list_item, expensesList);
    }

    @Override
    public View getView(int pos, View view, ViewGroup parent) {
        if (view == null) {
            view = getActivity().getLayoutInflater().inflate(R.layout.list_item_series, parent, false);
        }

        ShowListItem current = expensesList.get(pos);

        TextView expens = (TextView) view.findViewById(R.id.expenseText);
        expens.setText(current.getSpendMoney());

        ... (all other needed values of your Expense object)

        return view;
    }
}

最后,我可以设置

ArrayAdapter<Expense> adapter = new expenseListAdapter();
expenseListView.setAdapter(adapter);

那就是它。第一眼看上去可能有点复杂,但这种方法非常强大。如果您需要帮助,请不要害怕;)

电贺 马立克