1

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.

enter image description here

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:footerDividersEnabled="false" now become like this

enter image description here

But still clickable !!!

enter image description here

Hoo
  • 1,806
  • 7
  • 33
  • 66
  • please share the xml – Swas_99 Jan 05 '17 at 06:58
  • @Swas_99 edited. I have 3 xml, one is activity_main, another is adapter, lastly is footer layout xml. Let me know which xml . – Hoo Jan 05 '17 at 07:01
  • So is the issue, the footer is in the list view and the position is greater than the number of objects in obj? Maybe you should just add a check before you do getItem. – matt Jan 05 '17 at 10:08
  • @matt How can I move the footerLayout out from listview ? – Hoo Jan 05 '17 at 10:13
  • @matt this is java. sure we deal with 0 based indicies... – Malik Jan 05 '17 at 13:01
  • @Hoo Do you ave an custom litViewAdapter? If so, could you please provide your full listViewAdapter? – Malik Jan 05 '17 at 13:04
  • @malik The title of the question seems to imply op is not clear about that detail. – matt Jan 05 '17 at 14:09
  • @matt post edited. – Hoo Jan 05 '17 at 15:02
  • 1
    Possible duplicate of [How to avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException?](https://stackoverflow.com/questions/32568261/how-to-avoid-arrayindexoutofboundsexception-or-indexoutofboundsexception) –  Aug 28 '17 at 15:20

4 Answers4

2

The footerLayout is supposed to be displayed outside the listView, not inside.

Actually, the footer is also part of the ListView and it adds up to the number of items the list has. Neverhteless, there are a few approaches how to ignore the click events on a footer view.

One option is instead of adding your view using:
addFooterView(footerLayout);,
use:
addFooterView(footerLayout, null, false);

The third parameter specifies whether the footer view should be selectable or not.

Another option would be to ignore the click when the position parameter is bigger than the size of your adapter dataset:

 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();
          }
     }
 });
Andy Res
  • 15,963
  • 5
  • 60
  • 96
1

Try to set the following line in your listview.

            android:footerDividersEnabled="false"
Parin Parikh
  • 385
  • 1
  • 6
1

I change this code and it works fine now.

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();
                }
            }
        });
Hoo
  • 1,806
  • 7
  • 33
  • 66
0


just a quick tutorial, if you are new to custom ListViewAdapters.

I would have an Object Expense like this:

class Expense {

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

  /** Continue with POJO **/
}

and an expenses_list_item.xml layout. (if you need help with that, i can add it later...)


In my MainActivity I would have an ArrayList<Expense> expensesList and
my custom ArrayListAdapter would look something like this:

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

And finally, I could just set

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

That would be it. Maybe a little bit complicated on the first look, but this method is very powerfull. If you need help, don't be afraid to ask ;)

Greets Malik

Malik
  • 878
  • 2
  • 9
  • 23