2

I have a navigation drawer which gets populated from strings and by using their position i am handling which activity to launch i just want listview last item should come at the footer of the listview.

I used setFooter property but it is not working.

What is the way to do it?

Here is the code:

class MyAdapter extends BaseAdapter {
    private Context context;
    String[] navlist;
    int[] images = { R.drawable.job, R.drawable.career, R.drawable.project,
            R.drawable.facebook, R.drawable.games, R.drawable.mail, R.drawable.rate };

    public MyAdapter(Context context) {
        this.context = context;
        navlist = context.getResources().getStringArray(R.array.navdra);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return navlist.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return navlist[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.customrow, parent, false);
        }

        else {
            row = convertView;
        }
        TextView titleTextView = (TextView) row.findViewById(R.id.textView11);
        ImageView titleImageView = (ImageView) row
                .findViewById(R.id.imageView1);
        titleTextView.setText(navlist[position]);
        titleImageView.setImageResource(images[position]);

        return row;
    }

}

ListView and ArrayAdapter code :

listView = (ListView) findViewById(R.id.drawerlist);
        myAdapter = new MyAdapter(this);

        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(this);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerListner = new ActionBarDrawerToggle(this, drawerLayout,
                R.drawable.ic_drawer, R.string.dopen, R.string.dclose);

        drawerLayout.setDrawerListener(drawerListner);
Puneet Kushwah
  • 1,495
  • 2
  • 17
  • 35

2 Answers2

2

Please see the sample code to set the footer to a ListView

mListview = (ListView) mContentView.findViewById(R.id.myListView);

// Inflating header and footer view to ListView
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header_item, mListview,false);
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer_item,mListview,false);

// Adding view to mlist header and footer
mListview.addHeaderView(header, null, false);
mListview.addFooterView(footer, null, false);

// Initializing the adapter
ListAdapter adapter = new ListAdapter(getNGAActivity(), myData);

// Setting the listview adapter
mListview.setAdapter(adapter);

I hope the code comments are self explanatory

Nipun
  • 990
  • 1
  • 16
  • 25
MRX
  • 1,400
  • 3
  • 12
  • 32
  • Thats Okay ... but I guess this is all you need ... can you brief what else do you need ... addFooterView will add a footer to listview thats all – MRX Feb 12 '15 at 11:48
  • Okay after that how to add a item which is at 6th position of that list view....? – Puneet Kushwah Feb 12 '15 at 11:54
  • You have all the values which you want to show in context.getResources().getStringArray(R.array.navdra) right ? – MRX Feb 12 '15 at 12:02
  • use only n-1 items @Override public int getCount() { // TODO Auto-generated method stub return navlist.length -1 ; } from your array or create a string value for your last item and use that in your footer to add . I am guessing you only have Strings to show in listview – MRX Feb 12 '15 at 12:03
  • @Puneet Kushwah Please upvote if you found the info provided by me useful :P – MRX Feb 12 '15 at 12:07
1

There is no method like "setfooter" for listview. You can inflate your footer layout and add it as footer to listview by using below code.

View footerView = ((LayoutInflater)ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);

ListViewObject.addFooterView(footerView);

see this answer How to add a footer in ListView?

Hope it will help.thanks.

Community
  • 1
  • 1
Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36