Java 在活动中创建一个名为Category的类,我想在Android的主活动中调用这个类的对象

Java 在活动中创建一个名为Category的类,我想在Android的主活动中调用这个类的对象,java,android,Java,Android,在活动中创建一个名为Catagory的类,我想在Android上的mainactivity中调用该类的对象 package com.example.listviewexample; public class Catagory { private int id; private String name; public Catagory(int id, String name, String desc, String type) {

在活动中创建一个名为
Catagory
的类,我想在Android上的
mainactivity
中调用该类的对象

 package com.example.listviewexample;

    public class Catagory {

        private int id;
        private String name;

        public Catagory(int id, String name, String desc, String type) {
            super();
            this.id = id;
            this.name = name;
            this.desc = desc;
            this.type = type;
        }

        private String desc;
        private String type;

        public int getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

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

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }

您可以在另一个单独的Java类上声明这个类。首先在分类类中声明以下变量:

public class Catagory {
    private int id;
    private String name;
    private String desc;
    private String type;

    //Your rest of the code
}
然后只需在MainActivity.java中声明其对象:

ArrayList<Catagory> list=new ArrayList<>();
list.add(new Catagory(1, "Name 1", "Description 1", "Type 1")); // Its index is 0.
list.add(new Catagory(2, "Name 2", "Description 2", "Type 2")); // Its index is 1.
list.add(new Catagory(3, "Name 3", "Description 3", "Type 3")); // Its index is 2.
list.add(new Catagory(4, "Name 4", "Description 4", "Type 4")); // Its index is 3.

//Keep on adding these as many as you want, and another way:
Catagory person = new Catagory(5, "Lisa", "Female", "Good");
list.add(person); // Its index will be 4.

//To access these variables just use index:
String firstName=list.get(0).getName(); // Will return Name1.
String secondDescription=list.get(1).getDesc); // Will return Description 2.

//To change the some values, just use this:
list.get(0).setName("Alex");
list.get(0).setDesc("Male");
list.get(0).setId(9);
ArrayList list=new ArrayList();
添加(新类别(1,“名称1”、“说明1”、“类型1”);//它的指数是0。
添加(新类别(2,“名称2”,“说明2”,“类型2”);//其指数为1。
添加(新类别(3,“名称3”,“说明3”,“类型3”);//其指数为2。
列表。添加(新类别(4,“名称4”,“说明4”,“类型4”);//其指数为3。
//继续添加您想要的数量,另一种方式是:
分类人=新分类(5,“丽莎”,“女性”,“良好”);
列表。添加(人);//其指数将为4。
//要访问这些变量,只需使用索引:
String firstName=list.get(0.getName();//将返回Name1。
String secondDescription=list.get(1.getDesc);//将返回说明2。
//要更改某些值,只需使用以下命令:
list.get(0.setName)(“Alex”);
list.get(0.setDesc(“男性”);
list.get(0).setId(9);

您可以在不同的单独Java类上声明此类。首先在分类类中声明以下变量:

public class Catagory {
    private int id;
    private String name;
    private String desc;
    private String type;

    //Your rest of the code
}
然后只需在MainActivity.java中声明其对象:

ArrayList<Catagory> list=new ArrayList<>();
list.add(new Catagory(1, "Name 1", "Description 1", "Type 1")); // Its index is 0.
list.add(new Catagory(2, "Name 2", "Description 2", "Type 2")); // Its index is 1.
list.add(new Catagory(3, "Name 3", "Description 3", "Type 3")); // Its index is 2.
list.add(new Catagory(4, "Name 4", "Description 4", "Type 4")); // Its index is 3.

//Keep on adding these as many as you want, and another way:
Catagory person = new Catagory(5, "Lisa", "Female", "Good");
list.add(person); // Its index will be 4.

//To access these variables just use index:
String firstName=list.get(0).getName(); // Will return Name1.
String secondDescription=list.get(1).getDesc); // Will return Description 2.

//To change the some values, just use this:
list.get(0).setName("Alex");
list.get(0).setDesc("Male");
list.get(0).setId(9);
ArrayList list=new ArrayList();
添加(新类别(1,“名称1”、“说明1”、“类型1”);//它的指数是0。
添加(新类别(2,“名称2”,“说明2”,“类型2”);//其指数为1。
添加(新类别(3,“名称3”,“说明3”,“类型3”);//其指数为2。
列表。添加(新类别(4,“名称4”,“说明4”,“类型4”);//其指数为3。
//继续添加您想要的数量,另一种方式是:
分类人=新分类(5,“丽莎”,“女性”,“良好”);
列表。添加(人);//其指数将为4。
//要访问这些变量,只需使用索引:
String firstName=list.get(0.getName();//将返回Name1。
String secondDescription=list.get(1.getDesc);//将返回说明2。
//要更改某些值,只需使用以下命令:
list.get(0.setName)(“Alex”);
list.get(0.setDesc(“男性”);
list.get(0).setId(9);

您可以单独声明类。然后通过创建其类的对象来调用其构造函数

单独上课

public class Catagory implements Serializable{

    private int id;
    private String name;
    private String desc;
    private String type;

    public Catagory(int id, String name, String desc, String type) {
        this.id = id;
        this.name = name;
        this.desc = desc;
        this.type = type;
    }

    public int getId() {
         return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
 }
然后您可以在活动中将其称为:

Catagory objCatagory = new Catagory(id, name, desc, type); //This will call the constructor and create a object with initial values you have passed as parameter

您可以单独声明该类。然后通过创建其类的对象来调用其构造函数

单独上课

public class Catagory implements Serializable{

    private int id;
    private String name;
    private String desc;
    private String type;

    public Catagory(int id, String name, String desc, String type) {
        this.id = id;
        this.name = name;
        this.desc = desc;
        this.type = type;
    }

    public int getId() {
         return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
 }
然后您可以在活动中将其称为:

Catagory objCatagory = new Catagory(id, name, desc, type); //This will call the constructor and create a object with initial values you have passed as parameter

你面临什么问题?你主要的活动是什么?你主要的活动是什么?ArrayListCatagory>c=newArrayList();Catagory类是Catagory.java文件中的独立类,mainactivty.java是我想通过使用数组列表调用Catagory类的对象来显示mainactivity.java中所有参数的独立文件。ArrayListCatagory>c=newArrayList();Catagory类是Catagory.java文件中的独立类,mainactivty.java是独立文件,我想通过使用数组列表调用Catagory类的对象来显示mainactivity.java中的所有参数。