这是 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/