【问题标题】:Android ListView Onclick Intent data delay (work in second time click)Android ListView Onclick Intent 数据延迟(在第二次点击时工作)
【发布时间】:2016-09-14 06:57:17
【问题描述】:

我想在单击 ListView 中的项目时显示项目详细信息,这些详细信息是通过 Volley 从数据库中获取的。我完成了代码,但问题是: 它仅适用于第二次点击。 例如当我点击item1(data->Intent)时,跳转到详情Activity,但它只显示默认布局(视图中的每个元素都是空的)。然后点击返回上一个ListView,点击item1everything display。 现在,如果我点击item2,它将在详细信息页面中显示item1。返回ListView点击item2,会显示item2。

这是我的代码: 列表视图:

       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(String 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

【问题讨论】:

  • 使用 ViewHolder 模式?

标签: android listview android-intent android-activity android-volley


【解决方案1】:

对于您的情况: 如我所见,您在数据库中读取数据并等待响应回调。但是,如果您还没有收到回电,您的应用程序调用会转到另一个活动,那么这就是您看不到任何内容的原因。

你可以这样更新:

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

【讨论】:

    【解决方案2】:

    您正在通过 Volley 从数据库中获取数据,因此需要一些时间才能成功。 因此,如果数据还没有获取成功,你将它传递给下一个Activity,你将在下一个Activity得到空值

    解决方案
    您需要等待数据获取成功
    之后,你通过 Intent 将它传递给下一个 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() {
            ...
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 2012-02-07
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      相关资源
      最近更新 更多