OnClickListener不适用于Android Fragment中的TextView

时间:2016-12-19 18:35:48

标签: android android-fragments onclicklistener

我有一个Fragment,它有一个TextView,点击时应该重定向到另一个Activity。我为TextView实现了OnClickListener"空"但它不起作用。我点了一个System.out来打印一些东西,甚至不打印它。

这是我的片段:

public class OrderFragment extends Fragment implements AdapterView.OnItemClickListener {

    private BeeGif viewGif;
    private ListView lstChefOrders;
    private ArrayList<OrderList> orderLists, listOrders;
    private TextView empty;

    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_order, null, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        empty = (TextView) view.findViewById(R.id.empty);
        lstChefOrders = (ListView) view.findViewById(R.id.lstChefOrders);
        lstChefOrders.setOnItemClickListener(this);

        RelativeLayout viewLayout = (RelativeLayout) view.findViewById(R.id.beeGifOrder);
        viewGif = (BeeGif) viewLayout.findViewById(R.id.view);

        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (NetworkHelper.isOnline(getActivity())) {
                    callOrderService();
                } else {
                    NetworkHelper.noNetworkToast(getActivity());
                }
            }
        });

        if (NetworkHelper.isOnline(getActivity())) {
            if (!PrefernceHelper.getBoolean(getActivity(), Commons.Constants.USERLOGGEDIN)) {

                lstChefOrders.setVisibility(View.GONE);
                empty.setText(R.string.order_history);

                empty.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        System.out.println("Teste");
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), SignInActivity.class);
                        startActivity(intent);
                    }

                });

            } else {
                lstChefOrders.setVisibility(View.VISIBLE);
                empty.setVisibility(View.GONE);
                viewGif.setVisibility(View.VISIBLE);
                callOrderService();
            }
        } else
            NetworkHelper.noNetworkToast(getActivity());
    }


    private void callOrderService() {
        //  Log.e("TAG", "Kuch toh patta chala : " + PrefernceHelper.getString(getActivity(), Commons.Constants.USER_ID));

        new VolleyHelper(getActivity()).get("foodiesOrderListing/" + PrefernceHelper.getString(getActivity(), Commons.Constants.USER_ID), null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                viewGif.setVisibility(View.GONE);
                mSwipeRefreshLayout.setRefreshing(false);
                //  Log.e("TAG", "Thanks for the return GIFT : " + response.toString());
                try {
                    // HashMap<String, List<OrderList>> listDataChild = new HashMap<String, List<OrderList>>();
                    ArrayList<OrderId> orderIdList = new ArrayList<>();
                    orderLists = new ArrayList<>();
                    listOrders = new ArrayList<>();

                    JSONArray array = response.getJSONArray("order_ids");

                    for (int i = 0; i < array.length(); i++) {
                        String status = "";
                        OrderList orderList1 = new OrderList();
                        OrderId orderId = new OrderId();
                        JSONObject object = array.getJSONObject(i);
                        String date = object.getString("order_date");
                        orderId.setOrder_date(date);
                        orderId.setOrderid(object.getString("orderid"));
                        orderIdList.add(orderId);

                        //orderList1.setOrder_status("Pending");
                        orderList1.setOrderid(object.getString("orderid"));
                        orderList1.setChef_id("");
                        orderList1.setUser_id("");
                        orderList1.setItem_id("");
                        orderList1.setItem_qty("");
                        orderList1.setItem_price("");
                        orderList1.setItem_name(object.getString("orderid"));
                        orderList1.setOrderDate(object.getString("order_date"));

                        if (i % 2 == 0) {
                            orderList1.setBackGroundColor(Commons.Constants.TYPE_WHITE);
                        } else {
                            orderList1.setBackGroundColor(Commons.Constants.TYPE_GREY);
                        }
                        //listOrders.add(orderList1);


                        JSONArray orderListArray = response.getJSONArray("orderList");
                        for (int j = 0; j < orderListArray.length(); j++) {
                            OrderList orderList = new OrderList();
                            JSONObject orderListObject = orderListArray.getJSONObject(j);
                            if (orderIdList.get(i).getOrderid().equals(orderListObject.getString("orderid"))) {

                                status = orderListObject.getString("order_status");
                                //orderList.setOrder_status(orderListObject.getString("order_status"));
                                //orderList.setOrderid(orderListObject.getString("orderid"));
                                //orderList.setChef_id(orderListObject.getString("chef_id"));
                                //orderList.setUser_id(orderListObject.getString("user_id"));
                                //orderList.setItem_id(orderListObject.getString("item_id"));
                                //orderList.setItem_qty(orderListObject.getString("item_qty"));
                                //orderList.setItem_price(orderListObject.getString("item_price"));
                                //orderList.setItem_name(orderListObject.getString("orderid"));
                                //orderList.setOrderDate(date);
                                /*if (j % 2 == 0) {
                                    orderList.setBackGroundColor(Commons.Constants.TYPE_WHITE);
                                } else {
                                    orderList.setBackGroundColor(Commons.Constants.TYPE_GREY);
                                }
                                orderLists.add(orderList);*/
                            }

                        }
                        orderList1.setOrder_status(status);
                        listOrders.add(orderList1);

                        // listDataChild.put(orderId.getOrder_date(), orderLists);

                    }

                    if (listOrders.size() > 0) {
                        OrderListAdapter adapter = new OrderListAdapter(getActivity(), listOrders); //listDataChild (was the 3rd paramter for the Constructor earlier)
                        lstChefOrders.setAdapter(adapter);
                        empty.setVisibility(View.GONE);
                    } else {
                        if (getView().findViewById(R.id.empty) != null)
                            lstChefOrders.setEmptyView(getView().findViewById(R.id.empty));
                        // for (int i = 0; i < adapter.getGroupCount(); i++)
                        //    lstChefOrders.expandGroup(i);
                    }
                } catch (JSONException je) {

                }catch (NullPointerException npe){
                    npe.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent();
        intent.setClass(getActivity(), OrderDetailsActivity.class);
        //intent.putExtra("orderID", orderLists.get(position).getOrderid());
        intent.putExtra("orderID", listOrders.get(position).getOrderid());
        startActivity(intent);

    }
}

这是带有textView的片段的XML:

<?xml version="1.0" encoding="utf-8"?>
<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">


    <TextView
        android:id="@+id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="No Orders placed"
        android:textColor="@color/toolbar_title_color"
        android:textSize="23sp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:clickable="true"/>

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_below="@+id/searchText"
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".NearbyJobsActivity$PlaceholderFragment">
        <ListView
            android:id="@+id/lstChefOrders"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/margin_five"
            android:divider="@color/line_color"
            android:dividerHeight="1dp"
            android:padding="@dimen/margin_five"/>
    </android.support.v4.widget.SwipeRefreshLayout>


    <include
        android:id="@+id/beeGifOrder"
        layout="@layout/view_bee_animation"
        android:layout_width="@dimen/size_of_bee_gif"
        android:layout_height="@dimen/size_of_bee_gif"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

如果这些条件为false,则不会将单击侦听器绑定到文本视图:

jquery.validate.unobtrusive.js

答案 1 :(得分:0)

这样做:

empty.setText(getResources().getString(R.string.order_history));
empty.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

 Toast.makeText(getBaseContext(), "Teste", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
 }
});