【发布时间】:2015-10-01 14:52:08
【问题描述】:
我刚刚学习 Android 两周,所以也许我的问题很愚蠢,但我的代码有两个问题....
//[improt etc.]
ArrayList<HashMap<String, String>> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {...
arrayList = new ArrayList<HashMap<String, String>>();
//[...]
try {
jsonObject = new JSONObject(uebergabe);
jsonArray = jsonObject.getJSONArray("Fux");
int i;
for (i=0; i < jsonArray.length(); i++)
{
HashMap<String, String> Hashmap = new HashMap<String, String>();
JSONObject jO = jsonArray.getJSONObject(i);
String headline = jO.optString("headline");
Hashmap.put("headline", headline);
String content = jO.optString("content");
Hashmap.put("content", content);
arrayList.add(Hashmap);
}
list = (ListView) findViewById(R.id.list_fuxenfibel);
ListAdapterFibel adapterFibel = new ListAdapterFibel(this, R.layout.layout_fibel, arrayList);
list.setAdapter(adapterFibel);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Intent intent = new Intent(Fuxenfibel.this, Fuxenfibel_Inhalt.class);
intent.putExtra("content",arrayList.getPosition(i).get("content"));
startActivity(intent);
}
});
} catch (JSONException e) {
//[...]
我不能用
intent.putExtra("content", arrayList.getPosition(i).get("content"));
为了获取位置,它会导致“无法解析方法”错误,因为它不是ArrayList的PublicMethod,而是ArrayAdapter的方法。
是否有类似ArrayList 的方法,或者其他无需重新考虑整个代码即可解决此问题的方法?
我的第二个问题是同一代码行中的变量 i,它抛出“无法解析符号 'i' 错误。一些如何在 for 循环之外声明 i 来修复此错误?
当我这样做时
//[...]
final int i;
for (i=0;i<jasonArray.lenght;i++)
//[...]
intent.putExtra("content",arrayList./*MISSING METHOD not.getPosition*/.get("content"));
//[...]
for-loop 不能做 i++ 因为它是最终的,当我声明它不是最终的时,它不能从内部类中访问...
如何让变量在循环外工作?
谢谢你帮助我。
【问题讨论】:
-
使用
((HashMap<String, String>)parent.getItemAtPosition(position)).get("content")