在结束listview android添加项目

时间:2011-04-26 03:17:57

标签: android listview

以下示例显示了list1中的动态listview获取值。现在我想在结束位置添加项目(最后)。我们插入时不会覆盖项目。它将是项目的分页。请有人帮帮我吗?

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

2 个答案:

答案 0 :(得分:25)

您可以添加一个“页脚视图”,它始终显示在列表的底部:

View footerView = getLayoutInflater().inflate(R.layout.footer_layout, lister1, false);
lister1.addFooterView(footerView);

答案 1 :(得分:0)

在dataSource中添加新项目时,您将通过

更新适配器
itemsAdapter.notifyDataSetChanged();