• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

在活动中创建一个名为Category的类,并且我想在Android的主要活动中调用此类的对象(Ma ...

原作者: [db:作者] 来自: 网络 收藏 邀请

在活动中创建一个名为Category的类,并且我想在Android的主要活动中调用此类的对象(Make a class with name Category in a activity and I want to call the object of this class in the main activity on 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) {
            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;
        }
    }

Make a class with name Catagory in an activity, and I want to call the object of this class in the mainactivity on Android.

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

原文:https://stackoverflow.com/questions/32578331
更新时间:2022-03-21 13:03

最满意答案

您可以在另一个单独的Java类上声明此类。 首先在你的Catagory类中声明这些变量:

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

You can declare this class on a different separate Java class. First declare these variable in your Catagory class:

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

    //Your rest of the code
}

Then simply in your MainActivity.java, declare its object:

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

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap