2016-05-09 5 views
0

bookListからonItemClickListenerの項目にアクセスしたいと思います。特定の書籍にアクセスし、特定の書籍を入手する方法を教えてください。Arraylist Itemsへのアクセス

コードは次のとおりです。

// 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); 
    } 
}); 

私は多くの方法を試してみましたが、それらのほとんどは、範囲外のバウンド例外のうち、インデックスで終わります。

+2

投稿したすべての400行のコードのうち、関連性は何ですか?投稿した問題をどのように解決しようとしますか?そうでない場合は、なぜそれらを含めましたか?削除してください。ヘルプセンター、特に[MCVE]の提供方法を​​お読みください。 –

+0

私はコードを簡略化しました:)どんな助け? – Aborroum

答えて

0

onItemClick(...)から、クリックされたアイテムの位置とアイテムのソースにアクセスできるため、その情報を使用してクリックされた実際のアイテムを取得できます。

パラメータをonItemClick()に変更して、わかりやすい名前にして、Bookのフィールドに一致するゲッターメソッドがあると仮定しました。

@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); 
} 
+0

ありがとう、ジョージ、コーディングの7時間を想像し、最後に私を救った笑、私は愚かな感じ、もう一度ありがとう:) – Aborroum

+0

あなたは大歓迎です。お役に立てて嬉しいです。 –

関連する問題