-1

I have a ListView which is working just fine inside my Fragment.

public class MyFragment extends Fragment {

    String[] itemname ={ "item 1", "item 2", "item 3"};

    ListView my_list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_example, container, false);

        my_list = (ListView) v.findViewById(R.id.listView1);
        ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemname); 

         my_list.setAdapter(adapter);  

        return v;
        }
}

Therefore, when I try to set it to my custom ListView, it returns a NullPointerException.

ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(getActivity(), R.layout.custom_list, itemname); 

         list_poderes.setAdapter(adapter);  

What`s going on over here? How should I implement it?

Machado
  • 14,105
  • 13
  • 56
  • 97

1 Answers1

1

You can not achieve this by simply passing the list as the resource id of adapter. You may go through following steps.

  1. Create custom view for your listview row.
  2. Define custom adapter class extended by BaseAdapter
  3. Inflate above (01) view within the getView() method.
  4. Prepare your data model and pass into adapter within your fragment.

You will find suitable example here

Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45