Home » Android » Android Studio-How do I apply A Custom Adapter to a listview in A fragment?

Android Studio-How do I apply A Custom Adapter to a listview in A fragment?

Posted by: admin November 1, 2017 Leave a comment

Questions:

Sorry, I’m confused at the moment. I was able to make a custom list for an activity, but could someone help me figure out how to translate the same code to make a custom list for a fragment? Here’s my code

public class Digital_Fragment extends Fragment
{
    String[] art_Names;
    TypedArray pics;
    String[] artist_Names;
    String[] desc;

    List<RowItem> rowItems;
    ListView myListView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(first_layout, container, false);
        return rootView;

        rowItems = new ArrayList<RowItem>();
        art_Names = getResources().getStringArray(R.array.Digital_Arts);
        pics = getResources().obtainTypedArray(R.array.DigitalArt_Pics);
        artist_Names = getResources().getStringArray(R.array.Digital_Artist_Names);
        desc = getResources().getStringArray(R.array.Digital_Descriptions);

        for (int i = 0; i < art_Names.length; i++) {
            RowItem item = new RowItem(art_Names[i], pics.getResourceId(i, -1), artist_Names[i], desc[i]);
            rowItems.add(item);
        }

        myListView = (ListView) getView().findViewById(R.id.list);
        CustomerAdapter adapter = new CustomerAdapter(getActivity(), rowItems);
        myListView.setAdapter(adapter);
    }
}
Answers: