单击按钮将自定义对象添加到arraylist

编程入门 行业动态 更新时间:2024-10-08 02:19:36
本文介绍了单击按钮将自定义对象添加到arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在这里有点迷路了.我试图在单击按钮时向数组列表添加一个自定义对象(具有3个整数,1个双精度和1个日期).我希望将arraylist显示在另一个类的listview中.到目前为止,这是我自定义类的内容:

I'm a little lost here. I'm trying to add a custom object(with 3 integers, 1 double, and 1 date) to an arraylist when a button is clicked. I want the arraylist to be shown in another classes listview. So far this is what I have for the custom class:

ublic class Record { private Integer squat, bench, dead; private double total; private Date dateTime; public Record(int squat, int bench, int dead, double total, Date date) { this.bench = bench; this.dateTime = date; this.dead = dead; this.squat = squat; this.total = total; } public Integer getSquat() { return squat; } public void setSquat(Integer squat) { this.squat = squat; } public Integer getBench() { return bench; } public void setBench(Integer bench) { this.bench = bench; } public Integer getDead() { return dead; } public void setDead(Integer dead) { this.dead = dead; } public Double getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public Date getDateTime() { return dateTime; } public void setDateTime(Date dateTime) { this.dateTime = dateTime; } }

类名:MyMaxes.class:在单击按钮的类中,我有这个(不确定按钮单击时如何获取当前日期):

Classname: MyMaxes.class : In the class where the button is clicked, I have this(I'm not sure how to get the current date when button is clicked):

public ArrayList<Record> records = new ArrayList<>(); maxTotal = maxDead + maxBench + maxSquat; int maxDead = Integer.parseInt(mEditTextDead.getText().toString()); int maxSquat = Integer.parseInt(mEditTextSquat.getText().toString()); int maxBench = Integer.parseInt(mEditTextBench.getText().toString()); records.add(new Record(maxSquat, maxBench, maxDead, maxTotal, ));

类名称= MyProgress.class:在该类中,我要将列表视图设置为此数组列表(我不确定如何从其他类获取数组列表):

Class name = MyProgress.class : And in the class I want to set the listview to this arraylist(I'm not sure how to get the arraylist from the other class):

RecordAdapter adapter = new RecordAdapter(getApplicationContext(),R.layout.custom_record); ListView listViewRec = (ListView) findViewById(R.id.listViewRecord); }

这是我的RecordAdapter类,但是我不确定在其中添加什么:

This is my RecordAdapter class, but im not sure what to add there either:

public class RecordAdapter extends ArrayAdapter<Record> { public RecordAdapter(Context context, int resource) { super(context, resource); } @Override public View getView(int position, View convertView, ViewGroup parent) { return super.getView(position, convertView, parent); } }

感谢您的帮助

推荐答案

您不需要自定义RecordAdapter.您也不需要ArrayList.您可以将ArrayAdapter当作ArrayList并输入ListView中.您需要做的就是创建一个ArrayAdapter并以与ArrayList相同的方式填充它:

You do not need the custom RecordAdapter. Nor do you need the ArrayList. You can treat your ArrayAdapter as an ArrayList that feeds into your ListView. All you need to do is create an ArrayAdapter and populate it the same way you do with the ArrayList:

public ArrayAdapter<Record> records = new ArrayAdapter<>(getApplicationContext(), R.layout.custom_record); maxTotal = maxDead + maxBench + maxSquat; int maxDead = Integer.parseInt(mEditTextDead.getText().toString()); int maxSquat = Integer.parseInt(mEditTextSquat.getText().toString()); int maxBench = Integer.parseInt(mEditTextBench.getText().toString()); records.add(new Record(maxSquat, maxBench, maxDead, maxTotal ));

然后,每当您要将记录添加到列表视图时,请使用以下方法:

Then, whenever you want to add a record to you listview, use this:

records.add(new Record(maxSquat, maxBench, maxDead, maxTotal));

忘记了非常重要的部分.您需要获取ListView并将其设置为ArrayAdapter来填充它.

Forgot a very important part. You need to get your ListView and set the ArrayAdapter to populate it.

ListView listViewRec = (ListView) findViewById(R.id.listViewRecord); listViewRec.setAdapter(records);

更多推荐

单击按钮将自定义对象添加到arraylist

本文发布于:2023-11-11 08:39:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577912.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   单击   按钮   对象   arraylist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!