1

リストビューでアイテムをクリックするとアイテム詳細が表示されますが、その詳細はVolley経由でデータベースから取得されました。コードは終了しましたが、問題は次のとおりです。 2回目のクリックでのみ動作します。たとえば、アイテム1(data-> Intent)をクリックすると、詳細アクティビティにジャンプしますが、は、デフォルトのレイアウト(表示されるすべての要素がnullです)のみを表示します。その後、前のListViewに戻り、アイテム1、すべてを表示をクリックします。 アイテム2をクリックすると、詳細ページのアイテム1が表示されます。リストビューに戻るアイテム2をクリックすると、item2が表示されます。ここでAndroid ListView Onclickインテントデータの遅延(2度目のクリックで作業)

は私のコードです: リストビュー:

 lvResults.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
      /** 
      * get data from db based on id_product 
      */ 
      Bean bean = resultAdapter.getItem(position);//GET ITEM POSITION 
      getPromotionById(bean.getIdProduct());//GET DATA JSONOBJECT FROM DATABASE via Volley 

      Intent intent = new Intent(ListActivity.this, DetailsActivity.class); 
      intent.putExtra("item", itemToPass); 
      startActivity(intent); 
     } 
    }); 

getPromotionById(文字列id_product)

private void getPromotionById(String id_product) { 
    Map<String, String> map = new HashMap<String, String>(); 
    map.put("id_product",id_product); 

    CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST, showUrl,map,new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      try { 
       JSONObject promotion = response.getJSONObject("product"); 
       itemToPass.setIdProduct(promotion.getString("id_product")); 
       itemToPass.setTitle(promotion.getString("name")); 
      } 
      catch (JSONException e) {e.printStackTrace();} 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) {}}); 
     requestQueue.add(jsonObjectRequest); 
} 

DetailActivity:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    item = new Item(); 
    item = (Item) getIntent().getSerializableExtra("item"); 

    setContentView(R.layout.item_detail_1); 

    itemTitle = (TextView) findViewById(R.id.title); 
    setData(item); 
} 
private void setData(Item item) { 
    new DownloadImageTask((ImageView) findViewById(R.id.img)) 
      .execute("url"); 
    itemTitle.setText(item.getTitle()); 
} 
あなたのケースのために

1st click

2nd click

+0

ViewHolderパターンを使用しますか? –

答えて

1

それが成功を得るためにいくつかの時間を必要とするので、あなたはバレーボールを経由してデータベースからデータを取得しています。データが成功取得されていない、まだ、あなたは次のActivityにそれを渡した場合 したがって、あなたは、データが取得成功
後になるまで待つ必要が

Activity次のソリューション
にnull値を取得しますそれをインテント経由で Activityに渡します。

CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST, showUrl,map,new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      try { 
       JSONObject promotion = response.getJSONObject("product"); 
       itemToPass.setIdProduct(promotion.getString("id_product")); 
       itemToPass.setTitle(promotion.getString("name")); 

       // Move Intent to here 
       Intent intent = new Intent(ListActivity.this, DetailsActivity.class); 
       intent.putExtra("item", itemToPass); 
       startActivity(intent); 
      } 
      ... 
     } 
    }, new Response.ErrorListener() { 
     ... 
} 
+0

ありがとう!今働いている –

2

私は見ての通り、あなたはデータベース内のデータを読み取り、バック応答呼び出しを待っています。しかし、まだコールバックを受け取っていない場合は、アプリ呼び出しが別のアクティビティに移動すると、何も表示されません。

あなたは次のように更新することができます:

private void getPromotionById(String id_product) { 
    Map<String, String> map = new HashMap<String, String>(); 
    map.put("id_product",id_product); 

    CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST, showUrl,map,new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      try { 
       JSONObject promotion = response.getJSONObject("product"); 
       itemToPass.setIdProduct(promotion.getString("id_product")); 
       itemToPass.setTitle(promotion.getString("name")); 

       //after receive call back, you can start your activity instead of start right after call function read database 
       Intent intent = new Intent(ListActivity.this, DetailsActivity.class); 
       intent.putExtra("item", itemToPass); 
       startActivity(intent); 
       }catch (JSONException e) {e.printStackTrace();} 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) {} 
     }); 
     requestQueue.add(jsonObjectRequest); 
    } 
+0

ありがとうございました:) –

関連する問題