ExpandableListView并在Android中单击子项

时间:2014-11-04 16:18:10

标签: android listview arraylist expandablelistview

我有疑问,因为如果我点击ExpandableListView中的子项,点击它就打开了一个具有相同名称的新活动,也就是说,如果我点击“Butelka”项,它会带我进入一类活动“Butelka”和类比。

public class MyListAdapter extends BaseExpandableListAdapter {

        private Context context;
        private ArrayList<Alphabet> alphabetList;
        private ArrayList<Alphabet> originalList;

        public MyListAdapter(Context context, ArrayList<Alphabet> alphabetList){
            this.context = context;
            this.alphabetList = new ArrayList<Alphabet>();
            this.alphabetList.addAll(alphabetList);
            this.originalList = new ArrayList<Alphabet>();
            this.originalList.addAll(alphabetList);
        }
        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return alphabetList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            ArrayList<Waste> wasteList = alphabetList.get(groupPosition).getWasteList();

            return wasteList.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return alphabetList.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            ArrayList<Waste> wasteList = alphabetList.get(groupPosition).getWasteList();
            return wasteList.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            Alphabet alphabet = (Alphabet) getGroup(groupPosition);
            if(convertView == null)
            {
                LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.group_row, null);
            }
            TextView heading = (TextView) convertView.findViewById(R.id.heading);
            heading.setText(alphabet.getName().trim());

            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            Waste waste = (Waste) getChild(groupPosition, childPosition);
            if(convertView == null){
                LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.child_row, null);
            }


            TextView name = (TextView) convertView.findViewById(R.id.name);     
            name.setText(waste.getName().trim());

            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }

        public void filterData(String query)
        {
            query = query.toLowerCase();
            Log.v("MyListAdapter", String.valueOf(alphabetList.size()));
            alphabetList.clear();

            if(query.isEmpty())
            {
                alphabetList.addAll(originalList);
            } else {
                for(Alphabet alphabet: originalList)
                {
                    ArrayList<Waste> wasteList = alphabet.getWasteList();
                    ArrayList<Waste> newList = new ArrayList<Waste>();
                    for(Waste waste: wasteList)
                    {
                        if(waste.getName().toLowerCase().contains(query)){
                            newList.add(waste);
                        }
                    }
                    if(newList.size() > 0)
                    {
                        Alphabet nAlphabet = new Alphabet(alphabet.getName(), newList);
                        alphabetList.add(nAlphabet);
                    }
                }
            }
            Log.v("MyListAdapter", String.valueOf(alphabetList.size()));
            notifyDataSetChanged();
        }
    }

在改变之前我使用了这段代码

String classNames[] = {"Aerozol"};
    ListView myList;

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.segregate_waste_activity);
            myList = (ListView)findViewById(android.R.id.list);

            setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,classNames));
     }

     @Override
     protected void onListItemClick(ListView lv, View v, int position, long id){
         super.onListItemClick(lv, v, position, id);
         String openClass = classNames[position];
         try{
             Class selected = Class.forName("com.odpad.odpadygdansk.waste." + openClass);
             Intent selectedIntent = new Intent(this, selected);
             startActivity(selectedIntent);
         }catch(ClassNotFoundException e){
             e.printStackTrace();
         }
     }            

您是否有任何想法在当前版本中使用此代码?

1 个答案:

答案 0 :(得分:0)

您可以直接从适配器为每个孩子设置一个监听器。像这样:

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Waste waste = (Waste) getChild(groupPosition, childPosition);
        if(convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.child_row, null);
        }


        TextView name = (TextView) convertView.findViewById(R.id.name);  
        name.setText(waste.getName().trim());

        // Click listener
        convertView.setOnClickListener(new View.onClickListener() {
           String openClass = waste.getName().trim();
           try{
              Class selected = Class.forName("com.odpad.odpadygdansk.waste." + openClass);
              Intent selectedIntent = new Intent(this, selected);
              ((Activity)getContext()).startActivity(selectedIntent);
           }catch(ClassNotFoundException e){
              e.printStackTrace();
           }
        });


        return convertView;
    }