zoukankan      html  css  js  c++  java
  • 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;
    
            }
        }
    }
  • 相关阅读:
    [FJOI2016] 建筑师
    AtCoder
    [Poetize6] IncDec Sequence
    CodeForces
    洛谷 P4551 最长异或路径
    WC 2018/CTSC 2018/APIO 2018 游记
    洛谷 P4538 收集邮票
    「PKUWC 2018」随机算法 (60分部分分做法)
    bzoj 3718: [PA2014]Parking
    bzoj 1023: [SHOI2008]cactus仙人掌图 2125: 最短路 4728: 挪威的森林 静态仙人掌上路径长度的维护系列
  • 原文地址:https://www.cnblogs.com/xianfeng-zhang/p/8241504.html
Copyright © 2011-2022 走看看