android - 如何使用 Android onItemClick 获取 ListView 项?

标签 android json listview hashmap onitemclicklistener

这是 SO Q here 的延续但我仍然缺少一些东西。

我不知道如何获取从 ListView 的 JSONObject 映射出来的项目。 HashMap 键值为:

map.put(TAG_RES_FILE, resFile);

我想将该字符串放入我的 onItemClick(){int passResFile = getResources().getIdentifier(TAG_RES_FILE, "raw", "com.andaero.app");}

我以为通过将标签名称放在下面的方法中,系统会自动从该项目位置拉出它 - 显然不是。那它是怎么得到的呢??谢谢。

编辑:我添加了一个 log.i() 来查看点击位置内有哪些值并返回:

getIdentifier(11925): {isRawRes=true, title=Advisory Circulators, label=AC, _id=1, resFile=advisory_circulators_sort_list, description=Provides guidance such as methods, procedures, and practices for complying with regulations and requirements., containerID=R.id.listContainer}

这是

resFile=advisory_circulators_sort_list

这就是我需要得到的 - 我该怎么做??

这是整个监听器:

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                    MainActivity.mLayout.toggleSidebar();
                    setHasOptionsMenu(true);

                    FragmentManager fm = getFragmentManager();
                    final FragmentTransaction lcFT = fm.beginTransaction();
                    lcFT.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out);

                    final Bundle args = new Bundle();

                    Object o = lv.getItemAtPosition(pos);
                    String resFile = (String) o.toString();
                    int passResFile = getResources().getIdentifier(TAG_RES_FILE, "raw", "com.andaero.app");
                    args.putInt("KEY_RES_FILE", passResFile);

                    boolean isRawRes = true;
                    args.putBoolean("KEY_IS_RAW_RES", isRawRes);
                    Log.i("getIdentifier", resFile );

                    // Delayed to improve animations
                    final Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        public void run() {
                            ListViewFragment lvf = new ListViewFragment();
                            lcFT.replace(R.id.listContainer, lvf).commit();
                            lvf.setArguments(args);
                        }
                    }, 300);
                }

最佳答案

所以我继续将项目发送到 textView 并在布局文件中将其可见性设置为消失。这工作得很好,但我希望有一种更好/更清洁的方法。

在 onItemClick() 方法中更改自:

Object o = lv.getItemAtPosition(pos);
String resFile = (String) o.toString();
int passResFile = getResources().getIdentifier(TAG_RES_FILE, "raw", "com.andaero.app");
args.putInt("KEY_RES_FILE", passResFile);

对此:

String resFile = ((TextView) view.findViewById(R.id.listResFile)).getText().toString();
int passResFile = getResources().getIdentifier(resFile, "raw", "com.andaero.app");
args.putInt("KEY_RES_FILE", passResFile);

关于android - 如何使用 Android onItemClick 获取 ListView 项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11003846/

相关文章:

android - 将应用程序的 Beta 版本发布为生产版本

java - Json 响应未打印

javascript - 如何使用 PHP 和 JS 将 URL 中的 json 数组与另一个 json 数组组合起来

android - 单击Flutter ListView导航到另一个屏幕

Android 控制台错误 : "requires .class compatibility set to 5.0. Please fix project properties"

android - MUPDF 上的超链接 pdf

c# - 使用 JavaScriptserializer 将 google places details JSON 转换为 C# 对象

java - jackson 没有填充所有属性

android - 使用数组适配器对 ListView 进行排序

WPF - Listview 数据绑定(bind)列宽问题