2

i have a created a fragment in which i want to display listview but my fragment is dislplayed wtihout the listview nor i am getting an error

i have read many solutions to this question but i can't find the right one. a small help would be great.thank you!

public class ServicesFragment extends Fragment {
    public TextView servicesName;
    ListView listView;
    String[] servicesNameArray;
    int[] serviceImages = {
            R.drawable.ic_menu_camera,
            R.drawable.ic_menu_camera,
            R.drawable.ic_menu_camera,
            R.drawable.ic_menu_camera,
            R.drawable.ic_menu_camera,
            R.drawable.ic_menu_camera,
            R.drawable.ic_menu_camera,
            R.drawable.ic_menu_camera
    };
     public ServicesFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.services_fragment, container, false);
        listView = (ListView) view.findViewById(R.id.listView_services);
        ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);
        listView.setAdapter(adapter);
        Resources resources = getResources();
        resources.getStringArray(R.array.servicesName);
        return view;
    }

  public class ServicesAdapter extends ArrayAdapter<String> {

 Context c;
        int[] servicesImageArray;
        String[] servicesNameArray;
        public ServicesAdapter(Context context, String[] servicesNameArray, int[] serviceImages) {
            super(context, R.layout.services_layout, R.id.listView_services);
            this.c = context;
            this.servicesImageArray = serviceImages;
            this.servicesNameArray = servicesNameArray;
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.services_layout, parent, false);
            ImageView serviceImages = (ImageView) view.findViewById(R.id.image_of_services);
            TextView servicesName = (TextView) view.findViewById(R.id.name_of_services);
            serviceImages.setImageResource(servicesImageArray[position]);
            servicesName.setText(servicesNameArray[position]);
            return view;}}}
4
  • make sure you pass desired data to your list adapter Commented Mar 26, 2017 at 13:18
  • yes i am passing it Commented Mar 26, 2017 at 13:19
  • where you fill servicesNameArray with data ?? it seem it passed empty Commented Mar 26, 2017 at 13:21
  • i have defined the array in the string.xml file and i am getting in the fragment using getResources() Commented Mar 26, 2017 at 13:30

2 Answers 2

2

You are not assigning value to the string array: use

servicesNameArray = getResources().getStringArray(R.array.servicesName);  

and instead of :

 ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);

use the activity context:

ServicesAdapter adapter = new ServicesAdapter(this.getActivity(), servicesNameArray, serviceImages);

you need to notify you adapter about your data set size:

super(context, R.layout.services_layout, servicesNameArray); //change here
Sign up to request clarification or add additional context in comments.

2 Comments

i know you by the way.....u have answered one of my question long time ago
Great!...**Always happy to help**
0

I think this issue cause you dont pass data in correct way

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.services_fragment, container, false);
Resources resources = getResources();
        servicesNameArray = resources.getStringArray(R.array.servicesName);
        listView = (ListView) view.findViewById(R.id.listView_services);
        ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);
        listView.setAdapter(adapter);

        return view;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.