如何在可扩展列表视图中向子项添加OnItemClickListener

时间:2014-11-15 18:06:31

标签: java android listview expandablelistview

我想在点击项目后,孩子打开了我的新活动,其名称与项目名称相同。那是项目的“测试”,如果我点击它打开test.java类。我发现了一个执行此操作的代码,但我不知道如何将其添加到我的类中。谢谢你的帮助。

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();
        }
    }

SegregateWasteActivity.java

public class SegregateWasteActivity extends Activity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
    private SearchView search;
    private MyListAdapter listAdapter;
    private ExpandableListView myList;
    private ArrayList<Alphabet> alphabetList = new ArrayList<Alphabet>();

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.segregate_waste_activity);

            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            search = (SearchView) findViewById(R.id.search);
            search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            search.setIconifiedByDefault(false);
            search.setOnQueryTextListener(this);
            search.setOnCloseListener(this);

            displayList();
            expandAll();
            }

    @Override
    public boolean onClose() {
        // TODO Auto-generated method stub
        listAdapter.filterData("");
        expandAll();
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        listAdapter.filterData(query);
        expandAll();
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        listAdapter.filterData(newText);
        expandAll();
        return false;
    }

    private void expandAll()
    {
        int count = listAdapter.getGroupCount();
        for(int i = 0; i < count; i++)
        {
            myList.expandGroup(i);
        }
    }

    private void displayList()
    {
        loadSomeData();

        myList = (ExpandableListView) findViewById(R.id.expandableList);

        listAdapter = new MyListAdapter(SegregateWasteActivity.this, alphabetList);

        myList.setAdapter(listAdapter);
    }

    private void loadSomeData()
    {
        ArrayList<Waste> wasteList = new ArrayList<Waste>();
        Waste waste = new Waste("Aerozol");
        wasteList.add(waste);
        waste = new Waste("Aaaa");
        wasteList.add(waste);

        Alphabet alphabet = new Alphabet("A", wasteList);
        alphabetList.add(alphabet);

        wasteList = new ArrayList<Waste>();
        waste = new Waste("Butelka");
        wasteList.add(waste);

        alphabet = new Alphabet("B", wasteList);
        alphabetList.add(alphabet);
    }
}

我希望这个片段抛出:

@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 :(得分:3)

我相信你想要这样的东西:

expListView.setOnChildClickListener(new ExpandableListView
        .OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView elv, View view, int i,
                                int i2, long l) {
        TextView tv = view.findViewById(R.id.name);
        String name = (String) tv.getText();
        try {
            // Change package.name to your package
            Class cls = Class.forName("package.name." + name);
            Intent intent = new Intent(MainActivity.this, cls);
            startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return false;
    }
});

代码几乎是不言自明的。

您可以使用点击回调中收到的位置参数i来获取特定名称而不是findViewById()。那会更有效率。