1

我正在处理quiz application. 我在这里使用Viewpager indicator。在这我已经实现了listview with radio button

我的问题是,如何diffent valuesviewpager.

我尝试了谷歌,但我没有找到解决方案。请帮我。谢谢你

见下图:

在此处输入图像描述 在此处输入图像描述

检查我的代码:

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

    }
}
4

2 回答 2

0

好吧,我建议为列表所在的每个“视图”设置一个 ID,然后当您想要检索该值时,您只需要循环遍历从 0 到适配器大小减 1 的所有 id。这将在每次迭代中为您提供完整视图,因此之后您需要迭代视图的子视图以找到复选框。希望能帮助到你。

于 2012-08-22T18:33:26.630 回答
0

试试这个:

首先根据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"};

将参数添加到 getItem listArray[pos]Like 中:

@Override
    public Fragment getItem(int pos) {

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

    }

并更改为 ItemFragment 类 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;
    } 
于 2012-08-23T12:07:27.827 回答