0

I'm trying to add an item to arraylist/adapter on a Button click in my class

MyMaxes.java:

Date currentDate = new Date();
ArrayAdapter<Record> records = new ArrayAdapter<Record>(getApplicationContext(), R.layout.custom_record);
records.add(new Record(currentDate ,maxSquat, maxBench, maxDead, maxTotal));
records.notifyDataSetChanged();

And in my other class, MyProgress.java, I'm trying to assign that adapter to the ListView:

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

The records is underlined and says "Expression Expected".
I feel I need to set the TextViews in my custom layout file in the adapter somehow so it knows which item to put in which TextView.
Am I supposed to have an adapter class?

here is my custom_record:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date"
        android:textSize="18sp"
        android:id="@+id/txtDate"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bench"
        android:id="@+id/txtBench"
        android:paddingRight="5dp"
        android:textSize="18sp"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:layout_toLeftOf="@+id/txtSquat"
        android:layout_toStartOf="@+id/txtSquat"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Squat"
        android:textSize="18sp"
        android:paddingRight="5dp"
        android:id="@+id/txtSquat"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:layout_toLeftOf="@+id/txtDead"
        android:layout_toStartOf="@+id/txtDead"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dead"
        android:paddingRight="5dp"
        android:textSize="18sp"
        android:id="@+id/txtDead"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:layout_toLeftOf="@+id/txtTotal"
        android:layout_toStartOf="@+id/txtTotal"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Total"
        android:textSize="18sp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:id="@+id/txtTotal"
        android:layout_marginRight="34dp"
        android:layout_marginEnd="34dp"

        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

and my custom object:

public class Record {

    private Integer squat, bench, dead;
    private double total;
    private Date dateTime;

    public Record(Date date,int squat, int bench, int dead, double total) {
        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 ::Verify example::

main activity code:

public class MainActivity extends AppCompatActivity {

    Button mButton;
    Integer maxSquat = 1;
    Integer maxBench = 1;
    Integer maxDead = 1;
    Double maxTotal = 3.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton= (Button) findViewById(R.id.btnTest);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Date currentDate = new Date();
                ArrayAdapter<Record> records = new ArrayAdapter<Record>(getApplicationContext(), R.layout.custom_record);
                records.add(new Record(currentDate ,maxSquat, maxBench, maxDead, maxTotal));
                records.notifyDataSetChanged();
            }
        });
    }
}

listview.java:

public class listview extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

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

listview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

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

activitymain.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bestworkouts.myapplication.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnTest"/>

I then have the record.java class from above, as well as the custom_record.xml from above.
Oddly enough, this one gives me a different error, it cannot resolve "records" when setting the adapter, where in my app it says "Expression Expected" for records.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
LBJ33
  • 425
  • 11
  • 29
  • What do you mean "your other class"? You cannot reference variables across classes just like that. – OneCricketeer Aug 11 '16 at 22:43
  • Are you trying to [Pass data between Activities](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android)? – OneCricketeer Aug 11 '16 at 22:44
  • I want to be able to add items to an arraylist in one class, and populate a listview with that arraylist in another class.. I got this far using the answer on my previous question: http://stackoverflow.com/questions/38904588/adding-custom-object-to-arraylist-on-button-click/38905151?noredirect=1#comment65169369_38905151 – LBJ33 Aug 11 '16 at 22:47
  • 1
    Sorry, but you are going to have to show a [mcve] of your class definitions. – OneCricketeer Aug 11 '16 at 22:49
  • Edited giving the best example I could – LBJ33 Aug 11 '16 at 23:08
  • Thanks. Now, was it your idea or someone else who told you that you needed two activities? You have a Java compilation error. You can't reference variables between classes just because you used `extends` – OneCricketeer Aug 11 '16 at 23:11
  • Mine, as far as my knowledge goes I would need two. I'm new to android but the rest of my app is done. It has a navigation bar with 6 pages, all pages are done except for this one. The user inputs 3 variables (maxDead, maxSquat, maxBench), then the total is calculated and used later in the app. The last page im working on now I decided to log those, so every time the user updates the values, a new log with the date is added to another page called "My Progress", not sure how to use one class for something like this – LBJ33 Aug 11 '16 at 23:15

1 Answers1

1

Why can't you have one view with a Button and a ListView that you add to?

Your first problem - you can't reference the local variable records from anywhere outside the click listener. This is a compilation error.

Second problem - you would be creating a brand new, empty adapter each time you clicked the button, then only adding one Record to it.


activitymain.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnTest"/>

</LinearLayout>

Let's call this RecordActivity because it displays and adds records to a ListView.

I would suggest using an AlertDialog to show a popup View when you click the button to get a Record object.

public class RecordActivity extends AppCompatActivity {

    Button mButton;
    ArrayAdapter<Record> mAdapter;
    ListView listViewRec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton= (Button) findViewById(R.id.btnTest);
        mAdapter = new ArrayAdapter<Record>(getApplicationContext(), R.layout.custom_record);
        listViewRec = (ListView) findViewById(R.id.listViewRecord);
        listViewRec.setAdapter(mAdapter);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.add(getRecord());
            }
        });
    }

    private Record getRecord() {
        Date currentDate = new Date();
        // TODO: Get your max values from somewhere
        int maxSquat = 1;  // TODO: From input field
        int maxBench = 1; // TODO: From input field
        int maxDead = 1; // TODO: From input field

        return new Record(currentDate ,maxSquat, maxBench, maxDead);
    }
}

Remove maxTotal from the parameters. You can calculate that inside the constructor of a Record.

public Record(Date date,int squat, int bench, int dead) {
    this.bench = bench;
    this.dateTime = date;
    this.dead = dead;
    this.squat = squat;
    this.total = bench + dead + squat;
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks for this answer, very good example. Is there any way to do this from the separate layout file though? I'm not opposed to doing it this way, it would work, I'd just rather it automatically add the values from the other layout class as it's one less step for the user – LBJ33 Aug 11 '16 at 23:34
  • You can break apart and rearrange the layout as much as you want - it's the variable passing that is the problem. Yes, it is possible with `startActivityForResult` and `onActivityResult` to "return back a Record" from some "CreateRecordActivity"... [Example here](http://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android). However, as I said, an `AlertDialog` for a simple pop-up input window seems simpler. – OneCricketeer Aug 11 '16 at 23:45