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

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
LBJ33
  • 425
  • 11
  • 29
  • 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? – Code-Apprentice Aug 11 '16 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? – Code-Apprentice Aug 11 '16 at 19:31
  • Also, it is confusing that you have two adapters: `adapter` and `arrayAdapter`. What is the purpose of doing this? – Code-Apprentice Aug 11 '16 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 – LBJ33 Aug 11 '16 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. – Code-Apprentice Aug 11 '16 at 20:00
  • I am rolling this back to your original question. Please post a new question instead. (My apologies for not being more clear about my expectations for an edit.) – Code-Apprentice Aug 11 '16 at 20:02

2 Answers2

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 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, )); – LBJ33 Aug 11 '16 at 19:41
  • 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? – LBJ33 Aug 11 '16 at 19:42
  • 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 Aug 11 '16 at 19:45
  • @LBJ33 It sounds like the NPE issue is a new question. Suggested reading before you post: http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it, http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Code-Apprentice Aug 11 '16 at 19:46
  • @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. – Code-Apprentice Aug 11 '16 at 19:47
  • @LBJ33 Also, you should learn about SQLite databases. Using a database with a `ListView` is a little complicated, but Android manages a lot of stuff for you, such as updating the list automatically when new items are added or items are deleted. – Code-Apprentice Aug 11 '16 at 19:48
  • I know a small amount about SQLite database, I followed a tutorial on using one to create a database for contacts. I decided not to use one for this because I thought it would be simpler haha maybe I was wrong.. I thought this would be easier since they should only be adding an item every month or two so it would be quite small.. edited the question though! – LBJ33 Aug 11 '16 at 19:53
  • The other advantage of a database is that it is persistent between invocations of your app. An `ArrayList` is in-memory only and you will have to write your own code to save it to a file. With that said, I think you have the right idea. Using an `ArrayList` will help you understand how `ListView` and `ArrayAdapter` work. That will make understanding `CursorAdapter` a little easier. – Code-Apprentice Aug 11 '16 at 19:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120740/discussion-between-lbj33-and-code-apprentice). – LBJ33 Aug 11 '16 at 20:13
  • I posted a new question that might make it a bit more clear what I'm trying to do and the problems I'm having: http://stackoverflow.com/questions/38907185/arraylist-populating-listview-in-separate-class-not-working?noredirect=1#comment65172917_38907185 – LBJ33 Aug 11 '16 at 22:49
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);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Logan Rodie
  • 673
  • 4
  • 12
  • 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 – LBJ33 Aug 11 '16 at 20:12
  • 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 Aug 11 '16 at 20:16
  • @LBJ33 You need to specify the `from` and `to` parameters for the `ArrayAdapter` constructor. This will populate the `TextView`s for you. – Code-Apprentice Aug 11 '16 at 20:17