简体   繁体   中英

Different items on the listview of the viewpager - Android

I am Working on the quiz application . Here I am using Viewpager indicator .In this I have implemented listview with radio button .

My question is,How can I got the diffent values for listviews on every page of the viewpager .

I tried Google,But I don't find solution. Please help me. Thank You

see below Images:

在此处输入图片说明在此处输入图片说明

Check My code :

 class PagerAdapter extends FragmentPagerAdapter implements SwipeyTabsAdapter{

    public final String[] list = new String[]
        {"Operating System", "Product Of Google", "Both Above", "None of above"}
; 

    private final Context mContext;

    public PagerAdapter(Context context, FragmentManager fm) {
        super(fm);

        this.mContext = context;
    }


    @Override
    public Fragment getItem(int pos) {

        return ItemFragment.newInstance(QUES[pos]);
        //return ItemFragment.newInstance(QUES[pos % TITLES.length]);

    }

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

    public TextView getTab(final int position, SwipeyTabs root) {
        TextView view = (TextView) LayoutInflater.from(mContext).inflate(
                R.layout.swipey_tab_indicator, root, false);
        view.setText(TITLES[position]);
        view.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mViewPager.setCurrentItem(position);

            }
        });
        return view;
    }


}

public static class ItemFragment extends  Fragment{

    static ItemFragment newInstance(String title) {
        ItemFragment f = new ItemFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();

        args.putString("title",title);
        f.setArguments(args);

        System.out.println("value of the title : ================>"+title);
        return f;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.view_question, container, false);
        View tv = v.findViewById(R.id.text);
        final View listvw = v.findViewById(R.id.listview);

         final String title = getArguments().getString("title");
         ((TextView)tv).setText(title);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_single_choice, list);
        ((ListView) listvw).setAdapter(adapter);
        ((ListView) listvw).setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        return v;
    }

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


    }

    public void onListItemClick(ListView l, View v, int position, long id) {

        Log.i("FragmentList", "Item clicked: " + id);

    }
}

Well i would recommend to set an ID to every "view" where the list is, and then when you want to retrieve the value you just need to loop trough all the id from 0 to the size of the adapter minus 1. and that will give you in each iteration the FULL VIEW, so after that you need to iterate the childs of the view to find the checkbox. Hope it helps.

Try this One :

First of all initialize the listvalue as per display below

/*public final String[] list = new String[]
    {"Operating System", "Product Of Google", "Both Above", "None of above"};*/


public static final String[] listArray = new String[]
        {
        "Operating System,Product Of Google,Both Above,None of above",
        "os,Google,Both,None",
        "Operating System,Product Of Google,Both Above,None of above",
        "os,Google,Both,None",
        "Operating System,Product Of Google,Both Above,None of above",
        "os,Google,Both,None",
        "Operating System,Product Of Google,Both Above,None of above",
        "os,Google,Both,None"};

Add the parameter into getItem listArray[pos] Like :

@Override
    public Fragment getItem(int pos) {

        return ItemFragment.newInstance(QUES[pos],listArray[pos]);

    }

And changes into ItemFragment class Like :

static ItemFragment newInstance(String title,String arrayList) {
        ItemFragment f = new ItemFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();

        args.putString("title",title);
        args.putString("listvalues",arrayList);
        f.setArguments(args);

        return f;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.view_question, container, false);
        View tv = v.findViewById(R.id.text);

        final String title = getArguments().getString("title");
        final String  Listvalues = getArguments().getString("listvalues");


        String[] arr=Listvalues.split(",");

        System.out.println("Array :"+arr.length);
        for(int i=0;i<arr.length;i++)
        {

          //  System.out.println("array"+i+":========================>"+arr[i]);
        } 

         ((TextView)tv).setText(title);

        final View listvw = v.findViewById(R.id.listview);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_single_choice, arr);
        ((ListView) listvw).setAdapter(adapter);
        ((ListView) listvw).setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        return v;
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM