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

我目前正在查看自定義數組的工作方式以及什么不起作用,我認為我會一起取得一些體面的成果,直到我的getItem開始被視為整數為止。 Android告訴我將return更改為int,但這會適得其反。 當我嘗試使用toStringString.valueOf ,我在listview項目中僅得到一長串文本。 誰能告訴我我在這里做錯了什么?

public String toString()從未為Item實現,因此,它不返回像javascript這樣的語言中的數據,而是返回Item 在內存中的位置


例:

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不確定如何將某些內容轉換為String時,它將給出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 + "]";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM