First, You have to read this tutorial which simply extends ArrayAdapter and create view according to requirement and then look this example, it explains almost all things those you want to implement.
add multiple data on a custom listview
-
23-07-2023 - |
Question
I have a mainactivity which calls a fragment to load a new xml with listview on it that calls another xml for the items on the listview. How can I put multiple data on it. I can only add one data on the textview. here is my fragment that loads the data
public class HomeFragment extends Fragment {
protected ProgressBar mProgressBar;
protected JSONArray blogPostData;
protected String[] blogPostTitle;
protected String[] blogPostDate;
protected String[] blogPostAuthor;
private ListView listView;
private View rootView;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
listView = (ListView) rootView.findViewById(R.id.article_list);
mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressBarHome);
ArticleController articleController = new ArticleController(mListener);
articleController.execute();
return rootView;
}
MyListener mListener = new MyListener(){
@Override
public void onComplete(JSONArray result) {
blogPostData = result;
updateArticleList();
List<String> stringList = new ArrayList<String>(Arrays.asList(blogPostTitle));
List<String> stringList1 = new ArrayList<String>(Arrays.asList(blogPostDate));
List<String> stringList2 = new ArrayList<String>(Arrays.asList(blogPostAuthor));
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(getActivity(), R.layout.layout_listview_article, R.id.titleTV, stringList);
arrayAdapter1.add(getActivity(), R.layout.layout_listview_article, R.id.authorTV, stringList1);
arrayAdapter1.add(getActivity(), R.layout.layout_listview_article, R.id.authorTV, stringList2);
listView.setAdapter(arrayAdapter1);
mProgressBar.setVisibility(View.INVISIBLE);
}
};
protected void updateArticleList(){
try {
blogPostTitle = new String[blogPostData.length()];
String[] blogPostDate = new String[blogPostData.length()];
String[] blogPostAuthor = new String[blogPostData.length()];
for (int i = 0; i < blogPostData.length(); i++)
{
JSONObject jsonObject = blogPostData.getJSONObject(i);
String title = jsonObject.getString("title");
title = Html.fromHtml(title).toString();
blogPostTitle[i] = title;
String date = jsonObject.getString("created_at");
date = Html.fromHtml(date).toString();
blogPostDate[i] = date;
String author = jsonObject.getString("author");
author = Html.fromHtml(author).toString();
blogPostAuthor[i] = author;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow