LBJ33
LBJ33

Reputation: 449

Adding custom object to arraylist on button click

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;
}
}

EDIT: 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, ));

Edit: 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);

}

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);

}
}

Thanks for any help

Upvotes: 0

Views: 1242

Answers (2)

Logan Rodie
Logan Rodie

Reputation: 693

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));

EDIT:

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);

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83537

I think the problem is that you have two different adapters:

RecordAdapter adapter = new RecordAdapter(getApplicationContext(),R.layout.custom_record);
ArrayAdapter<Record> arrayAdapter = new ArrayAdapter<Record>(this, R.layout.custom_record, records);

You should use one or the other, not both. It appears that arrayAdapter should work. So change all of your code to use it.

Alternatively, you can use your custom RecordAdapter. If you do this, then you should create the ArrayList inside this class. Then when the user clicks a button, you send the new Record object to the RecordAdapter instance. The easiest way to do this is to add a addRecord() method to RecordAdapter.

Get Current Date:

All you need to do is create a new Date object:

Date currentDate = new Date();

See the linked javadocs for details about how to use Date.

Upvotes: 1

Related Questions