0

I'm new to android and I need help. I couldn't find a link that explains me what I'm going to ask, that's why I'm asking here.

I have a listview with one layout.

My customLayout have some textviews and images, all that I get from 2 jsonArrays inside one json object.

TEXT1 TEXT2 IMAGE1

I want to populate this layout from 2 different Array Lists.

I get this array lists from 2 jsonArrays on my webservice.

My class is a simple one, like this:

public class Example{

private String text;
private String text2;
private String imageLink;

Constructor...
Getter and Setter...
}

I can put the values of ArrayList1 inside the customLayout but not the values of ArrayList2.

Like this:

public class CustomListAdapterStreams extends ArrayAdapter<StreamsData> {


   ArrayList<MyClass> ArrayList;
    ArrayList<MyClass> ArrayList2;
    Context context;
    int resource;


    public CustomListAdapterStreams(Context context,  int resource,  ArrayList<MyClass> ArrayList, ArrayList<MyClass> ArrayList2;) {

        super(context, resource, ArrayList);
        this.ArrayList= ArrayList;
        this.context = context;
        this.resource = resource;
        this.ArrayList2= ArrayList2;
    }

@Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            if (convertView == null){
                LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.activity_custom_list_adapter, null);
            }

            MyClass myclass= ArrayList.get(position);
    String getUsername = myclass.getUsername();
    String quality = myclass.getQuality();

    TextView txtUsername = (TextView) convertView.findViewById(R.id.txtUsername);
    txtUsername.setText(getUsername);

    and so on

So I need to understand how to put in the same custom layout, in the same views, dinamically, different values. Like when Array List 1 has nothing left, populate the rest of the list with the values of Array List 2.

EDIT:

Answers saying I should put both json arrays in the same arraylist.

By a rookir mistake I thought that I shouldn't mix up two JSONArrays, into the same ArrayList but that is what solved my problems.

Thank you Wizard, I know it was a stupid question. Rookie mistake here.

7
  • Why can you not just combine the data in the lists after you've received them from the server? Commented Mar 17, 2017 at 12:39
  • you can merge the two arraylist in a single arraylist and use it for your custom listview Commented Mar 17, 2017 at 12:40
  • Exactly, Make one arrayList Commented Mar 17, 2017 at 12:40
  • they are from 2 different json arrays. CanI put both in the same ArrayList? Commented Mar 17, 2017 at 12:40
  • So what? Put items of arrayList2 into arrayList1. Do you want me to show how you do that? Commented Mar 17, 2017 at 12:42

2 Answers 2

2

I would join the two arrays.

for (Example item: arrayList2)
{
    arrayList1.add(item)
}

If you need to show different data for each array item, at the time of joining I would put a property to differentiate, type:

for (Example item: arrayList2)
{
   item.setIsArrayList2(true)
    arrayList1.add(item)
}

And it would apply to the Adapter where you could show the two and so on.

One tip: Use optString instead of getString to not get a JSONException in case it comes null

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

1 Comment

Thank you for the answer. Although Wizard answered pretty well your answer is equivalent and helpful. I appreciate.
1

Seems your two arrayList have same Pojo..

What you can do is Merge two arrayList and keep data in one arrayList before setting an adapter;

Here is how you can do that -

arraList1.addAll(arraList2)

2 Comments

Thank you Sr. I know it was a stupid question but I got confused about putting 2 jsonArrays with different names but same content inside one ArrayList. I thought I shouldn't do that for a reason but that was a rookie mistake. Thanks again.
That is what StackOverFlow for ;-)

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.