Android BaseAdapter基本使用及数据刷新

Android BaseAdapter基本使用及数据刷新

1. 首先是Adapter适配器类:

MyListAdapter.java

public abstract class MyListAdapter extends BaseAdapter {

    private Context context;
    private int listItemId = 0;

    public MyListAdapter(Context context, int listItemId) {
        super();
        this.context = context;
        this.listItemId = listItemId;
    }

    public Context getContext() {
        return context;
    }

    private List list = new ArrayList();

    public void add(T item) {
        list.add(item);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        list.remove(position);
        notifyDataSetChanged();
    }

    public void removeLast() {
        remove(getCount() - 1);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(listItemId, null);
            System.out.println("convertView=null");
        }
        initListItem(position, convertView, parent);
        return convertView;
    }

    protected abstract void initListItem(int position, View listItem, ViewGroup parent);

}
主界面布局文件:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button 
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="add"
            />
        <Button 
            android:id="@+id/btn_remove"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="remove"
            android:layout_weight="1"
            />
    LinearLayout>

LinearLayout>
然后是业务代码实现:

MainActivity.java

public class MainActivity extends ListActivity implements OnClickListener{

    private MyListAdapter adapter;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new MyListAdapter(MainActivity.this, 
                android.R.layout.simple_list_item_1) {

            @Override
            protected void initListItem(int position, View listItem, ViewGroup parent) {
                ((TextView)listItem).setText(getItem(position));
                System.out.println(getItem(position));
            }
        };

        setListAdapter(adapter);

        for (index = 0; index < 5; index++) {
            adapter.add("eoe" + index);
        }

        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_remove).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_add:
            index ++;
            adapter.add("eoe" + index);
            break;
        case R.id.btn_remove:
            adapter.removeLast();
            break;
        }
    }

}

你可能感兴趣的:(Android)