简体   繁体   English

由于某些原因,将getArrayList视为int

[英]getArrayList is being treated like an int for some reason

public class Item {

    //declare private data instead of public to ensure the privacy of data field of each class
    private String It;
    private String Title;

    public Item(String item, String hometown) {
        this.It = item;
        this.Title = hometown;
    }

    //retrieve user's name
    public String getIt(){
        return It;
    }

    //retrieve users' hometown
    public String getTitle(){
        return Title;
    }

    public static ArrayList<Item > getItem() {
        ArrayList<Item> item = new ArrayList<Item>();
        item.add(new Item("Harry", "San Diego"));
        item.add(new Item("Marla", "San Francisco"));
        item.add(new Item("Sarah", "San Marco"));
        return item;
    }
}
public class UsersAdapter extends ArrayAdapter<Item> {
        public UsersAdapter(Context context, ArrayList<Item> it) {
            super(context, 0, it);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            Item item = getItem(position);
            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
            }
            // Lookup view for data population
            TextView tv1 = (TextView) convertView.findViewById(R.id.tv1);
            TextView tv2 = (TextView) convertView.findViewById(R.id.tv2);
            // Populate the data into the template view using the data object
            String tv = String.valueOf(Item.**getItem**()); //.toString();
            tv1.setText(tv);
            String title = Title.getText().toString();
            tv2.setText(title);
            // Return the completed view to render on screen
            return convertView;
        }

I'm currently looking up how custom arrays and whatnot work.I thought I out something decent together until my getItem started getting treated like an integer. 我目前正在查看自定义数组的工作方式以及什么不起作用,我认为我会一起取得一些体面的成果,直到我的getItem开始被视为整数为止。 Android tells me to change the return to int but that would be counter productive. Android告诉我将return更改为int,但这会适得其反。 When I try using toString or String.valueOf , I just get a long string of text in my listview item. 当我尝试使用toStringString.valueOf ,我在listview项目中仅得到一长串文本。 Can anyone tell what I might be doing wrong here? 谁能告诉我我在这里做错了什么?

public String toString() is never implemented for Item, so instead of returning the data like in a language such as javascript, it returns the location in memory of the Item. public String toString()从未为Item实现,因此,它不返回像javascript这样的语言中的数据,而是返回Item 在内存中的位置


Example: 例:

public static void main(String[] args) {
    ArrayList<Item> list = new ArrayList<Item>();
    list.add(new Item("foo", "bar"));
    list.add(new Item("Stuff", "Bla"));

    System.out.println(list);
}

public class Item {
    String a, b;

    public Item(String a, String b) {
        this.a = a;
        this.b = b;
    }
}

Output: 输出:

[Item@4554617c, Item@74a14482] [Item @ 4554617c,Item @ 74a14482]

Explanation: 说明:

When java is unsure how to convert something to a String it gives type@address . 当java不确定如何将某些内容转换为String时,它将给出type@address For example if you had a node 例如,如果您有一个节点

class Node {
    Node next;
}

and then did 然后做了

Node A = new Node();
Node B = new Node();
A.next = B;
B.next = A;
String.valueOf(A);

You would get an infinite loop which would end in your program erroring. 您将得到一个无限循环,最终将导致程序错误。 Java handles this by just not going to the effort of showing the contents. Java通过不去显示内容来解决这个问题。

Fix: 固定:

The solution is to implement toString() so that java doesn't use the default version or as for the value of variables directly. 解决方案是实现toString(),以便Java不使用默认版本或直接使用变量的值。

public Item {
    private String It;
    private String Title;

    public String toString() {
        return "[it: " + IT + ", title: " + Title + "]";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM