【问题标题】:Android Java Object Lists with Images带有图像的 Android Java 对象列表
【发布时间】:2017-03-04 13:16:43
【问题描述】:

我已经用 Java 编写了简单的代码,现在我正在学习 Android 并尝试转换我的代码。

我想做的是用 Image 制作对象。 有数千个对象(分为不同的类,如“盒子”“汽车”“主要”等) 而且我想添加每个对象图像,以便以后可以使用它,例如。 BMW_e30.Image();

我会尽量展示我所有的代码可读性和可理解性,如果不是,请评论并告诉我如何改进我的代码。

首先,我将向您展示我的代码如何在简单的 Java 中运行(所有 Java 代码都可以工作,但它很混乱且难以理解)


BoxesHolder.class(对象列表)

public class BoxesHolder {

    public Integer boxes[] = new Integer[20];

    public BoxesHolder(){

        MakeBoxes();
        }


    public void BoxesMaker(int k, int a1, int k1, int a2){

        boxes[a1] = k;
        boxes[a2] = k1;
    }

    public void MakeBoxes(){

        int i =0; int j =1;
        BoxesMaker(10, i, 0, j); // Boxes Level 1
        i +=2; j +=2;
        BoxesMaker(20, i, 0, j); // Boxes Level 2
        i +=2; j +=2;
        BoxesMaker(30, i, 0, j); // Boxes Level 3
        i +=2; j +=2;
        BoxesMaker(40, i, 0, j); // Boxes Level 4
        i +=2; j +=2;
        BoxesMaker(50, i, 0, j); // Boxes Level 5
        i +=2; j +=2;
        BoxesMaker(60, i, 0, j); // Boxes Level 6
        i +=2; j +=2;
        BoxesMaker(70, i, 0, j); // Boxes Level 7
        i +=2; j +=2;
        BoxesMaker(80, i, 0, j); // Boxes Level 8
        i +=2; j +=2;
        BoxesMaker(90, i, 0, j); // Boxes Level 9
        i +=2; j +=2;
        BoxesMaker(100, i, 0, j); // Boxes Level 10
        i +=2; j +=2;
    }
}

ImagesHolder.class

public class ImagesHolder {

       URL BoxLv1_2Icon = ImagesHolder.class.getResource("/BoxLv1_2.png");
       URL BoxLv3_4Icon = ImagesHolder.class.getResource("/BoxLv3_4.png");
       URL BoxLv5_6Icon = ImagesHolder.class.getResource("/BoxLv5_6.png");
       URL BoxLv7_8Icon = ImagesHolder.class.getResource("/BoxLv7_8.png");
       URL BoxLv9_10Icon = ImagesHolder.class.getResource("/BoxLv9_10.png");
    }

在 JPanel 中显示图像的标签:

Labels.BoxesLVL1Label.setIcon(new ImageIcon(ImagesHolder.BoxLv1_2Icon));
Labels.BoxesLVL2Label.setIcon(new ImageIcon(ImagesHolder.BoxLv1_2Icon));
Labels.BoxesLVL3Label.setIcon(new ImageIcon(ImagesHolder.BoxLv3_4Icon));
Labels.BoxesLVL4Label.setIcon(new ImageIcon(ImagesHolder.BoxLv3_4Icon));
Labels.BoxesLVL5Label.setIcon(new ImageIcon(ImagesHolder.BoxLv5_6Icon));
Labels.BoxesLVL6Label.setIcon(new ImageIcon(ImagesHolder.BoxLv5_6Icon));
Labels.BoxesLVL7Label.setIcon(new ImageIcon(ImagesHolder.BoxLv7_8Icon));
Labels.BoxesLVL8Label.setIcon(new ImageIcon(ImagesHolder.BoxLv7_8Icon));
Labels.BoxesLVL9Label.setIcon(new ImageIcon(ImagesHolder.BoxLv9_10Icon));
Labels.BoxesLVL10Label.setIcon(new ImageIcon(ImagesHolder.BoxLv9_10Icon));

