Android 用ListView实现排序「建议收藏」

Android 用ListView实现排序「建议收藏」点击“单价”按钮或“数量信息”按钮,可按据升序或降序进行排序。布局没什么好说的在这里插入代码片<LinearLayoutxmlns: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_widt

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

点击“单价”按钮或“数量信息”按钮,可按据升序或降序进行排序。 在这里插入图片描述

布局没什么好说的

在这里插入代码片<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=".shangpingActivity" android:orientation="vertical">

    <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="商品列表" android:textColor="#000000" android:textStyle="bold" android:textSize="25sp" android:gravity="center"/>

    <LinearLayout android:layout_marginTop="5dp" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
            <TextView android:text="ID" android:textStyle="bold" android:textColor="#000000" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/>
            <TextView android:text="商品名称" android:textStyle="bold" android:textColor="#000000" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/>
            <TextView android:text="供应商" android:textStyle="bold" android:textColor="#000000" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/>
            <TextView android:id="@+id/tv_but_price" android:text="单价(元)" android:textStyle="bold" android:textColor="#000000" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/>
            <TextView android:id="@+id/tv_but_num" android:text="数量(个)" android:textStyle="bold" android:textColor="#000000" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/>

        </LinearLayout>


        <ListView android:id="@+id/list_viewsp" android:layout_width="match_parent" android:layout_height="match_parent"/>

    </LinearLayout>

</LinearLayout>

接下来写 商品的实体类

在这里插入代码片
public class Shangping {

    private String id;
    private String name;
    private String shop;
    private String price;
    private String num;

