从json JSONArray检索时过滤数据

时间:2015-02-16 04:37:00

标签: android json filter

我已使用(http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/)实现了可滑动的标签页。我的第一个标签中有一个网格,它是一个片段。我得到一个json响应,携带要在第一个选项卡中加载的项目。此刻我已经加载了所有物品。但这些项目属于3类。所以我有一个图片中显示的页脚来根据需要过滤项目。目前我有28个项目属于所有这3个类别。目前我正在加载所有项目。我想改变当我按下第一个页脚它应该过滤并只显示属于那个项目。我需要一些建议怎么做? 任何帮助,将不胜感激。

enter image description here

pizzaFragment是第一个标签页,在该页面的页脚中,我有3个文本视图来过滤项目

pizzafragment

public class PizzaFragment extends ListFragment implements OnTaskCompleted {

    private QuickReturnListView mListView;
    private TextView mQuickReturnViewTop;
    private TextView mQuickReturnViewBig;
    private TextView mQuickReturnViewIta;

    GridView grid;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_grid_main_pizza, container, false);

        new PizzaMenuAsyncTask(getActivity(), this).execute();

        mQuickReturnViewTop = (TextView) view.findViewById(R.id.footerTop);
        mQuickReturnViewBig = (TextView) view.findViewById(R.id.footerBig);
        mQuickReturnViewIta = (TextView) view.findViewById(R.id.footerIta);

        mQuickReturnViewTop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //change last selected view to be normal
                if(selectedView != null)
                     selectedView.setBackgroundColor(Color.parseColor("#D8000000"));

                //set selectedView to be current selected view
                selectedView = (TextView)v;

                //change selected view to be red
                selectedView.setBackgroundColor(Color.parseColor("#e92624"));



                Toast.makeText(getActivity(), "toppings!",
                        Toast.LENGTH_LONG).show();
            }

        });
        mQuickReturnViewBig.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(selectedView != null)
                     selectedView.setBackgroundColor(Color.parseColor("#D8000000"));

                //set selectedView to be current selected view
                selectedView = (TextView)v;

                //change selected view to be red
                selectedView.setBackgroundColor(Color.parseColor("#e92624"));



                Toast.makeText(getActivity(), "big!",
                        Toast.LENGTH_LONG).show();
            }

        });
        mQuickReturnViewIta.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(selectedView != null)
                     selectedView.setBackgroundColor(Color.parseColor("#D8000000"));

                //set selectedView to be current selected view
                selectedView = (TextView)v;

                //change selected view to be red
                selectedView.setBackgroundColor(Color.parseColor("#e92624"));



                Toast.makeText(getActivity(), "ita!",
                        Toast.LENGTH_LONG).show();
            }

        });

        grid = (GridView) view.findViewById(R.id.grid_pizza);
        return view;


}
    @Override
    public void onTaskCompleted(JSONArray responseJson) {

        try {
            List<String> descriptions = new ArrayList<String>();
            List<String> imageUrls = new ArrayList<String>();
            List<String> price = new ArrayList<String>();

            for (int i = 0; i < responseJson.length(); ++i) {
                JSONObject object = responseJson.getJSONObject(i);

                if ((object.getString("MainCategoryID")).equals("1")
                        && (object.getString("SubCategoryID")).equals("1")) {
                    Log.i("ImageURL ", object.getString("ImageURL"));
                    imageUrls.add(object.getString("ImageURL"));
                    Log.i("Description ", object.getString("Description"));
                    descriptions.add(object.getString("Description"));
                    Log.i("Price ", object.getString("Price"));
                    price.add(object.getString("Price"));
                }

                if ((object.getString("MainCategoryID")).equals("1")
                        && (object.getString("SubCategoryID")).equals("2")) {
                    Log.i("ImageURL ", object.getString("ImageURL"));
                    imageUrls.add(object.getString("ImageURL"));
                    Log.i("Description ", object.getString("Description"));
                    descriptions.add(object.getString("Description"));
                    Log.i("Price ", object.getString("Price"));
                    price.add(object.getString("Price"));
                }

                if ((object.getString("MainCategoryID")).equals("1")
                        && (object.getString("SubCategoryID")).equals("8")) {
                    Log.i("ImageURL ", object.getString("ImageURL"));
                    imageUrls.add(object.getString("ImageURL"));
                    Log.i("Description ", object.getString("Description"));
                    descriptions.add(object.getString("Description"));
                    Log.i("Price ", object.getString("Price"));
                    price.add(object.getString("Price"));
                }

            }

            CustomGridPizza adapter = new CustomGridPizza(getActivity(), descriptions,
                    imageUrls, price);
            grid.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eaeaea"
    android:gravity="center"
    tools:context=".MenuItems" >


    <GridView
        android:id="@+id/grid_pizza"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="10dp"
        android:columnWidth="120dp"
        android:drawSelectorOnTop="true"
        android:gravity="center"
        android:horizontalSpacing="7dp"
        android:numColumns="auto_fit"
        android:padding="10dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="7dp" />

    <lk.gamma.pizzakraft.menu.QuickReturnListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/footerBig"
        android:layout_width="106.5dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:textColor="@drawable/textview_font"
        android:paddingTop="8dip"
        android:text="@string/footer3"
        android:textSize="12sp"
        />
    <!-- android:background="#D8000000" -->

    <TextView
        android:id="@+id/footerIta"
        android:layout_width="107dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/footerTop"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dip"
        android:textColor="@drawable/textview_font"
        android:text="@string/footer2"
        android:textSize="12sp"
         />

    <TextView
        android:id="@+id/footerTop"
        android:layout_width="106dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dip"
        android:textColor="@drawable/textview_font"
        android:text="@string/footer1"
        android:textSize="12sp"
         />

</RelativeLayout>

0 个答案:

没有答案