expandablelistview指标问题

时间:2016-03-29 12:45:02

标签: android json listview android-studio onclicklistener

我正在使用列表视图创建多级可扩展列表视图。所以我想要点击它时列表视图的指示符发生了变化。我在drawable文件夹中有两个png图像。点击事件无法正常工作。点击减号时没有来。我指的是 this < / strong>即可。 我的主要活动

true

我的listitem xml是

public class MainActivity extends Activity {

    List<NLevelItem> lists;
    ListView listView;
    RequestQueue requestQueue1, requestQueue2;
    int[] firstcatid = null,secondcatid = null;
    String firsttitle[] = null,secondtitle[] = null;
    int j = 0;
    //ArrayList<Group> list,list2,list3;
    NLevelItem parent;
    Boolean b=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView1);
        lists = new ArrayList<NLevelItem>();
       // Random rng = new Random();
        final LayoutInflater inflater = LayoutInflater.from(this);
        requestQueue1= Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://www.fasr.com/cntrldata/ezproductfristcategory.php",
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            //list = new ArrayList<Group>();
                            JSONArray ja = response.getJSONArray("hi");
                            JSONArray ja2 = response.getJSONArray("hi2");
                            JSONArray 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]), 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);
                                        //tv.setBackgroundColor(Color.GREEN);

                                        iv.setVisibility(View.VISIBLE);
/*
                                        view.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
                                                if(!b){
                                                    iv.setBackgroundResource(R.drawable.ic_action_minus);
                                                    b=true;
                                                }else {
                                                    b=false;
                                                    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")), 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);
                                                //tv.setBackgroundColor(Color.YELLOW);
                                                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")), 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);}
                                    }
                                }
                            }

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

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

                                }
                            });
                            // Toast.makeText(getApplicationContext(),url[2],Toast.LENGTH_SHORT).show();
                            //output.setText(data);
                            //ExpListItems =  list;
                            // ExpAdapter = new ExpandListAdapter(getApplicationContext(), ExpListItems);
                            // ExpandList.setAdapter(ExpAdapter);
                        } 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 SomeObject(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    }
我的照片是 enter image description here

2 个答案:

答案 0 :(得分:1)

在适配器的getGroupView()方法

中替换以下条件中的指标
if(isExpanded)
{
}
else
{
}

答案 1 :(得分:1)

            @Override
            public View getView(NLevelItem item)
            {
                View convertView = inflater.inflate(R.layout.seperated_cows_first_row, null);

                TextView tv = (TextView) convertView.findViewById(R.id.tct_name);
                ImageView icon = (ImageView) convertView.findViewById(R.id.select_icon);

                String name = ((ListObject) item.getWrappedObject()).getName();
                tv.setText(name);

                if (item.isExpanded())
                    icon.setBackgroundResource(R.drawable.list_item_selector_icon_down);
                else
                    icon.setBackgroundResource(R.drawable.list_item_selector_icon_up);

                return convertView;
            }

这解决了我的问题