1

I am a beginner to Android programming and I am having tremendous trouble with this simple task.

I have a ArrayList:

ArrayList<HashMap<String, String>> tableList = new ArrayList<HashMap<String, String>>();

And I want to add each key/value pair to a ListView.

How do I add them to the ListView? I have seen countless explanations of using the adapter online but they all use variables I know nothing about.

1 Answer 1

2

Build your own adapter class:

public class MyAdapter extends BaseAdapter {

    private Activity activity;
    private HashMap<String, String> map;

    public MyAdapter(Activity activity, HashMap<String, String> map) {
        this.activity = activity;
        this.map = map;
    }

    public int getCount() {
        return map.size();
    }    

    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.my_list_item,
                    null);
        }

            // Recommended to use a list as the dataset passed in the constructor.
            // Otherwise not sure how you going to map a position to an index in the dataset.
            String key = // get a key from the HashMap above
            String value = map.get(key);

            TextView keyView = convertView.findViewById(R.id.item_key);
            keyView.setText(key);

            TextView valueView = convertView.findViewById(R.id.item_value);
            valueView .setText(value);

        return convertView;
    }    
}

Then, pass it into your ListView setAdapter method:

MyAdapter myAdapter = new MyAdapter(this, map);
ListView listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(myAdapter);

Sample layout/my_list_item.xml:

<LinearLayout xmlns:android:http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

    <TextView
        android:id="@+id/item_key"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

    <TextView
        android:id="@+id/item_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>
Sign up to request clarification or add additional context in comments.

4 Comments

What is R.layout.my_list_item? I only have a listview.
I'm also not sure how to populate the view with my items.
You can create your own custom layout for how you want a row to look like. That is R.layout.my_list_item.
Updated answer to include sample layout file and sample code. Hope this helps you :)

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.