    public Shangping(String id, String name, String shop, String price, String num) {
        this.id = id;
        this.name = name;
        this.shop = shop;
        this.price = price;
        this.num = num;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getShop() {
        return shop;
    }

    public void setShop(String shop) {
        this.shop = shop;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
}

实体类为后面的传值做准备

接着写listview的布局文件

在这里插入代码片

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">
    <TextView android:id="@+id/tv_id" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/tv_name" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/tv_shop" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/tv_privc" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/tv_num" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/>

</LinearLayout>

然后写listview的适配器

在这里插入代码片


public class AdapterShangPing extends ArrayAdapter {

    private int count;

    public AdapterShangPing(@NonNull Context context, int resource, List<Shangping> list) {
        super(context, resource,list);
        count=resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            Shangping sp= (Shangping) getItem(position);
            View view= LayoutInflater.from(getContext()).inflate(count,null);
            TextView tv1,tv2,tv3,tv4,tv5;
            tv1=view.findViewById(R.id.tv_id);
            tv2=view.findViewById(R.id.tv_name);
            tv3=view.findViewById(R.id.tv_shop);
            tv4=view.findViewById(R.id.tv_privc);
            tv5=view.findViewById(R.id.tv_num);
            tv1.setText(sp.getId());
            tv2.setText(sp.getName());
            tv3.setText(sp.getShop());
            tv4.setText(sp.getPrice());
            tv5.setText(sp.getNum());

        return view;
    }
}


接着写排序,排序其实很简单,只需要创建一个实体类然后实现Comparator接口,然后重写compare方法

在这里插入代码片
public class Pricesort implements Comparator<Shangping> { 
   
    @Override
    public int compare(Shangping o1, Shangping o2) { 
   
        return o1.getPrice().compareTo(o2.getPrice());
    }
}
在这里插入代码片
public class Numsort implements Comparator<Shangping> { 
   
    @Override
    public int compare(Shangping o1, Shangping o2) { 
   
        return o1.getNum().compareTo(o2.getNum());
    }
}

最后附上Activity代码

在这里插入代码片
public class shangpingActivity extends AppCompatActivity {
    private ListView listView;
    private List<Shangping> list_sp=new ArrayList<>();//存数据
    TextView tvbut_price,tvbut_num;
    AdapterShangPing adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shangping);
        into();//初始化数据
        listView=findViewById(R.id.list_viewsp);
        tvbut_num=findViewById(R.id.tv_but_price);
        tvbut_price=findViewById(R.id.tv_but_num);
         adapter=new AdapterShangPing(shangpingActivity.this,R.layout.list_shangping,list_sp);//初始化适配器
        listView.setAdapter(adapter);
            setClick();//设置textview的点击事件,然后排序
    }
    private  void into(){
        list_sp.add(new Shangping("1","发动机10A","发动机供应商","3,100","6"));
        list_sp.add(new Shangping("2","发动机10A","发动机供应商","4,100","1"));
        list_sp.add(new Shangping("3","发动机10A","发动机供应商","5,100","3"));
        list_sp.add(new Shangping("4","发动机10A","发动机供应商","4,300","4"));
    }
    private void setClick(){
			
        tvbut_price.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collections.sort(list_sp,new Numsort());
                adapter.notifyDataSetChanged();
            }
        });
        tvbut_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collections.sort(list_sp,new Pricesort());
                adapter.notifyDataSetChanged();
            }
        });

    }

}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194194.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sitemap死链检测工具-免费sitemap死链检测抓取工具

    sitemap死链检测工具-免费sitemap死链检测抓取工具sitemap死链检测工具,为什么要检测sitemap死链?当你辛辛苦苦生成sitemap然后再提交到搜索引擎收录。搜索引擎抓取你的网站后发现你的sitemap存在大量的死链,给你网站降权,或者降低关键词排名就得不偿失了。今天给大家分享这款免费的sitemap生成软件。不仅可以检测网站的死链。还支持未收录网站sitemap生成详细参考图片。目前需求建立网站的企业十分得多,sitemap死链检测工具而且有许多企业以为,网站建立是一个十分重要的工作,这一点的正确性大家不能承认,但是还有一点大家一定也不可以无视那

    2022年7月23日
    16
  • struts中的action_type object has no attribute

    struts中的action_type object has no attribute在Strust2中,有一个内置对象叫ActionContext,通过该对象可以获得之前Servlet中的对象,比如:requst对象,response对象…那么为什么可以通过ActionContext获得那些对象呢?那是因为在ActionContext内容引用了那些对象,也就是在ActionContext内部记录了那些对象的地址,看下图上图就是简单理解为什么通过Action

    2025年10月16日
    4
  • oracle错误 904,IMP-00058: 遇到 ORACLE 错误 904

    oracle错误 904,IMP-00058: 遇到 ORACLE 错误 904我将A服务器下的导入B服务器时其中一个表出现以下错误,出错误后我单独将这个表导出,然后导入。B服务器下已有T_CALLREORDS表,并且已有新数据,T_CALLREORDS有外键约束T_USER表。我的语句如下C:UsersAdministrator>impgxcfkefu/gxcfkefufull=yfile=e:/gxcf_T_CAL…显示全部我将A服务器下的导入B服务器时…

    2026年2月2日
    3
  • 理解的英文(言语理解)

    开篇明义,dropout是指在深度学习网络的训练过程中,对于神经网络单元,按照一定的概率将其暂时从网络中丢弃。注意是暂时,对于随机梯度下降来说,由于是随机丢弃,故而每一个mini-batch都在训练不同的网络。dropout是CNN中防止过拟合提高效果的一个大杀器,但对于其为何有效,却众说纷纭。在下读到两篇代表性的论文,代表两种不同的观点,特此分享给大家。

    2022年4月10日
    47
  • Mac配置Maven环境[通俗易懂]

    Mac配置Maven环境[通俗易懂]1.下载maven包到本地https://maven.apache.org/download.cgi1.在应用程序找到终端(实用工具)文件夹里面2.输入命令:vi~/.bash_profile输入i进入编辑模式输入:(注意⚠️M2_HOME需要填写为自己的路径哦~)exportM2_HOME=/Library/apache-maven-3.5.3exportPATH…

    2022年5月15日
    62
  • latex大括号公式编辑「建议收藏」

    latex大括号公式编辑「建议收藏」公式最近,在使用latex编辑论文的过程中,遇到如下类似的公式:可以看到,这个公式下面使用的括号型公式。在latex中可使用如下的方式产生:\begin{equation}\label{eqn11}%%\label\begin{aligned}f_{k}^{-d,n}\left(x_{dn}=w\right)&=\frac{\int_{\phi_{k…

    2022年10月11日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号