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;
}
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.