我有一个 ArticlesAdapter 用于读取远程 JSON 数据。适配器正在工作,但我正在努力让我的 ListView setonitemclicklister 工作。
我想要完成的是能够单击 ListItem 并获取所单击选项的文章数据。我的 Article 类中有 getName() 之类的函数,我需要调用它来响应 ListItem 单击,但我似乎找不到完成它的方法。
我正在尝试在我的 AsyncTask 类中执行此操作。
使用以下代码:
protected void onPostExecute(JSONArray result) {
// TODO Auto-generated method stub
Log.i("CHECK", "RESULTS: " + result);
List<Article> articles = new ArrayList<Article>();
String title = null;
String nid = null;
try{
for(int i=0; i < data.length(); i++){
JSONObject dataObj = (JSONObject)data.get(i);
JSONObject record = dataObj.getJSONObject("node");
title = (record.getString("title"));
nid = (record.getString("nid"));
Log.i("FOUND", "title: " + title);
Log.i("FOUND", "nid: " + nid);
articles.add( new Article(title, "", Integer.parseInt(nid)) );
}
}catch(JSONException j){
Log.e("CHECK", "Attempting to read data returned from JSONReader: " + j.toString());
}
ListView articlesList = (ListView)findViewById(R.id.articlesList);
ArticleAdapter adapter = new ArticleAdapter(ArticlesActivity.this, R.layout.article_item, articles);
articlesList.setAdapter(adapter);
articlesList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "testing : ", Toast.LENGTH_LONG);
Log.i("CHECK", "AdapterView: " + parent);
Log.i("CHECK", "VIEW : " + view);
Log.i("CHECK", "POSITION : " + position);
Log.i("CHECK", "ID : " + id);
}
});
if(dialog.isShowing())
dialog.dismiss();
}
我的问题是我不确定如何让 setOnItemClickListener 工作。我可以通过 Log 函数打印参数:
Log.i("CHECK", "AdapterView: " + parent);
Log.i("CHECK", "VIEW : " + view);
Log.i("CHECK", "POSITION : " + position);
Log.i("CHECK", "ID : " + id);
...但是当我尝试将任何参数转换为 Article 对象时出现错误,因此我可以将其称为 getName() 函数等。