我想访问bookList
中onItemClickListener
的商品。我想访问特定的书籍并从中获取具体项目如何执行此操作?
代码是:
// Creating volley request obj
final JsonArrayRequest bookrequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj =(JSONObject) response.getJSONObject(i);
Book book = new Book();
book.setTitle(obj.getString("BTitle"));
book.setThumbnailUrl(obj.getString("BCoverpath"));
book.setAuthor(obj.getString("BAuthor"));
book.setEdition(obj.getString("BEdition"));
book.setCategory(obj.getString("Bcategory"));
book.setLanguage(obj.getString("BLanguage"));
book.setDescription(obj.getString("BDescription"));
book.setCover(obj.getString("BCover"));
book.setOwner(obj.getString("UserFN"));
// adding book to books array
bookList.add(book);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
}
);
// Adding request to request queue
AppController.getInstance().addToRequestQueue(bookrequest);
// setting an onItem click listener in order to open the view book activity
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
bookname=((TextView)view.findViewById(R.id.title)).getText().toString();
Bauthor=((TextView)view.findViewById(R.id.author)).getText().toString();
Bedition=((TextView)view.findViewById(R.id.edition)).getText().toString();
// intent to take the viewbook activity as its next dextination
Intent i = new Intent(getApplicationContext(), ViewBook.class);
// putting the name of the book with the inten extra package to use it as
// a key to retrieve the book info from the database and display it
i.putExtra("bookname",bookname);
i.putExtra("category",Bcategory);
i.putExtra("Bauthor",Bauthor);
i.putExtra("Bedition",Bedition);
startActivity(i);
}
});
我尝试了很多方法,其中大多数方法以超出范围的异常和索引超出范围结束。
答案 0 :(得分:0)
在onItemClick(...)
内,您可以访问所点击项目的位置和项目来源,以便您可以使用该信息获取所点击的实际项目。
我将参数重命名为onItemClick()
,以便为其提供更具描述性的名称,并假设您在Book
中的字段具有匹配的getter方法。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Book b = bookList.get(position);
Intent i = new Intent(Romance.this, ViewBook.class)
.putExtra("bookname", b.getTitle())
.putExtra("category", b.getCategory())
.putExtra("Bauthor", b.getAuthor())
.putExtra("Bedition", b.getEdition);
startActivity(i);
}