I want to setup Listview Using ArrayAdapter in Fragment.Here is my All Code.

Fragment :::

public class HairstylesFragment extends Fragment {
    ListView HairStylelz;
    Integer[] imageId = {
            R.drawable.man_menu_buzz,
            R.drawable.man_menu_buzz,
            R.drawable.man_menu_buzz,
            R.drawable.man_menu_buzz,
            R.drawable.man_menu_buzz,
            R.drawable.man_menu_buzz,
            R.drawable.man_menu_buzz
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hairstyles, container, false);
        HairStylelz=(ListView)view.findViewById(R.id.Listview_HairStyle);

        HairStylez_Adapter  adapter = new HairStylez_Adapter(view.getContext(),imageId);
        HairStylelz.setAdapter(adapter);

        return view;
    }
}

ArrayAdapter::

public class HairStylez_Adapter extends ArrayAdapter {

     Integer[] imageId;
    Context context;



    public HairStylez_Adapter(Context context, Integer[] imageId) {
        super(context, R.layout.listview_adapter_hairstyle);
        this.context=context;
        this.imageId=imageId;

    }


    @Override
    public View getView(int position, View view, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView= inflater.inflate(R.layout.listview_adapter_hairstyle, null, true);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.Hairstyle_imageview);
        imageView.setImageResource(imageId[position]);

        Log.d("settingup","");
        return rowView;
    }
}

But Listview is Not Setting Up Its Showing Blank Screen. is there Any Missing in my Code ? Help to Setup Listview In Fragment

Thanks

Ответы (3)

As per your use case i can suggest you to use BaseAdapter and override getCount(), getItemId(int position) and getItem(int position) methods and return imageId.length, 0 and imageId[position]. Below is sample implementation of BaseAdapter.

public class CustomBaseAdapter extends BaseAdapter {
        Integer[] imageId;
        Context context;
         
        public CustomBaseAdapter(Context context, Integer[] imageId) {
            this.context=context;
            this.imageId=imageId;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView= inflater.inflate(R.layout.listview_adapter_hairstyle, null, true);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.Hairstyle_imageview);
            imageView.setImageResource(getItem(position));

            Log.d("settingup","");
            return rowView;
        }

        @Override
        public int getCount() {     
            return imageId.length;
        }

        @Override
        public Integer getItem(int position) {
            return imageId[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
}

     

Inside getView() method :

if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listview_adapter_hairstyle, parent, true);
}
ImageView imageView = (ImageView) view.findViewById(R.id.Hairstyle_imageview);
imageView.setImageResource(imageId[position]);

Log.d("settingup","");
return view; //You are initialing view and returning rowView which is wrong.

You should inflate the view only once if it is null.And while inflating pass parent.

You need to add String data to ArrayAdapter by calling add(someValue)

2022 WebDevInsider