OnButtonClick(添加新框的功能)

 if(ae.getSource() == jbtnBoxes){
            CarMain.main[2] += 1;
            CarMain.main[3] += 1;
            Statistic.statistic[1] +=1;
            Statistic.statistic[2] +=1;
            if(CarMain.main[2] == 5){
                CarMain.main[2] -= 5;
                if(CarMain.main[4] == 1){
                    Box.boxes[1] = Box.boxes[1] + 1;
                    Statistic.statistic[13] +=1;
                    JOptionPane.showMessageDialog (null, "You have gained 1 LVL box!", "Congralations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImagesHolder.BoxLv1_2Icon));}                  
                if(CarMain.main[4] == 2){ 
                    Random randomGenerator = new Random();
                    int randomInt = randomGenerator.nextInt(2)+1;
                    if (randomInt == 1){
                        Box.boxes[1] = Box.boxes[1] + 1;
                        Statistic.statistic[13] +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 1 LVL box!", "Congralations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImagesHolder.BoxLv1_2Icon));}
                    if (randomInt == 2){
                        Box.boxes[3] = Box.boxes[3] + 1;
                        Statistic.statistic[14] +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 2 LVL box!", "Congralations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImagesHolder.BoxLv1_2Icon));}
                    }

添加值的行数(计数):

Box.boxes[1] = Box.boxes[1] + 1;

Box.boxes[3] = Box.boxes[3] + 1;

图像显示线:

JOptionPane.showMessageDialog (null, "You have gained 1 LVL box!", "Congralations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImagesHolder.BoxLv1_2Icon));}

现在我想在 Android Studio 中做什么


BoxHolder.class

package com.crelix.crelix;

public class BoxHolder {

    int id;
    String name;
    int level;
    int price;
    int sellprice;
    int count;

    public void id(int id) {

    }

    public BoxHolder(String name) {

    }

    public void level(int level) {

    }

    public void price(int price) {

    }

    public void sellprice(int sellprice) {

    }

    public void count(int count) {

    }


        public static void main(String args[]) {

            BoxHolder AccessoriesBoxOne = new BoxHolder("Accessories Pack LV-1");
            BoxHolder AccessoriesBoxTwo = new BoxHolder("Accessories Pack LV-2");
            BoxHolder AccessoriesBoxThree = new BoxHolder("Accessories Pack LV-3");
            BoxHolder AccessoriesBoxFour = new BoxHolder("Accessories Pack LV-4");
            BoxHolder AccessoriesBoxFive = new BoxHolder("Accessories Pack LV-5");
            BoxHolder AccessoriesBoxSix = new BoxHolder("Accessories Pack LV-6");
            BoxHolder AccessoriesBoxSeven = new BoxHolder("Accessories Pack LV-7");
            BoxHolder AccessoriesBoxEight = new BoxHolder("Accessories Pack LV-8");
            BoxHolder AccessoriesBoxNine = new BoxHolder("Accessories Pack LV-9");
            BoxHolder AccessoriesBoxTen = new BoxHolder("Accessories Pack LV-10");

 AccessoriesBoxOne.id(1);
            AccessoriesBoxOne.level(1);
            AccessoriesBoxOne.price(10);
            AccessoriesBoxOne.sellprice(5);
            AccessoriesBoxOne.count(0);

            AccessoriesBoxTwo.id(2);
            AccessoriesBoxTwo.level(2);
            AccessoriesBoxTwo.price(20);
            AccessoriesBoxTwo.sellprice(10);
            AccessoriesBoxTwo.count(0);

            AccessoriesBoxThree.id(3);
            AccessoriesBoxThree.level(3);
            AccessoriesBoxThree.price(30);
            AccessoriesBoxThree.sellprice(15);
            AccessoriesBoxThree.count(0);

            AccessoriesBoxFour.id(4);
            AccessoriesBoxFour.level(4);
            AccessoriesBoxFour.price(40);
            AccessoriesBoxFour.sellprice(20);
            AccessoriesBoxFour.count(0);

            AccessoriesBoxFive.id(5);
            AccessoriesBoxFive.level(5);
            AccessoriesBoxFive.price(50);
            AccessoriesBoxFive.sellprice(25);
            AccessoriesBoxFive.count(0);

            AccessoriesBoxSix.id(6);
            AccessoriesBoxSix.level(6);
            AccessoriesBoxSix.price(60);
            AccessoriesBoxSix.sellprice(30);
            AccessoriesBoxSix.count(0);

            AccessoriesBoxSeven.id(7);
            AccessoriesBoxSeven.level(7);
            AccessoriesBoxSeven.price(70);
            AccessoriesBoxSeven.sellprice(35);
            AccessoriesBoxSeven.count(0);

            AccessoriesBoxEight.id(8);
            AccessoriesBoxEight.level(8);
            AccessoriesBoxEight.price(80);
            AccessoriesBoxEight.sellprice(40);
            AccessoriesBoxEight.count(0);

            AccessoriesBoxNine.id(9);
            AccessoriesBoxNine.level(9);
            AccessoriesBoxNine.price(90);
            AccessoriesBoxNine.sellprice(45);
            AccessoriesBoxNine.count(0);

            AccessoriesBoxTen.id(10);
            AccessoriesBoxTen.level(10);
            AccessoriesBoxTen.price(100);
            AccessoriesBoxTen.sellprice(50);
            AccessoriesBoxTen.count(0);

我想添加像 AccessoriesBoxOne.Image(ImageUrl); 这样的图片我该怎么做?

我现在如何访问对象?无法理解,因为这些行不再起作用:

Box.boxes[1] = Box.boxes[1] + 1;

我在想我会这样做:

Box.AccessoriesBoxOne.count = +1;

但它不起作用。

我希望你能理解我想说的和想做的,带着希望,Crelix。

【问题讨论】:

  • 变量名仅相差一个数字意味着您应该使用数组或列表。请查找有关这些基本编程概念的教程。
  • 这也不是安卓问题,因为您使用的是 JPanel。
  • @Code-Apprentice 我没有在新代码中使用 JPanel(代码如下),JPanel 代码来自 eclipse 中的简单 Java 编码
  • 我不明白您需要什么帮助。你的问题是什么?部分困难在于这里有太多代码。你能把它简化为一个非常简单的例子吗?
  • @Code-Apprentice 好的,我现在就去做。问题就在那里,使用新代码 e (BoxHolder) 而不是 (BoxesHolder) 将无法使用此“Box.boxes[1] = Box.boxes[1] + 1;”我正在尝试使用“Box.AccessoriesBoxOne.count = +1;”但它仍然不起作用。

标签: java arrays swing arraylist


【解决方案1】:

因此,此代码质量低劣且难以理解。

  1. 了解 DRY、循环、数组和集合。
  2. 您还应该使用命名约定。
  3. 使用有意义的名称:k, k1, a, a2 - 这是什么意思?
  4. 使用私有字段和 getter/setter。

如果可以,请使用原始 int 数组,而不是 Integer 数组:

int[] boxes = new int[20]; 然后Box.boxes[1]++(递增)

【讨论】:

  • 谢谢,但如果我理解正确,这些要点是关于“简单 Java 代码”(我不会再使用它了),我需要知道如何在 Android 代码中增加(在主题中列出)。
  • 我怎么说 Box.boxes[1] = Box.boxes[1] + 1 。正在使用简单的 Java,我只是不想使用该代码。
  • 您是否尝试在 Android 中使用 BoxHolder(您问题中的最后一个代码)?
  • 你有 main() 方法,在 Android 中没有使用。您的 Android 项目中有一些 Activity 类和布局文件吗?
  • 是的,我有。我有 mainActivity 和 activity_main.xml
猜你喜欢
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多