0

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

6
  • Please include the names of classes which contains each code snippet. For example, which class has the ArrayList? Also, when and where do you create an instance of this class? Commented Aug 11, 2016 at 19:30
  • As for the date issue, do you want to automatically get the current date or do you want to allow the user to select a date? Commented Aug 11, 2016 at 19:31
  • Also, it is confusing that you have two adapters: adapter and arrayAdapter. What is the purpose of doing this? Commented Aug 11, 2016 at 19:32
  • I added the names of the classes, and I want the app to get the current date when button is clicked, basically I'm trying to make a log of items when the button is clicked, so when the user updates it, and clicks the button, it will add another item to array list.. and oops! forgot to delete one of the adapters Commented Aug 11, 2016 at 19:38
  • This is probably too much of an edit. Now my answer to your original question doesn't make much sense. In the future, you should just post a new question rather than completely replace an existing question, especially when there is an accompanying answer. Commented Aug 11, 2016 at 20:00

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks, I will test out the arrayAdapter, I just need to find out how to add the date in my line records.add(new Record(maxSquat, maxBench, maxDead, maxTotal, ));
Also, how do I stop it from giving an NPE error when setting the listview to arrayAdapter if there is no items in the records arraylist?
Thanks for the date, I have one more problem.. where I create the new ArrayAdapter, records is underlined and it says "Expression Expected".. Do I have to somehow call the arraylist records from the other class?
@LBJ33 It sounds like the NPE issue is a new question. Suggested reading before you post: stackoverflow.com/questions/218384/…, stackoverflow.com/questions/3988788/…
@LBJ33 For the ArrayAdapter error, please edit your question or post a new one and include the code as well as the exact error message.
|
1

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

3 Comments

Unfortunately this did not work, I no longer have any errors, but the items are not being added to the listview in the separate class either
I feel like I have to set the values of the different textviews in my custom layout custom_record.xml.. so it knows where to put which value, not sure where to do that though
@LBJ33 You need to specify the from and to parameters for the ArrayAdapter constructor. This will populate the TextViews for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.