-2

我在 .json 中有 jsonParse 方法onCreateView。我的申请中有 3 页。每次,当我
打开一个一直创建的页面数据时,我想检查当前位置是否已经有东西。现在,我的问题是推荐的方法来检查ArrayList.

这是我的代码:

public class ScheduleSlideFragment extends Fragment {

    final static String ARG_PAGE = "page";

    private static ViewPager pager;

    public static int pageNumber;

    public static int PAGE_NUM = ScheduleMainActivity.NUM_PAGES ;

    public  ArrayList<ScheduleItem> data = new ArrayList<ScheduleItem>();

    public int getPageNumber;

    private void jsonParseData(int _getPageNumber) {
        try {
            BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this
                    .getResources().openRawResource(R.raw.program)));
            StringBuilder jsonBuilder = new StringBuilder();
            for (String line = null; (line = jsonReader.readLine()) != null;) {
                jsonBuilder.append(line).append("\n");
            }



            // Parse Json
            JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
            JSONArray jsonArray = new JSONArray(tokener);

            _getPageNumber = getPageNumber;


            JSONObject jsonObject = jsonArray.getJSONObject(_getPageNumber);
            String getDate = jsonObject.getString("date");
           // data.add(new ScheduleItem(getDate));
            JSONArray getFirstArray = new JSONArray(jsonObject.getString("events"));

            for (int i = 0; i < getFirstArray.length(); i++) {

                JSONObject getJSonObj = (JSONObject)getFirstArray.get(i);
                String time = getJSonObj.getString("time");
                //Log.e("Time Log",time);
                String type = getJSonObj.getString("type");
                String title = getJSonObj.getString("title");
                int typeId = getJSonObj.getInt("type_id");

                data.add(new ScheduleItem(time, title, typeId, getDate));
                Log.e("Check Size", String.valueOf(data.size()));

                /*
                 * Get Events
                 */
                if (typeId == 0) {

                    JSONArray getEventsArray = new JSONArray(getJSonObj.getString("events"));

                    for (int j = 0; j < getEventsArray.length(); j++) {

                        JSONObject getJSonEventobj = (JSONObject)getEventsArray.get(j);
                        int typeEventId = getJSonEventobj.getInt("type_id");

                        if (typeEventId == 1) {

                            String EventInfo = getJSonEventobj.getString("info");
                            String EventType = getJSonEventobj.getString("type");
                            String EventTitle = getJSonEventobj.getString("title");
                            String Eventtime = getJSonEventobj.getString("time");
                            data.add(new ScheduleItem(Eventtime, EventTitle, EventInfo,
                                    typeEventId, getDate));
                        } else {

                            String EventType = getJSonEventobj.getString("type");
                            String EventTitle = getJSonEventobj.getString("title");
                            String Eventtime = getJSonEventobj.getString("time");
                            data.add(new ScheduleItem(Eventtime, EventTitle, typeEventId,
                                    getDate));
                        }
                    }
                }

            }
            //Log.e("Check Date", String.valueOf(data.get(_getPageNumber).getDate()));
        } catch (Exception e) {
            Log.getStackTraceString(e);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.schedule, container, false);
        getPageNumber = pageNumber;
        /** 
         * JSON Parsing 
         */
        checker = getPageNumber + 1;
        jsonParseData(getPageNumber);

        /**
         * Set header date 
         */

        ((TextView)rootView.findViewById(R.id.tvDay)).setText(data.get(pageNumber).getDate().toString());
        final ListView list = (ListView)rootView.findViewById(R.id.list);
        BinderData bindingData = new BinderData(this.getActivity(), data);
        list.setAdapter(bindingData);

        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                if (data.get(position).getItemType() == 0
                        || data.get(position).getItemType() == 3
                        || data.get(position).getItemType() == 2)
                    return;
                Intent intent = new Intent(ScheduleSlideFragment.this.getActivity(),
                        ContentExtended.class);
                intent.putExtra("title", data.get(position).getTitle());
                intent.putExtra("content", data.get(position).getContent());
                startActivity(intent);
            }
        });

        ImageButton ibLeft = (ImageButton)rootView.findViewById(R.id.ibLeft);
        if (pageNumber == 0)
            ibLeft.setVisibility(View.INVISIBLE);

        else
            ibLeft.setVisibility(View.VISIBLE);

        ibLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pager.getCurrentItem() > 0)
                    pager.setCurrentItem(pager.getCurrentItem() - 1, true);
            }
        });

        ImageButton ibRight = (ImageButton)rootView.findViewById(R.id.ibRight);
        if (pageNumber + 1 == PAGE_NUM)
            ibRight.setVisibility(View.INVISIBLE);
        else
            ibRight.setVisibility(View.VISIBLE);

        ibRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pager.getCurrentItem() < PAGE_NUM)
                    pager.setCurrentItem(pager.getCurrentItem() + 1, true);
            }
        });

        return rootView;
    }

    public static Fragment create(int position) {
        Fragment fragment = new ScheduleSlideFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, position);
        fragment.setArguments(args);
        return fragment;
    }

    public static Fragment create(int position, ViewPager _pager) {
        pageNumber = position;
        pager = _pager;
        Fragment fragment = new ScheduleSlideFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageNumber = getArguments().getInt(ARG_PAGE);
    }

}
4

2 回答 2

3

如果 ArrayList 中的对象具有正确覆盖和定义的 equal 方法,则可以使用contains方法。

于 2013-07-03T06:43:56.873 回答
0

每当您需要检查您的 List 是否包含某些内容时,您应该使用 Set 而不是 List。以哈希集为例。

于 2013-07-03T06:59:20.023 回答