1

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<String> {

     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

4
  • i m not sure but to me it seems you require to use Baseadapter, as you are using ArrayAdapter<String> and you are passing Integer array. Try override getCount(), getItemId(int position) and getItem(int position) methods and return imageId count, 0 and imageId[position]. OR try changing ArrayAdapter<String> to ArrayAdapter<Integer>. Also try adding adapter.notifyDataSetChanged() after you setAdapter Commented Jan 1, 2016 at 6:17
  • Thanks @RajenRaiyarela its Working You Saved My time. Just Add this In answer i will Give Right mark of this Commented Jan 1, 2016 at 6:51
  • Welcome. But m not sure which one of my suggestion worked as i gave you 2 - 3 suggestions to check. Commented Jan 1, 2016 at 7:01
  • I Just Use BaseAdapter Insted Of ArrayAdapter So Please Consider it in answer i will Give you Right Mark to your Answer @RajenRaiyarela Commented Jan 1, 2016 at 7:03

3 Answers 3

1

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;
        }
}

     

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

i have to setup only image in listview
1

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.

5 Comments

what is rowview ?? @Akshay Bhat
You are not update properly itr return view instead of rowview
Sorry, but pass parent while inflating. It will work. I updated
its Not Worinkig.i am Using Side Bar Menu.So is there Any diff Style to setup Listview in Fragment If yes than Give me Link for GiideThanks
Thanks For Help its Solve

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.