简体   繁体   English

ViewPager的ListView上的不同项目-Android

[英]Different items on the listview of the viewpager - Android

I am Working on the quiz application . 我正在研究quiz application Here I am using Viewpager indicator .In this I have implemented listview with radio button . 我在这里使用Viewpager indicator 。在此我实现了listview with radio button

My question is,How can I got the diffent values for listviews on every page of the viewpager . 我的问题是,如何在viewpager每一页上获取listviews的diffent values

I tried Google,But I don't find solution. 我尝试过Google,但是找不到解决方案。 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. 好吧,我建议您为列表所在的每个“视图”设置一个ID,然后当您要检索该值时,只需将所有ID从0循环到适配器的大小减去1,就可以了。在每次迭代中为您提供完整视图,因此之后您需要迭代视图的子级以找到复选框。 Hope it helps. 希望能帮助到你。

Try this One : 试试这个:

First of all initialize the listvalue as per display below 首先按照下面的显示初始化listvalue

/*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 : 将参数添加到getItem listArray[pos]如下所示:

@Override
    public Fragment getItem(int pos) {

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

    }

And changes into ItemFragment class Like : 并更改为ItemFragment类,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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