添加一个对象数组列表到文本

huangapple go评论84阅读模式
英文:

Adding a object arraylist to a text

问题

我同时拥有字符串和整数数据类型,因此我创建了一个对象arrayList来容纳它们,但是当我想将对象arrayList添加到文本中时遇到了错误。这样做不可能吗?
以下是我的代码:

public class MainActivity extends AppCompatActivity {
    public static List<Object> hi = new ArrayList<>();

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

        int converter = Integer.parseInt(editText.getText().toString());
        converter = converter / 2;

        if (copper1Pressed && copper2Pressed) {
            convertedCost.setText("Converted cost: " + convert);

            hi.add(editText.getText());
            hi.add(converter);
            editText.setText("");
            String string1 = "Copper";
            String string2 = "Copper";
            for (Object edit : hi) {
                edit = (string1 + " " + myList.get(0) + " = " + string2 + " " + myList.get(1));
                txtList.setText(edit);

(注意:由于您要求仅返回翻译好的部分,我已经移除了您提供的代码中的代码部分,只保留了翻译后的内容。)

英文:

I have both string and integer data types so I have created an object arrayList to hold them both but have errors when I want to add the object arrayList to a text. Is this not possible to do?
Here is my code:

public class MainActivity extends AppCompatActivity {
public static  List &lt;Object&gt; hi = new ArrayList&lt;&gt;();

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

 int converter = Integer.parseInt(editText.getText().toString());
            converter = converter/2;

            if (copper1Pressed &amp;&amp; copper2Pressed) {
                convertedCost.setText(&quot;Converted cost: &quot; + convert);

                hi.add(editText.getText());
                hi.add(converter);
                editText.setText(&quot;&quot;);
                String string1 = &quot;Copper&quot;;
                String string2 = &quot;Copper&quot;;
                for(Object edit : hi){
                    edit = (string1 + &quot; &quot; + myList.get(0) + &quot; = &quot; + string2 + &quot; &quot; + myList.get(1));
                    txtList.setText(edit);

答案1

得分: 1

首先,不要这样做。这是一个糟糕的策略。
另一种解决方案可以是

  • 如果您不需要知道实际类型,可以将int转换为String。

Object变量的问题在于容易犯错。

您的问题是int不是一个类。您不能将原生变量存储为对象,因为int并未扩展自Object类。然而,Integer将允许您正确执行应用程序。

英文:

First thing first, do not do that. It is bad strategy.
Alternative solution can be

  • Converting int to String, if you do not need to know what is the actual type.

The problem with Object variable is, easy to make mistake.

Your problem is int is not a class. You cannot store a native variable as object, since int is not extended from Object class. Integer however will let you execute your application properly.

huangapple
  • 本文由 发表于 2020年7月27日 07:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63106776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定