2

i'm doing a system of news feed, so, i've a difficulty for adapter my ArrayAdapter insert value into ListView, with: "username, name, picture, content and others values" i'm newbie in Android Studio.

Searching the internet I found this function

                HashMap<String,String> persons = new HashMap<String,String>();

            persons.put(TAG_ID,id);
            persons.put(TAG_NAME,name);
            persons.put(TAG_ADD,address);

            personList.add(persons);
        }

        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, personList, R.layout.list_item,
                new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                new int[]{R.id.id, R.id.name, R.id.address}
        );

        list.setAdapter(adapter);

But, i use ArrayList and ArrayAdapter:

ArrayList<Post> postList =
                    new JsonConverter<Post>().toArrayList(s, Post.class);

            ArrayList<String> postador = new ArrayList<String>();
            for(Post value: postList){
                postador.add(value.id_postador);
                postador.add(value.post_legenda);
                if(value.post_foto != null) {
                    postador.add(value.post_foto);
                }
                postador.add(value.post_data);
                postador.add(value.curtidas);

            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    MainActivity.this,
                    android.R.layout.simple_list_item_1, postador
                    );

            lvPost.setAdapter(adapter);

I tried it, but no success.

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    MainActivity.this,
                    R.layout.list_item, postador, new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                new int[]{R.id.id, R.id.name, R.id.address}
                    );

There a method convert ListAdapter in ArrayAdapter or adapter my ArrayAdapter for ListAdapter?

1 Answer 1

1

If you are a newbie to Android, I would like to recommend few things to you.

1). Start Using RecyclerView from the very beginning instead of ListView. It is more advanced, flexible and efficient version of ListView. You can find a sample demo for the RecyclerView here and here

2). If you still want to use ListView. Then you further have two options, either to use BaseAdapter or ArrayAdapter.

a). Base Adapter as the name suggests, is a base class for all the adapters. If you need more flexibility, you can go with it. You can find the demo sample here and here

b). On the other hand, ArrayAdapter is a concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.To use something other than TextViews (like in your case) for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want. You can find the demo sample here and here.

Hope it will help you in future and also I suppose you will get answer to your question in the specified demo samples. Thanks and have a good day.. :)

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

2 Comments

Thank you, this method solved my problem. I had problems to understand this method, but then after i had success.
Haha, thanks! This is the result of my project, an ealier version. link But there're a method of change the background of the cardview for white?

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.