0

I am New Android development. In My application I have two ArrayLists.

One ArrayList contains HashTbale. Another one contains HashTable

Now How can i set adpter and how to show in Listview image from Second ArrayList and Text From from ArrayList in ListView Row.

I am not able to get exact solution .

Please any one help in this issue.

Thanks in advance

3
  • if possible then give more detail of both the HashMap values. If possible i can suggest you to merge them into a single hashmap. Easy to handle. Commented Jul 5, 2012 at 6:13
  • @PareshMayani Thanks for your Response.My Exact requirement is in LIstView i need to show image and text. But i cant maintain both in one ArrayList Thats Why i am using two arraylist. But i am not able to how to set adpter for listview using two ArrayLists Commented Jul 5, 2012 at 6:16
  • Just use HashMap arraylist which store both Image path and your text and set that single ArrayList to your Adapter. Also you can make a Custom Object ArrayList for your requirement. Just google you can find many examples.. Commented Jul 5, 2012 at 6:20

2 Answers 2

2

If you want to display image and text in each row and if you are using 2 arraylist for the same then here is the solutions. Try this

List<Integer> imageList = getImageList();  //list of drawable ids
List<String> values = getValues();

class CustomAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imageList.size();;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // inflate the layout which contains imageview and textview which are aligned horizontally. 
        //Assuming you inflated layout and got imageView and textview from that layout
        textView.setText(values.get(position));

        imageView.setImageResource(imageList.get(position));
        return convertView;
    }

}

//Code wont work if you directly copy paste. Use the logic and change accordingly

Instead of using 2 arraylist, my suggestion is you use HashMap. Imagepath as key and text as value.

Sign up to request clarification or add additional context in comments.

2 Comments

@PareshMayani i agree. But he wanted to use 2 arraylists. so only i gave this solution
what about if both arraylist has a different size. Or Text and Image are not related to each-other?
2

1st Suggestion: (Not proposed)

Do you know how to define Custom adapter for ListView? If yes then you can easily pass two arraylist to your custom adapter. And inside getView() method, you can display whatever details you wants in ListView

2nd Suggestion: (Proposed solution)

If You make one ArrayList of CustomObject then there is no need to pass two ArrayList in Adapter.

3 Comments

If He makes one ArrayList of CustomObject then there is no need to pass two ArrayList in Adapter.
@user370305 thats the idea i was trying to convey in my above comments.
1st suggestion not works if both arraylist has a different size. Or Text and Image are not related to each-other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.