Two click listeners

时间:2016-04-04 16:34:31

标签: android listview android-studio textview listviewitem

I have a List view.Here what I did is the 3 level expandable listview using a tutorial.In that list view can I put a click listener on text view and item of the list view? my mainActivity

public drawerlist(final Context context, final LayoutInflater inflater, ListView listView1){
            this.listView=listView1;
            lists = new ArrayList<NLevelItem>();
            requestQueue1= Volley.newRequestQueue(context);
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://www.ezybzy.com/cntrldata/ezproductfristcategory.php",
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            try {
                                //list = new ArrayList<Group>();

                                ja = response.getJSONArray("hi");

                                ja2 = response.getJSONArray("hi2");

                                ja3 = response.getJSONArray("hi3");
                                firsttitle = new String[ja.length()];
                                firstcatid = new int[ja.length()];
                                JSONObject jsonObject2 = null, jsonObject3 = null, jsonObject;
                                for (int i = 0; i < ja.length(); i++) {

                                    jsonObject = ja.getJSONObject(i);


                                // Log.e("hie",jsonObject2.getInt("second_category_id")+"");
                                //jsonObject3 = ja3.getJSONObject(i);
                                firstcatid[i] = jsonObject.getInt("first_category_id");
                                firsttitle[i] = jsonObject.getString("first_category_name");
                            }
                            secondcatid = new int[ja2.length()];
                            for (int i = 0; i < ja2.length(); i++) {
                                jsonObject2 = ja2.getJSONObject(i);
                                secondcatid[i] = jsonObject2.getInt("second_category_id");
                            }
                            for (int i = 0; i < ja.length(); i++) {

                                final NLevelItem grandParent = new NLevelItem(new SomeObject(firsttitle[i],firstcatid[i]+""), null, new NLevelView() {

                                    @Override
                                    public View getView(NLevelItem item) {
                                        final View view = inflater.inflate(R.layout.list_item, null);
                                        TextView tv = (TextView) view.findViewById(R.id.textView);
                                        final ImageView iv=(ImageView)view.findViewById(R.id.imageView);
                                        if (item.isExpanded()) {
                                            iv.setBackgroundResource(R.drawable.ic_action_minus);
                                            tv.setTypeface(null, Typeface.BOLD);
                                        } else iv.setBackgroundResource(R.drawable.ic_action_add);

                                        String name = (String) ((SomeObject) item.getWrappedObject()).getName();
                                        tv.setText(name);
                                        return view;
                                    }
                                });
                                lists.add(grandParent);
                                //int numChildren = rng.nextInt(4) + 1;
                                for (int j = 0; j < ja2.length(); j++) {
                                    jsonObject2 = ja2.getJSONObject(j);

                                    if (firstcatid[i] == jsonObject2.getInt("first_category_id")) {

                                        parent = new NLevelItem(new SomeObject(jsonObject2.getString("second_category_name"),jsonObject2.getInt("second_category_id")+""), grandParent, new NLevelView() {

                                            @Override
                                            public View getView(NLevelItem item) {
                                                View view = inflater.inflate(R.layout.list_item, null);
                                                TextView tv = (TextView) view.findViewById(R.id.textView);
                                                final ImageView iv=(ImageView)view.findViewById(R.id.imageView);
                                                if (item.isExpanded()) {
                                                    iv.setBackgroundResource(R.drawable.ic_action_minus);
                                                    tv.setTypeface(null, Typeface.BOLD);
                                                } else iv.setBackgroundResource(R.drawable.ic_action_add);
                                                tv.setPadding(20,0,0,0);
                                                String name = (String) ((SomeObject) item.getWrappedObject()).getName();
                                                tv.setText(name);
                                                return view;
                                            }
                                        });

                                        lists.add(parent);                                // int grandChildren = rng.nextInt(5) + 1;
                                    for (int k = 0; k < ja3.length(); k++) {
                                        jsonObject3 = ja3.getJSONObject(k);
                                        if (secondcatid[j] == jsonObject3.getInt("second_category_id")) {
                                            NLevelItem child = new NLevelItem(new SomeObject(jsonObject3.getString("third_category_name"),jsonObject3.getString("third_category_id")+""), parent, new NLevelView() {

                                                @Override
                                                public View getView(NLevelItem item) {
                                                    View view = inflater.inflate(R.layout.list_item, null);
                                                    TextView tv = (TextView) view.findViewById(R.id.textView);
                                                    //tv.setBackgroundColor(Color.GRAY);
                                                    tv.setPadding(40,0,0,0);
                                                    String name = (String) ((SomeObject) item.getWrappedObject()).getName();
                                                    tv.setText(name);
                                                    return view;
                                                }
                                            });

                                            lists.add(child);} }
                                    }
                                }
                            }

                            final NLevelAdapter adapter = new NLevelAdapter(lists);
                            listView.setAdapter(adapter);
                            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                                        long arg3) {
                                    ((NLevelAdapter) listView.getAdapter()).toggle(arg2);
                                    ((NLevelAdapter) listView.getAdapter()).getFilter().filter();
                                    NLevelItem item2 = (NLevelItem)adapter.getItem(arg2);

                                    id = ((SomeObject)item2.getWrappedObject()).getId();


                                    Toast.makeText(context, id, Toast.LENGTH_SHORT).show();
                                    try {

                                        for (int i = 0; i < ja3.length(); i++) {
                                            JSONObject jsonObject1 = ja3.getJSONObject(i);
                                            if (id.equals(jsonObject1.getString("third_category_id"))){
                                                Intent intent = new Intent(context, Categorypage.class);
                                                intent.putExtra("id", id);
                                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                context.startActivity(intent);
                                            }
                                        }
                                    }catch (Exception e){

                                    }
                                }
                            });
                         View view = inflater.inflate(R.layout.list_item, null);
                        TextView tv = (TextView) view.findViewById(R.id.textView);
                        tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(context, "hi", Toast.LENGTH_SHORT).show();
                            }
                        });

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Volley", error.toString());

                    }
                }
        );
        requestQueue1.add(jsonObjectRequest);



    }

    class SomeObject {
        public String name;
        public String id;

        public String getId() {
            return id;
        }

        public SomeObject(String name,String id) {
            this.name = name;
            this.id=id;
        }
        public String getName() {
            return name;
        }
    }
}

In this I want to put on click listener to the text view and the list item.You can refer this tutorial.

1 个答案:

答案 0 :(得分:0)

Have you tried adding a click listener to it? Your code shows you adding an click listener to the list, but not the textview.

 tv.setOnClickListener(new OnClickListener() 
 {
     @Override
     public void onClick(View v) 
     {
         //on click code to execute....
     }
 });