BaseAdapter实现的投票案例

BaseAdapter实现的投票案例

1.知识补充

  •       android:descendantFocusability="blocksDescendants",关键是让谁先去获取焦点
  1. beforeDescendants:viewgroup会优先其子类控件而获取到焦点
  2. afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
  3. blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

2.案例:投票案例

2.2目录结构

2.2参考代码

1)toupaio_item.xml子列表项代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants">
<!--
    android:descendantFocusability="blocksDescendants"
    beforeDescendantsviewgroup会优先其子类控件而获取到焦点
    afterDescendantsviewgroup只有当其子类控件不需要获取焦点时才获取焦点
    blocksDescendantsviewgroup会覆盖子类控件而直接获得焦点

-->

    <LinearLayout
        android:id="@+id/toupiao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/vusername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="张三"
            />
        <ProgressBar
            android:id="@+id/vpro"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:max="100"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            />
        <Button
            android:id="@+id/vbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="投票"
            />
    </LinearLayout>



</LinearLayout>

2)activity_vote_list_view.xml主布局文件代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".VoteListViewActivity">
    <ListView
        android:id="@+id/toupiao_listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

3)VoteListViewActivity代码

  • 包含Student类
  • 包含StudentAdapter(继承BaseAdapter)
//投票的案例
public class VoteListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vote_list_view);
        //1.获取ListView
        ListView listView=findViewById(R.id.toupiao_listView);
        //2.创建数据集合(线性的和ArrayList都是可以的)
        LinkedList<Student> list=new LinkedList<>();
        list.add(new Student("张三",0));
        list.add(new Student("李四",0));
        list.add(new Student("王五",0));
        //3.创建StudnetAdapter(需要传入数据集合和Context上下文对象)
        StudnetAdapter adapter=new StudnetAdapter(list,this);
        //4.绑定适配器
        listView.setAdapter(adapter);
    // 5.创建事件监听
    listView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              ProgressBar progressBar=view.findViewById(R.id.vpro);
              TextView text=view.findViewById(R.id.vusername);
              int tcount=progressBar.getProgress();//获取进度就是获取当前的票数
              String tname=text.getText().toString();
              String str=tname+"票数为"+tcount;
              //结合页面跳转显示信息
              /* 省略,采用的是Toast显示当前投票人的信息*/
              Toast.makeText(VoteListViewActivity.this, ""+str, Toast.LENGTH_SHORT).show();

          }
        });
    }


//    创建BaseApdapter
    private class StudnetAdapter extends BaseAdapter {

        private Context context;
        private LinkedList<Student> listStudent;

    public StudnetAdapter(LinkedList<Student> listStudent, Context context) {
        this.context=context;
        this.listStudent=listStudent;
    }

    /*
    * 显示列表的项数
    * */
    @Override
    public int getCount() {
        return listStudent.size();
    }

    /*
    *返回的是第几个列表项的内容
    * */
    @Override
    public Object getItem(int position) {
        return listStudent.get(position);
    }

    /*
    * 返回第几个的id,其实就是position的值
    * */
    @Override
    public long getItemId(int id) {
        return id;
    }

    /*
    * 核心方法,控制每一项显示的内容
    * 结果:生成一个View对象的ContentView,这个View就是ListView
    *关键是给view赋成当前的列表项
    *
    * */
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        view=LayoutInflater.from(context).inflate(R.layout.toupaio_item,null,false);
        ProgressBar progressBar=view.findViewById(R.id.vpro);
        TextView text=view.findViewById(R.id.vusername);
        Button button=view.findViewById(R.id.vbutton);
        text.setText(listStudent.get(position).getName());
        progressBar.setProgress(listStudent.get(position).getCount());
        //设置投票的事件监听器
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listStudent.get(position).setCount(listStudent.get(position).getCount()+1);
                progressBar.setProgress(listStudent.get(position).getCount()+1);

            }
        });



        return view;
    }
}
    //  学生类
    protected class Student{
        private String name;
        private int count;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public Student(String name, int count) {
            this.name = name;
            this.count = count;
        }

    }
}

2.3效果图

图1 投票前

图2 投票后

图3 查看投票信息

3.扩展

  • 常见的适配器有ArrayAdapter,只能放一下简单的文本数据。
  • SimpleAdapter继承于BaseAdapter,基于HashMap,方便写,不适合复杂结构的
  • BaseAdapter适合复杂的结构,不过需要单独写一个类继承BaseAdaper并重写相应的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简单点了

谢谢大佬

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值