1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by Kenneth Dionisi, Oct 22, 2017.

  1. Kenneth Dionisi

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Oct 22, 2017

    Oct 22, 2017
    2
    0
    5
    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);

    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, pics.getResourceId(i, -1), artist_Names, desc);
    rowItems.add(item);
    }

    myListView = (ListView) myListView.findViewById(R.id.listFrag1);
    CustomerAdapter adapter = new CustomerAdapter(getActivity(),rowItems);
    myListView.setAdapter(adapter);

    return rootView;
    }
    }
     

    Advertisement

    #1 Kenneth Dionisi, Oct 22, 2017
    Last edited: Oct 22, 2017
  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #10
    Points:
    1,988
    Posts:
    8,530
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,530
    12,702
    1,988
    Male
    Software developer
    South West of England
    Hi. Could you explain what problems you're running into please?
     
  3. Kenneth Dionisi

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Oct 22, 2017

    Oct 22, 2017
    2
    0
    5
    I'm using a custom list and a custom adapter in this fragment, and when I run the app and go to this fragment, the app crashes.
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #10
    Points:
    1,988
    Posts:
    8,530
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,530
    12,702
    1,988
    Male
    Software developer
    South West of England

Share This Page

Loading...