转移阵列列表> gt; or&< integer>从一个课程到活动课

发布于 2025-01-24 02:11:54 字数 2520 浏览 0 评论 0原文

我正在尝试将此代码从一个类中转移到我在Android Studio中的一个活动课程中,因为我试图将其用于文本视图。

public class cartesian() {
    public ArrayList<Character> list1() {
        Random rand = new Random();
        int low = 3;
        int high = 5;
        int result = rand.nextInt(high - low) + low;

        ArrayList<Character> list1 = new ArrayList<>(result);

        for (int i = 0; i <= result; i++) {
            list1.add((char)(rand.nextInt(26) + 'a'));
        }

        return list1;
    }

    public ArrayList<Integer> list2() {
        Random rand = new Random();
        int low = 3;
        int high = 5;
        int result = rand.nextInt(high - low) + low;

        ArrayList<Integer> list2 = new ArrayList<>(result);

        for (int i = 0; i <= result; i++) {
            list2.add(rand.nextInt(100));
        }

        return list2;
    }
}

我尝试了一个来自在这里,一个由“ bastien pinaquy ”的“但这也不起作用,因为它每次都会返回null。我还尝试使用自定义对象,但我对自己的理解不信心,以使自己大量利用它。

自定义对象类

public class CustomObject {
    private List<Character> charList;
    private List<Integer> intList;

    public CustomObject(){
        // not sure if I need to put anything in here
    }

    public List<Character> getCharList() { return charList; }
    public void setCharList(List<Character> charList) { this.charList = charList; }
    public List<Integer> getIntList() { return intList; }
    public void setIntList(List<Integer> intList) { this.intList = intList; }
}

活动类

public class cartProdActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart_prod);
    
    ArrayList<Character> charList = list1();
    String charListStr = charList.toString();

    ArrayList<Integer> intList = list2();
    String intListStr = intList.toString();

    setTxt(charListStr, intListStr);
}

public void setTxt(String list1, String list2) {
    TextView list1View = (TextView) findViewById(R.id.list1);
    list1View.setText(list1);

    TextView list2View = (TextView) findViewById(R.id.list2);
    list2View.setText(list2);
}
// the list1 and list2 functions refer to the functions from the first code block
// they are there as I was testing whether the code was working or not

该功能本身正如我在活动类中所说的那样起作用出于其他目的。

I'm trying to transfer this code from one class to one of my activity classes in Android Studio as I am trying to use it for my Text View.

public class cartesian() {
    public ArrayList<Character> list1() {
        Random rand = new Random();
        int low = 3;
        int high = 5;
        int result = rand.nextInt(high - low) + low;

        ArrayList<Character> list1 = new ArrayList<>(result);

        for (int i = 0; i <= result; i++) {
            list1.add((char)(rand.nextInt(26) + 'a'));
        }

        return list1;
    }

    public ArrayList<Integer> list2() {
        Random rand = new Random();
        int low = 3;
        int high = 5;
        int result = rand.nextInt(high - low) + low;

        ArrayList<Integer> list2 = new ArrayList<>(result);

        for (int i = 0; i <= result; i++) {
            list2.add(rand.nextInt(100));
        }

        return list2;
    }
}

I have tried one of the answers from here, the one by "bastien pinaquy" but that didn't work either as it returns null each time. I also tried using a custom object but I'm not confident in my understanding of it to allow myself to make much use of it.

Custom Object Class

public class CustomObject {
    private List<Character> charList;
    private List<Integer> intList;

    public CustomObject(){
        // not sure if I need to put anything in here
    }

    public List<Character> getCharList() { return charList; }
    public void setCharList(List<Character> charList) { this.charList = charList; }
    public List<Integer> getIntList() { return intList; }
    public void setIntList(List<Integer> intList) { this.intList = intList; }
}

Activity Class

public class cartProdActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart_prod);
    
    ArrayList<Character> charList = list1();
    String charListStr = charList.toString();

    ArrayList<Integer> intList = list2();
    String intListStr = intList.toString();

    setTxt(charListStr, intListStr);
}

public void setTxt(String list1, String list2) {
    TextView list1View = (TextView) findViewById(R.id.list1);
    list1View.setText(list1);

    TextView list2View = (TextView) findViewById(R.id.list2);
    list2View.setText(list2);
}
// the list1 and list2 functions refer to the functions from the first code block
// they are there as I was testing whether the code was working or not

The function itself works as I have put it in the activity class to check but I want it in a separate class I need to work with the list itself for other purposes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文