Frederik Cretel
Frederik Cretel

Reputation: 3

Android - Array of objects with imageView attribute

I want to make an ArrayList of a Node object that has an ImageView attribute. Each of the objects would has a different icon associated with it that is inside the imageview. I create a new imageview and assign an image resource to it and then make a new node object containing that imageview. Then I assign another image resource to the imageview I created and make another node object with this "new" imageview. Thinking that would give me 2 node objects with different icons.

This is my code:

ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.icon1);
Node node1 = new Node(imageview, "some text", "some more text");

imageview.setImageResource(R.drawable.icon2);
imageview.setTag(R.drawable.light_kidney_ore);
Node node2 = new Node(imageview, "some text", "some more text");

nodes = new ArrayList<Node>();
nodes.add(node1);
nodes.add(node2);

If I do it this way both node objects get the same icon.

I know that the problem is that the imageview inside of the node objects just references the same imageview and that is why both objects have the same icon (the last one I put inside of the imageview).

I could ofcourse make a second ImageView and use that one for the second icon but seeing as I need to put like 50 nodes inside of my array I don't want to make a new ImageView for every node object.

I should really be able to find a solution to this myself but can't seem to find one at the moment. I am not a very experienced programmer as you might have guessed.

Could someone please give my a solution to my problem?

I could use an int ID instead of an ImageView like the first commenter said but then I run into another problem. I use an adapter to add these node objects to a ListView.

public class NodeAdapter extends ArrayAdapter {
List list = new ArrayList<>();

static class DataHandler {
    ImageView icon;
    TextView name;
    TextView location;
    TextView spawnTime;
    TextView slot;
    TextView rating;
}

public NodeAdapter(Context context, int resource) {
    super(context, resource);
}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return this.list.size();
}

@Override
public Object getItem(int position) {
    return this.list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row = convertView;
    DataHandler handler;
    if (convertView == null){
        LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.row_layout, parent, false);
        handler = new DataHandler();
        handler.icon = (ImageView)row.findViewById(R.id.imageViewNodeIcon);
        handler.name = (TextView)row.findViewById(R.id.textViewNodeName);
        handler.location = (TextView)row.findViewById(R.id.textViewNodeLocation);            
    }
    else {
        handler = (DataHandler)row.getTag();
    }

    NodeDataProvider dataProvider;
    dataProvider = (NodeDataProvider)this.getItem(position);
    handler.icon = dataProvider.getNodeIcon_resource();

    handler.name.setText(dataProvider.getNodeName_resource());
    handler.location.setText(dataProvider.getNodeLocation_resource());

    return row;
    }
}

Here I need to provide an ImageView to my handler and I can't seem to find a good way convert the id I would use back into a workable ImageView to provide to my handler.

Upvotes: 0

Views: 552

Answers (2)

Frederik Cretel
Frederik Cretel

Reputation: 3

Ok I solved it. I never needed an Imageview in to feed to my handler I could just use:

handler.icon.setImageResource(dataProvider.getNodeIcon_resource());

I was overlooking the fact that I could just set the ImageResource lol.

Kudos to P.Melch.

Upvotes: 0

P.Melch
P.Melch

Reputation: 8180

Why do you need to create an ImageView at all? Why not only store the image id in the node. Then, once you want to actually use the Node to initialize some layout you just create/re-use an ImageView and set the resource id.

Upvotes: 1

Related Questions