ExpandableListView、OnChildClickListener

2024-04-09

我有组列表,每个组内都有填充的子项目。我已经实现了searchview with filtered ressults and myExpandableListView` 可以展开和折叠。问题是,我不知道如何处理 OnChildClickListener,一旦我添加 OnChildClickListener 函数,应用程序突然停止工作。有人可以帮我解决这个问题吗?

这是我的代码:

package com.teamamazing.search;


import java.util.ArrayList;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import android.widget.ExpandableListView.OnChildClickListener;

public class Accomodation extends Activity implements
        SearchView.OnQueryTextListener, SearchView.OnCloseListener{

    private SearchView search;
    private Accomodation_Adapter accomodationAdapter;
    private ExpandableListView expandableListView;
    private ArrayList<AccomodationHeaderRow> headerRows = new ArrayList<AccomodationHeaderRow>();
    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accomodation);
        expandableListView.setOnChildClickListener(expandableListViewClickedItem);

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

        //display the list
        displayList();
        //expand all Groups
        expandAll();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.acco_search, menu);
        return true;
    }

    //method to expand all groups
    private void expandAll() {
        int count = accomodationAdapter.getGroupCount();
        for (int i = 0; i < count; i++){
            expandableListView.expandGroup(i);
        }
    }

//this is my current problem, there's no error yet the application isn't working.
    private OnChildClickListener expandableListViewClickedItem =  new OnChildClickListener() {

        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            if (groupPosition == 0) {
                switch (childPosition) {
                    //cebuano
                    case 0:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel1);
                        mp.start();
                        break;
                    //kapampangan
                    case 1:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel2);
                        mp.start();
                        break;
                    //ilocano
                    case 2:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel);
                        mp.start();
                        break;

                }
            } else if (groupPosition == 1) {
                switch (childPosition) {
                    //cebuano
                    case 0:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestaurant1);
                        mp.start();
                        break;
                    //kapampangan
                    case 1:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestau2);
                        mp.start();
                        break;
                    //ilocano
                    case 2:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestau);
                        mp.start();
                        break;

                }
            }
            return false;
        }

    };



    //method to expand all groups
    private void displayList() {

        //display the list
        loadSomeData();

        //get reference to the ExpandableListView
        expandableListView = (ExpandableListView) findViewById(R.id.accomodation_exp);
        //create the adapter by passing your ArrayList data
        accomodationAdapter = new Accomodation_Adapter(Accomodation.this, headerRows);
        //attach the adapter to the list
        expandableListView.setAdapter(accomodationAdapter);

    }

    private void loadSomeData() {

        ArrayList<AccomodationChildRow> accomodationChildRows = new ArrayList<AccomodationChildRow>();

        AccomodationChildRow accomodationChildRow = new AccomodationChildRow("Binisaya: Aduna bay bisag unsa nga hotel nga duol?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Kapampangan: Atin bang malapit a hotel kene?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Ilocano: Adda kadi ti uray ania nga ot-otel ditoy nga asideg?");
        accomodationChildRows.add(accomodationChildRow);

        AccomodationHeaderRow accomodationHeaderRow = new AccomodationHeaderRow("Are there any hotels near here? | Mayroon bang anumang malapit na hotel dito?", accomodationChildRows);
        headerRows.add(accomodationHeaderRow);


        accomodationChildRows = new ArrayList<AccomodationChildRow>();
        accomodationChildRow = new AccomodationChildRow("Binisaya: Aduna bay bisag-unsa nga ristorant duol dinhi?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Kapampangan:\tAtin bang malapit a pipanganan kene?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Ilocano:\tAdda kadi ti uray ania nga asideg ditoy nga resrestauran?");
        accomodationChildRows.add(accomodationChildRow);


        accomodationHeaderRow = new AccomodationHeaderRow("Are there any restaurants near here? | Mayroon bang anumang malapit na restawran dito?", accomodationChildRows);
        headerRows.add(accomodationHeaderRow);
    }


    @Override
    public boolean onClose() {
        accomodationAdapter.filterData("");
       // expandAll();
        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        accomodationAdapter.filterData(query);
        expandAll();
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        accomodationAdapter.filterData(query);
        expandAll();
        return false;
    }

}

Thanks!


我已经找到了正确答案。 这就是我所做的...

package com.teamamazing.search;


import java.util.ArrayList;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import android.widget.ExpandableListView.OnChildClickListener;




public class Accomodation extends Activity implements
        SearchView.OnQueryTextListener, SearchView.OnCloseListener {

    private SearchView search;
    private Accomodation_Adapter accomodationAdapter;
    private ExpandableListView expandableListView;
    private ArrayList<AccomodationHeaderRow> headerRows = new ArrayList<AccomodationHeaderRow>();
    MediaPlayer mp;



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


        //searchview

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

        //display the list
        displayList();
        //expand all Groups
        expandAll();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.acco_search, menu);
        return true;
    }

    //method to expand all groups
    private void expandAll() {
        int count = accomodationAdapter.getGroupCount();
        for (int i = 0; i < count; i++){
            expandableListView.expandGroup(i);
        }
    }


    //method to expand all groups
    private void displayList() {

        //display the list
        loadSomeData();

        //get reference to the ExpandableListView
        expandableListView = (ExpandableListView) findViewById(R.id.accomodation_exp);
        //create the adapter by passing your ArrayList data
        accomodationAdapter = new Accomodation_Adapter(Accomodation.this, headerRows);
        //attach the adapter to the list
        expandableListView.setAdapter(accomodationAdapter);


        setListener();

    }


    void setListener() {
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView listview, View view,
        int groupPosition, int childPosition, long id) {

            if (groupPosition == 0) {
                switch (childPosition) {
                    //cebuano
                    case 0:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel1);
                        mp.start();
                        break;
                    //kapampangan
                    case 1:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel2);
                        mp.start();
                        break;
                    //ilocano
                    case 2:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel);
                        mp.start();
                        break;

                }
            } else if (groupPosition == 1) {
                switch (childPosition) {
                    //cebuano
                    case 0:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestaurant1);
                        mp.start();
                        break;
                    //kapampangan
                    case 1:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestau2);
                        mp.start();
                        break;
                    //ilocano
                    case 2:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestau);
                        mp.start();
                        break;

                }
            }

            return false;
        }
    });
}





    private void loadSomeData() {

        ArrayList<AccomodationChildRow> accomodationChildRows = new ArrayList<AccomodationChildRow>();

        AccomodationChildRow accomodationChildRow = new AccomodationChildRow("Binisaya: Aduna bay bisag unsa nga hotel nga duol?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Kapampangan: Atin bang malapit a hotel kene?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Ilocano: Adda kadi ti uray ania nga ot-otel ditoy nga asideg?");
        accomodationChildRows.add(accomodationChildRow);

        AccomodationHeaderRow accomodationHeaderRow = new AccomodationHeaderRow("Are there any hotels near here? | Mayroon bang anumang malapit na hotel dito?", accomodationChildRows);
        headerRows.add(accomodationHeaderRow);


        accomodationChildRows = new ArrayList<AccomodationChildRow>();
        accomodationChildRow = new AccomodationChildRow("Binisaya: Aduna bay bisag-unsa nga ristorant duol dinhi?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Kapampangan: Atin bang malapit a pipanganan kene?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Ilocano: Adda kadi ti uray ania nga asideg ditoy nga resrestauran?");
        accomodationChildRows.add(accomodationChildRow);


        accomodationHeaderRow = new AccomodationHeaderRow("Are there any restaurants near here? | Mayroon bang anumang malapit na restawran dito?", accomodationChildRows);
        headerRows.add(accomodationHeaderRow);
    }


    @Override
    public boolean onClose() {
        accomodationAdapter.filterData("");
       // expandAll();
        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        accomodationAdapter.filterData(query);
        expandAll();
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        accomodationAdapter.filterData(query);
        expandAll();
        return false;
    }

        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ExpandableListView、OnChildClickListener 的相关文章

随机推荐

  • GWT CellTable 以编程方式选择 CheckBoxCell

    我有一个带有 CheckBoxCell 列的 cellTable 我想做的是在单元格表之外有一组按钮 让用户自动检查 取消检查一组不同的元素 例如检查所有 取消检查所有 但我想要更复杂的规则 我不明白的是如何访问该元素 我想通过行 列值 并
  • 为什么我没有从 Google OAuth 请求收到 RefreshToken?

    我正在尝试将 Google 日历集成到我的应用程序中 但在传递 RefreshToken 的 OAuth 授权方面遇到一些问题 我收到的 AccessToken 没有问题 但 RefreshToken 属性为 null 请参阅标记为 ERR
  • 用于处理 win32 FILETIME 的 Java 库?

    是否有任何 Java 库可以处理 win32文件时间 http msdn microsoft com en us library ms724284 28v vs 85 29 aspx时间间隔 它基本上是自 1601 年 1 月 1 日以来以
  • 3 个流体 Div 宽度 2 个固定边距?

    我有以下设置 但是将 div 的宽度设置为 30 左右并不能始终如一地工作 一旦窗口宽度小于某个数字 第三个 div 就会低于某个数字 有没有更好的方法来做到这一点 以便我的 div 始终保持内联并不断变得越来越小 同时它们之间的边距保持固
  • 用于 C++ 的 FastCGI

    我只找到了两个用于 C 的 FastCGI 库 有的是 官方 一 http www fastcgi com drupal node 6 q node 21 and 快速cgi https savannah nongnu org projec
  • 用户可编辑带有友好 ID 的 slugs

    Case 我的站点表单包含一个 slug 字段 如果输入了值 则应将其用作 slug 编辑 一些澄清 我想要的很像 slugs 在 WordPress 中的工作方式 如果未提供 slug gt slug 名称 如果提供了 slug gt 使
  • 如何有效检测逻辑和物理处理器的数量?

    目前我正在使用这个功能 它工作正常 但每个查询大约需要 1 秒 所以就我而言 我在应用程序中浪费了 3 秒钟 目前我正在考虑使用 3 个线程在一秒钟内获取所有信息 function GetWMIstring wmiHost wmiClass
  • Smalltalk:原语是如何实现的?

    我知道一切都是对象 您可以向 Smalltalk 中的对象发送消息来执行几乎所有操作 现在我们如何实现一个对象 内存表示和基本操作 来表示原始数据类型 例如如何 对于整数的实现 我查看了 Smalltalk 的源代码并发现了这个Smalli
  • Request.IsAuthenticated 始终为 true。我错过了什么?

    我正在尝试将用户名添加到 cookie 并在注销时清除它 但是Request IsAuthenticated返回始终为 true 因此我无法知道它是否是下一页上的成员 这是我正在尝试的 public void Logout FormsAut
  • 如何修复“向客户端发送响应时失败(22:无效参数)”

    使用 directio 时 如果有多个扇区大小不同的挂载点 Nginx 无法读取文件 我们的服务器在 Nginx 的根文件夹下安装了不同的硬盘 我们所有的旧磁盘的扇区大小都是 512 当我们更换磁盘时 Nginx 突然无法从该磁盘读取文件
  • 在 Android 设备中使用 ACTION_PICK 意图仅显示电话号码联系人

    我的目标是仅向用户显示带有电话号码的联系人 并让用户选择我想在本地存储的几个联系人 我在下面的方法中使用了各种选项来代替 ContactsContract Contacts CONTENT URI 但我收到了很多显示的联系人 许多都是垃圾邮
  • 查找 Java 类文件版本之间的差异

    我正在使用来自商业供应商的大型 Java Web 应用程序 我从供应商那里收到了一个新的 class 文件形式的补丁 该文件应该可以解决我们在软件方面遇到的问题 过去 应用该供应商的补丁会导致出现新的且完全不相关的问题 因此我想在将其应用于
  • 使用端点原型数据存储时设置父键但不设置子键

    如何为 EndpointsModel 设置父级 祖先并让数据存储区自动生成实体 ID 密钥 我已经尝试去适应带有祖先的键 http endpoints proto datastore appspot com examples keys wi
  • Lighttable,设置字体大小

    我是 Light Table IDEAS 的新手 有谁知道如何设置workspace和 Windows 字体大小 我可以更改编辑器字体大小 但不知道如何设置font size对于其他元素 或者更改所有 IDE 字体的全局字体大小 打开命令窗
  • DateTime.TryParseExact 未按预期工作

    谁能解释为什么以下代码片段返回 true 根据文档 d 自定义格式说明符 http msdn microsoft com en us library 8kb3ddd4 aspx dSpecifier 一位数日期的格式不带前导零 那么 当我给
  • Pyparsing - 匹配最外面的一组嵌套括号

    我正在尝试使用 pyparsing 构建一个解析器 该解析器将匹配任意嵌套的括号内的所有文本 如果我们考虑这样的字符串 A B C D E F G Random Middle text H I J 我想要的是解析器以返回两个匹配的方式进行匹
  • 地图在移动设备上显示错误(使用 JQuery mobile)

    我正在使用 jQuery mobile 并且必须显示一些地图 我使用的功能是每次单击特定链接时都会创建地图 但在生成第一个地图后 其他地图显示错误 这里有一个例子 第一张地图 其他地图 我使用这样的函数 function buildMap
  • 使用 EVAL、SCAN 和 DEL 的 Redis 通配符删除脚本返回“非确定性命令后不允许写入命令”

    因此 我正在寻求构建一个 lua 脚本 该脚本使用 SCAN 根据模式查找键并删除它们 原子地 我首先准备了以下脚本 local keys local done false local cursor 0 repeat local resul
  • gridview中如何合并两个单元格

    我在 gridview 中有一些数据 格式如下 A B 1 2 adeel 3 4 sml 现在我想将该行与 B 列下的空单元格合并 我该怎么做 您可以使用 layout columnSpan 或 layout rowSpan 根据需要使对
  • ExpandableListView、OnChildClickListener

    我有组列表 每个组内都有填充的子项目 我已经实现了searchview with filtered ressults and myExpandableListView 可以展开和折叠 问题是 我不知道如何处理 OnChildClickLis