0

I wanted to use a list view inside a list view item. The code is provided below.

MainActivity.java

String[][] topicLists = {{"one,one","one,two","one,three","one,four","one,five"}
                            ,{"two,one","two,two","two,three","two,four"}
                            ,{"three,one","three,two","three,three","three,four","three,five","three,six"}
                            ,{"four,one","four,two","four,three"}};

    ListView listView = (ListView) findViewById(R.id.recyclerView);
    CardsAdapter cardsAdapter = new CardsAdapter(this, topicLists);
    listView.setAdapter(cardsAdapter);

CardsAdapter.java

String[][] allTopics = {};

public CardsAdapter(@NonNull Context context, String[][] unitsAndTopics) {
    super(context, 0,unitsAndTopics);
    allTopics = unitsAndTopics;
}

@Nullable
@Override
public Object getItem(int position) {
    ArrayList<String> all = new ArrayList<String>();
    for (int i = 0; i <allTopics[position].length ; i++) {
        all.add(allTopics[position][i]);
        Log.e("",allTopics[position][i]);
    }

    return all;
}

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

    View listItem = convertView;
    if (listItem==null)
        listItem = LayoutInflater.from(getContext()).inflate(R.layout.card_view,parent,false);

    ArrayList<String> currentItem = (ArrayList<String>) getItem(position);

    ListView topic = (ListView) listItem.findViewById(R.id.listView);
    ListAdapter listAdapter = new ListAdapter(getContext(),currentItem);
    topic.setAdapter(listAdapter);

    return listItem;
}

ListAdapter.class

    public ListAdapter(@NonNull Context context, ArrayList<String> topics) {
    super(context, 0,topics);
}

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

    View listItem = convertView;
    if (listItem==null)
        listItem = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);

    final String currentItem = (String) getItem(position);

    TextView topic = (TextView) listItem.findViewById(R.id.topic_text_view);
    topic.setText(currentItem);

    Button search = (Button) listItem.findViewById(R.id.search_button);
    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
                Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                String term = currentItem;
                intent.putExtra(SearchManager.QUERY, term);
                getContext().startActivity(intent);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    });

    return listItem;
}

The list live have only first element

In the above image, the only first element of all the arrays is present in the list. rest all are not showing up Any suggestion ll be helpful.

Faysal Ahmed
  • 6,464
  • 5
  • 23
  • 43
Akhil P
  • 41
  • 1
  • 6

1 Answers1

1

Your Code is Working Proper look here

enter image description here

I make first listview item xml file height fix 150dp (card_view) and its working proper here is all code

1) card_view :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

2) ListViewAdapter :

import android.app.SearchManager;
    import android.content.Context;
    import android.content.Intent;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ListAdapter;
    import android.widget.TextView;

    import java.util.ArrayList;


    public class ListNewAdapter extends BaseAdapter{

        private static final String TAG = "ListNewAdapter";
        private final ArrayList<String> topics;
        private final Context context;

        public ListNewAdapter(@NonNull Context context, ArrayList<String> topics) {
           this.context = context;
           this.topics = topics;
        }

        @Override
        public int getCount() {
            Log.d(TAG, "getCount:"+topics.size());
            return topics.size();
        }

        @Override
        public Object getItem(int i) {
            return topics.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

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

            View listItem = convertView;
            if (listItem==null)
                listItem = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);

            final String currentItem = (String) getItem(position);

            TextView topic = (TextView) listItem.findViewById(R.id.topic_text_view);
            topic.setText(currentItem);


            return listItem;
        }
    }

3) CardAdapter :

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;


public class CardsAdapter extends BaseAdapter {

    private static final String TAG = "CardsAdapter";
    private final Context context;
    String[][] allTopics = {};

    public CardsAdapter(@NonNull Context context, String[][] unitsAndTopics) {
        this.context = context;
        allTopics = unitsAndTopics;
    }

    @Override
    public int getCount() {
        return allTopics.length;
    }

    @Nullable
    @Override
    public Object getItem(int position) {
        ArrayList<String> all = new ArrayList<String>();
        for (int i = 0; i <allTopics[position].length ; i++) {
            all.add(allTopics[position][i]);
            Log.d(TAG, "getItem:"+all);
        }

        return all;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

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

        View listItem = convertView;
        if (listItem==null)
            listItem = LayoutInflater.from(context).inflate(R.layout.card_view,parent,false);

        ArrayList<String> currentItem = (ArrayList<String>) getItem(position);

        ListView topic = (ListView) listItem.findViewById(R.id.listView);
        ListNewAdapter listNewAdapter = new ListNewAdapter(context,currentItem);
        topic.setAdapter(listNewAdapter);

        return listItem;
    }
}
Dhara Patel
  • 361
  • 5
  • 16
  • Thank you Dhara Patel. I did it, its working, but when i populate the inner list its not scrolling. Any suggestion, it will be very helpful. – Akhil P Jan 15 '18 at 13:40
  • for NestedScrollView you can refer this https://stackoverflow.com/a/6211286/4427519 – Dhara Patel Jan 16 '18 at 04:55