-5

Currently, I have 3 arrays. I am trying for couple of hours. Can I implement this function in any dynamic way. For me For loop didn't work. is there any way to dynamically implement this. I will be glad , if you guys help to solve this.

 switch(pageNumber){
            case 0:
                final ListView list1 = (ListView)rootView.findViewById(R.id.list);
                BinderData bindingData = new BinderData(this.getActivity(), d[0]);
                list1.setAdapter(bindingData);
                list1.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                        if (d[0].get(position).getItemType() == 0
                                || d[0].get(position).getItemType() == 3
                                || d[0].get(position).getItemType() == 2)
                            return;
                        Intent intent = new Intent(Schedule.this.getActivity(),
                                ContentExtended.class);
                        intent.putExtra("title", d[0].get(position).getTitle());
                        intent.putExtra("content", d[0].get(position).getContent());
                        startActivity(intent);
                    }
                });
                break;
            case 1:
                final ListView list2 = (ListView)rootView.findViewById(R.id.list);
                BinderData bindingData1 = new BinderData(this.getActivity(), d[1]);
                list2.setAdapter(bindingData1);
                list2.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                        if (d[1].get(position).getItemType() == 0
                                || d[1].get(position).getItemType() == 3
                                || d[1].get(position).getItemType() == 2)
                            return;
                        Intent intent = new Intent(Schedule.this.getActivity(),
                                ContentExtended.class);
                        intent.putExtra("title", d[1].get(position).getTitle());
                        intent.putExtra("content", d[1].get(position).getContent());
                        startActivity(intent);
                    }
                });
                break;
            case 2:
                final ListView list3 = (ListView)rootView.findViewById(R.id.list);
                BinderData bindingData2 = new BinderData(this.getActivity(), d[2]);
                list3.setAdapter(bindingData2);
                list3.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                        if (d[2].get(position).getItemType() == 0
                                || d[2].get(position).getItemType() == 3
                                || d[2].get(position).getItemType() == 2)
                            return;
                        Intent intent = new Intent(Schedule.this.getActivity(),
                                ContentExtended.class);
                        intent.putExtra("title", d[2].get(position).getTitle());
                        intent.putExtra("content", d[2].get(position).getContent());
                        startActivity(intent);
                    }
                });
                break;

        }
4

3 回答 3

2

在我看来,除了d数组的索引之外,整个代码都被复制了。所以,它会是这样的:

public void method(int pageNumber) {
     final ListView list = (ListView)rootView.findViewById(R.id.list);
     BinderData bindingData = new BinderData(this.getActivity(), d[pageNumber]);
     list.setAdapter(bindingData);
     list.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> a, View v, int position, long id) {
             if (d[pageNumber].get(position).getItemType() == 0
                            || d[pageNumber].get(position).getItemType() == 3
                            || d[pageNumber].get(position).getItemType() == 2)
                 return;
             Intent intent = new Intent(Schedule.this.getActivity(),
                            ContentExtended.class);
             intent.putExtra("title", d[pageNumber].get(position).getTitle());
             intent.putExtra("content", d[pageNumber].get(position).getContent());
             startActivity(intent);
         }
     });
}
于 2013-06-27T09:15:58.963 回答
0

You can't use for loops for switching. you can use if. as I see your code only difference in each case in array d indexes only. So you don't want to switch or if else either.

public void onClick(int pageNumber) { 
 //your implementaion
}

Now you can pass page number to this method

于 2013-06-27T09:22:26.600 回答
0

onClick 检查是在我看到的 pageNumber 上,在每种情况下,我看到的唯一区别是数组 d 的索引。在 onClick 方法中试试这个:

       final ListView list1 = (ListView)rootView.findViewById(R.id.list);
        BinderData bindingData = new BinderData(this.getActivity(), d[pageNumber]);
        list1.setAdapter(bindingData);
        list1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                if (d[0].get(position).getItemType() == 0
                        || d[pageNumber].get(position).getItemType() == 3
                        || d[pageNumber].get(position).getItemType() == 2)
                    return;
                Intent intent = new Intent(Schedule.this.getActivity(),
                        ContentExtended.class);
                intent.putExtra("title", d[pageNumber].get(position).getTitle());
                intent.putExtra("content", d[pageNumber].get(position).getContent());
                startActivity(intent);
            }
        });
于 2013-06-27T09:16:50.077 回答