简体   繁体   中英

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. Android tells me to change the return to int but that would be counter productive. When I try using toString or String.valueOf , I just get a long string of text in my listview item. 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.


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]

Explanation:

When java is unsure how to convert something to a String it gives 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.

Fix:

The solution is to implement toString() so that java doesn't use the default version or as for the value of variables directly.

public Item {
    private String It;
    private String Title;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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