Java, I have a problem creating a new instance of a class

0

Good first I have this class User

package com.example.leaftime.rocketdefender;

/**  * Created by Leaftime on 04/11/2017.  */

public class User {
    private Integer id;
    private String name;
    private Integer score;

    public User(Integer id, String name, Integer score)
    {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }
}

that simply takes care of the gets and sets of the id, name and score in a database sqlite that come integrated in android Studio and my error is the following

package com.example.leaftime.rocketdefender;

import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView;

import com.example.leaftime.rocketdefender.utils.utility;

import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List;

public class HighscoreListActivity extends AppCompatActivity {
    ListView highLIST;

    ArrayList<String> listInfo;
    ArrayList<User> listUsers;

    ConectionSQLiteHelper conn;

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

        conn = new ConectionSQLiteHelper(getApplicationContext(), "bd users", null, 1);

        highLIST = (ListView) findViewById(R.id.highscoreList);

        getListPersons();

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, listInfo);
        highLIST.setAdapter(adapter);
    }

    private void getListPersons()
    {
        SQLiteDatabase db = conn.getReadableDatabase();

        User user = null;
        listUsers = new ArrayList<User>();
        //select * from users
        Cursor cursor = db.rawQuery("SELECT * FROM " + utility.TABLE_USER, null);

        while(cursor.moveToNext())
        {
            user = new User();
            user.setId(cursor.getInt(0));
            user.setName(cursor.getString(1));
            user.setScore(cursor.getInt(2));

            listUsers.add(user);
        }
        obtainList();
    }

    private void obtainList()
    {
        listInfo = new ArrayList<String>();

        for (int i = 0; i < listUsers.size(); i++)
        {
            listInfo.add(listUsers.get(i).getId() + " - " + listUsers.get(i).getName());
        }
    } 
}

in this part I declare a user coming from User () is null User user = null;

and to create an instance in a listview I have this

while(cursor.moveToNext())
{
            user = new User();
            user.setId(cursor.getInt(0));
            user.setName(cursor.getString(1));
            user.setScore(cursor.getInt(2));

            listUsers.add(user); 

}

what it does is create each of the items in list view but when calling it in user = new User () it asks me to pass some parameters, specifically tell me User () in User can not be applied to: parameters arguments; Can someone give me a clue how to solve this problem?

    
asked by Kenji Miyashiro 06.11.2017 в 07:41
source

3 answers

1

The error that comes up to you indicates that you are "wrong" creating the User object. What happens is that when you have declared the User class, you have indicated in your constructor that you have to receive 3 parameters:

public User(Integer id, String name, Integer score){...}

You are indicating that to create an object of type User you have to pass it an id, a name and a score.

To fix this, you can pass the 3 parameters that it asks for or you can create a constructor to which you do not pass arguments (you can declare several constructors within a class that receive different parameters)

public User(Integer id, String name, Integer score){...}
public User(){...}
    
answered by 06.11.2017 / 08:20
source
0

The problem is that in the User class you have only declared a constructor with three parameters. When you create an instance of a class you have to use one of the constructors declared in the class. Add, therefore, in the User class the default constructor.

public User () {}

Greetings.

    
answered by 06.11.2017 в 08:09
0

That's because if the User's constructor has 3 parameters when creating it, you have to enter those 3 parameters, it would be like this:

Within your while there would be two lines:

user = new User(cursor.getInt(0), cursor.getString(1), cursor.getInt(2));
listUsers.add(user);

PS: if it seems more visible to you, you can also pick cursor.getInt (0) and the other two you have and put them in variables instead of putting them directly into your constructor. So maybe for some people it is a cleaner and more readable code.

    
answered by 06.11.2017 в 12:13