Android基础控件ListView基础操作

1、简介

  基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作:

    public boolean add(E e) {//添加数据
        throw new RuntimeException("Stub!");
    }
    public void add(int index, E element) {//通过索引添加数据
        throw new RuntimeException("Stub!");
    }
    public boolean remove(Object o) {//移除数据
        throw new RuntimeException("Stub!");
    }
    public E remove(int index) {//通过索引移除数据
        throw new RuntimeException("Stub!");
    }
    public void clear() {//清除所有数据
        throw new RuntimeException("Stub!");
    }
    public void notifyDataSetChanged() {//刷新ListView
        throw new RuntimeException("Stub!");
    }

2、简单使用

  1)添加按钮布局xml文件:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加数据"
            android:id="@+id/addbtn"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="指定位置添加数据"
            android:id="@+id/addbtn1"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除数据"
            android:id="@+id/Remove"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="指定位置删除数据"
            android:id="@+id/Remove1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除所有数据"
            android:id="@+id/clearAll"/>

    </LinearLayout>

  2)在自定义的Adapter.java文件中添加、移除代码:

    public void add(Custom custom){
        if (aData == null){
            aData = new LinkedList<>();
        }
        aData.add(custom);
        notifyDataSetChanged();
    }
    public void add(int position,Custom custom){
        if (aData == null){
            aData = new LinkedList<>();
        }
        aData.add(position,custom);
        notifyDataSetChanged();
    }
    public void remove(Custom custom){
        if (aData !=null){
            aData.remove(custom);
        }
        notifyDataSetChanged();
    }
    public void remove(int postition){
        if (aData !=null){
            aData.remove(postition);
        }
        notifyDataSetChanged();
    }
    public void clear() {
        if(aData != null) {
            aData.clear();
        }
        notifyDataSetChanged();
    }

  3)Java文件的代码:

public class LoginActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,OnClickListener{

    private String[] names = new String[]{"猪八戒","孙悟空","唐僧"};
    private String[] says = new String[]{"饿了","吃俺老孙一棒","紧箍咒"};
    private  int[] images = new int[]{R.drawable.icon,R.drawable.icon,R.drawable.icon};
    private Button btnAdd,addBtn1,removeBtn,removeBtn1,clearBtn;
    private CustomAdapter customAdapter = null;
    private Custom custom_1 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        btnAdd = (Button)findViewById(R.id.addbtn);
        btnAdd.setOnClickListener(this);
        addBtn1 = (Button)findViewById(R.id.addbtn1);
        addBtn1.setOnClickListener(this);
        removeBtn = (Button)findViewById(R.id.Remove);
        removeBtn.setOnClickListener(this);
        removeBtn1 = (Button)findViewById(R.id.Remove1);
        removeBtn1.setOnClickListener(this);
        clearBtn = (Button)findViewById(R.id.clearAll);
        clearBtn.setOnClickListener(this);
        ListView list_test = (ListView) findViewById(R.id.listview);
        final LayoutInflater inflater = LayoutInflater.from(this);
        View headView = inflater.inflate(R.layout.list_header, null, false);
        View footView = inflater.inflate(R.layout.list_header, null, false);

        List<Custom> aData = new LinkedList<Custom>();
        for (int i=0;i<names.length;i++){
            aData.add(new Custom(names[i],says[i],images[i]));
        }
        //添加表头和表尾需要写在setAdapter方法调用之前!!!
        list_test.addHeaderView(headView);
        list_test.addFooterView(footView);

        customAdapter = new CustomAdapter((LinkedList<Custom>)aData,LoginActivity.this);
        list_test.setAdapter(customAdapter);
        list_test.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.addbtn:
                custom_1 = new Custom("沙和尚","呵呵呵",R.drawable.icon);
                customAdapter.add(custom_1);
                break;
            case R.id.addbtn1:
                customAdapter.add(2,new Custom("指定","假的",R.drawable.icon));
                break;
            case R.id.Remove:
                customAdapter.remove(custom_1);
                break;
            case R.id.Remove1:
                //判断是否越界 省略
                customAdapter.remove(2);
                break;
            case R.id.clearAll:
                customAdapter.clear();
                break;

        }
    }
}
原文地址:https://www.cnblogs.com/xianfeng-zhang/p/8241504.html