Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add item at the End listview android

Here is an example that presents dynamic listview get values from list1. Now I would like to add item at end position(at last).means not overwrite item when we insert. It will be paging of item. Please anybody help me?

package com.list.viewer;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/** http://www.android-codes-examples.blogspot.com/ */
public class ListViewExample extends Activity {

    private String[] list1 = { "Icon", "Icon Creator", "Image", "Image Creator" };

    private ListView lister1;

    Button btn_add;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lister1 = (ListView) findViewById(R.id.ListView1);
    btn_add =(Button)findViewById(R.id.btn_add);
    String[] list2 = { "nikunj", "patel", "hi", "this" };
    ItemsAdapter itemsAdapter = new ItemsAdapter(ListViewExample.this,
            R.layout.list, list2);
    lister1.setAdapter(itemsAdapter);
    btn_add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            String[] list2 = { "nikunj", "patel", "hi", "this" };
            ItemsAdapter itemsAdapter = new ItemsAdapter(ListViewExample.this,
                    R.layout.list, list2);
            lister1.setAdapter(itemsAdapter);

        }
    });

        /*lister1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int positon, long arg3) {
                String[] lister = null;
                switch (positon) {
                case 0:
                    lister = list21;
                    break;
                case 1:
                    lister = list22;
                    break;
                case 2:
                    lister = list23;
                    break;
                case 3:
                    lister = list24;
                    break;
                default:
                    lister = list21;
                }
                lister2.setAdapter(new ArrayAdapter<String>(
                        ListViewExample.this,
                        android.R.layout.simple_list_item_1, lister));
            }
        });*/
    }

    private class ItemsAdapter extends BaseAdapter {
        String[] items;

        public ItemsAdapter(Context context, int textViewResourceId,
                String[] items) {
            this.items = items;
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            System.out.println("position is  "+position);
            TextView mDescription;
            View view = convertView;
            //ImageView image;
            if (view == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.list, null);
            }
            if(position == 3)
            {
                mDescription = (TextView) view.findViewById(R.id.desc);
                mDescription.setText(items[position]);
            }else
            {
                mDescription = (TextView) view.findViewById(R.id.desc);
                mDescription.setText(items[position]);

            }
            //image = (ImageView) view.findViewById(R.id.image);

            //image.setBackgroundResource(R.id.image);
            return view;
        }

        public int getCount() {
            return items.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }
    }
}
like image 651
soham Avatar asked Apr 26 '11 03:04

soham


1 Answers

You can add a "footer view" which will always show at the bottom of your list:

View footerView = getLayoutInflater().inflate(R.layout.footer_layout, lister1, false);
lister1.addFooterView(footerView);
like image 139
dmon Avatar answered Oct 12 '22 04:10

dmon