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

时间:2018-11-10 05:23:20

标签: java android arraylist

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

我目前正在查看自定义数组的工作原理以及其他方面的工作,我认为我一起做了一些体面的工作,直到我的getItem开始被视为整数为止。 Android告诉我将return更改为int,但这会适得其反。当我尝试使用toStringString.valueOf时,我在listview项目中只得到一长串文本。谁能告诉我在这里我做错了什么?

1 个答案:

答案 0 :(得分:0)

public String toString()从未为Item实现,因此,它返回Item的内存位置,而不是像javascript这样的语言返回数据。


示例:

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

输出:

  

[Item @ 4554617c,Item @ 74a14482]

说明:

当Java不确定如何将某些内容转换为字符串时,它将给出type@address。例如,如果您有一个节点

class Node {
    Node next;
}

然后做

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

您将得到一个无限循环,最终将导致程序错误。 Java通过不去显示内容来解决这个问题。

修复:

解决方案是实现toString(),以便Java不使用默认版本或直接使用变量的值。

public Item {
    private String It;
    private String Title;

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