1

I have a layout defined with some buttons, and a ListView widget. I also have a layout xml defined for each row in the ListView, with 2 TextViews. I also have data in the format List<String[]> where the String[] is populated with 3 items. I want to display the data in the ListView, with 2 of the 3 items from the String array (contained in each element of the list) assigned to one of the TextView rows in the ListView layout.

I can get a ListView, using one of the default views, to display the full list of just a single item in the array, by breaking it down into a single array of values, and using ArrayAdapter<String>, but I dont know how to do it for multiple rows in the ListView layout.

Would much appreciate any help.

Also, in addition, if the data in the original List<String[]> list should change, how can I refresh the ListView widget?

Thanks for any advice.

1 Answer 1

1

You should create your own extension of the ArrayAdapter:

class MyAdapter extends ArrayAdapter<String[]>{
        public MyAdapter(Context context, int textViewResourceId, ArrayList<String[]> objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if( convertView== null ) convertView = getLayoutInflater().inflate(R.layout.your_layout, null);

            String[] strings = getItem(position);
            TextView line1 = (TextView)convertView.findViewById(R.id.line1);
            line1.setText(strings[0])
            TextView line2 = (TextView)convertView.findViewById(R.id.line2);
            line2.setText(strings[0])
            //...

            return convertView;
        }
    }

To refresh your list, call the adapters notifyDataSetChanged() method.

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

3 Comments

FYI: this is the third time in just a few days I have answered a question with "create your own adapter" :p
Fantastic, thankyou. I guess I should have mentioned, I'm completely new to android coding (and in fact java), so although I did search, maybe I was looking for the wrong thing. Anyway, thanks for taking the time to respond. A quick point, you have return layout in here, I had to change it to return convertView, which seems to work. Any hints about how I can refresh this view from say another button in the activity when the underlying data has changed?
Edited my answer to include that (and also fix the small layout problem. (I tend to create a new view named layout in my adapters if it is a layout rather than just a view...)) Good luck with the programming :)

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.