android - Volley正在获取数据,但有时会在listview中显示数据,有时不会

标签 android listview android-volley

这段代码一切都很好,但有时它会向 ListView 添加数据,有时不会。请告诉我我缺少什么。我在不同的项目中尝试过。总是相同的结果。有时有效,有时无效。是否有其他方法可以轻松连接服务器。几周前我开始了 Android 编程。

private List<product> productFeed= new ArrayList<product>();

double qty = 0;
double item_sub_total = 0;
double item_price = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);







    RequestQueue queue = Volley.newRequestQueue(this);

   // Toast.makeText(getApplicationContext(), "After Request Q", Toast.LENGTH_SHORT).show();
    Log.i("Error","Before try");
    JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
            "http://localhost/product.php", null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                Log.i("Error","In try");
               // Toast.makeText(getApplicationContext(),"In Try",Toast.LENGTH_SHORT).show();

                JSONArray productList = response.getJSONArray("products");
                //Toast.makeText(getApplicationContext()," Try 1",Toast.LENGTH_SHORT).show();

                Log.i("Error", String.valueOf(productList.length()));
                //Toast.makeText(getApplicationContext(),productList.length(),Toast.LENGTH_SHORT).show();
                for (int i = 0; i < productList.length(); i++) {
                    JSONObject temp = productList.getJSONObject(i);

                    String name = temp.getString("name");
                    Double price = temp.getDouble("price");
                    String imageURL = temp.getString("image");
                    Log.i("Product", name);
                    productFeed.add(new product(name,price,imageURL));
                    //  Toast.makeText(getApplicationContext(),"3.1",Toast.LENGTH_SHORT).show();

                  //  Log.i("Error",name);

                }

            } catch (JSONException e) {
                Log.i("Error","In Catch");
                Toast.makeText(getApplicationContext(),"In Catch",Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("Error", error.toString());
        }
    });
    //Toast.makeText(getApplicationContext(),"Before My Req",Toast.LENGTH_SHORT).show();

    myReq.setRetryPolicy(new DefaultRetryPolicy(1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    queue.add(myReq);
    Log.i("Error","2");

    ArrayAdapter<product> adapter = new customAdapter();

    ListView myFirstListView = (ListView) (findViewById(R.id.productList));

    myFirstListView.setAdapter(adapter);
    Log.i("Error","3");
    myFirstListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            product currentItem = productFeed.get(position);

        }
    });

}

    class customAdapter extends ArrayAdapter<product>
    {
        public customAdapter() {
            super(MainActivity.this, R.layout.item,productFeed);
        }


        @Override
        public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            Log.i("Error","4");
            if (convertView == null)
                convertView = getLayoutInflater().inflate(R.layout.item,parent,false);

            final product currentItem = productFeed.get(position);
            ImageView add = (ImageView) convertView.findViewById(R.id.add);
            ImageView minus = (ImageView) convertView.findViewById(R.id.minus);
            ImageView addtocart = (ImageView)convertView.findViewById(R.id.addtocart);
            ImageView productImage = (ImageView)convertView.findViewById(R.id.productImage);

            final TextView product = (TextView)convertView.findViewById(R.id.product);
            final TextView price = (TextView)convertView.findViewById(R.id.price);
            final TextView subtotal = (TextView)convertView.findViewById(R.id.sub_total);
            final TextView quantity = (TextView)convertView.findViewById(R.id.quantity);
            //final long qty[]=new long[position];
          /*  for(int j=0;j<qty.length;j++)
            {
                qty[j]=0;
                Log.i("Array", String.valueOf(qty[j]));
            }
           // Toast.makeText(getApplicationContext(),String.valueOf(qty.length), Toast.LENGTH_SHORT).show();
          */  //Log.i("Length",String.valueOf(qty.length));
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String q= (String) quantity.getText();
                    Double d = Double.parseDouble(q);
                    qty=d;
                    qty++;
                  //  Toast.makeText(getApplicationContext(),String.valueOf(position),Toast.LENGTH_SHORT).show();
                    Double sub_total =currentItem.getPrice();


                   // Log.i("Error",)
                    item_sub_total = qty*sub_total;
                    quantity.setText(String.valueOf(qty));
                    subtotal.setText(String.valueOf(item_sub_total));
                }
            });

            minus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String q= (String) quantity.getText();
                    Double d = Double.parseDouble(q);
                    qty=d;

                    if(qty !=0)
                    {

                        double sub_total =currentItem.getPrice();
                        qty--;
                        item_sub_total = qty*sub_total;
                        quantity.setText(String.valueOf(qty));
                        subtotal.setText(String.valueOf(item_sub_total));

                    }

                }
            });
            addtocart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(),currentItem.getImageURL(),Toast.LENGTH_SHORT).show();
                }
            });


            product.setText(currentItem.getName());
            price.setText(String.valueOf(currentItem.getPrice()));
            quantity.setText("0");
            subtotal.setText("");
      //      productImage.setImageResource(currentItem.getImageID());
            // Toast.makeText(MainActivity.this,currentItem.getImageURL().toString(),Toast.LENGTH_SHORT).show();
            //Log.i("image",currentItem.getImageURL());
            Picasso.with(MainActivity.this).load(currentItem.getImageURL()).into(productImage);
            //ImageRequest imageRequest = new ImageRequest(currentItem.imageURL);
            return convertView;

        }


    }

}

最佳答案

将项目添加到productFeed后,您需要调用:

adapter.notifyDataSetChanged();

关于android - Volley正在获取数据,但有时会在listview中显示数据,有时不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45913665/

相关文章:

java - 在android中排序 ListView

Android Volley 似乎无法连接到 Flask 本地主机地址以获取 Flask 返回字符串的 GET 方法?

android - 在 Android 应用程序中重新映射坐标系

android - 检测我的无障碍服务是否启用

android listview 滚动后显示错误数据(自定义适配器)

java - 方法返回了一个值,但该值未正确传递

android - Volley 向服务器发送空参数

Android - 聚光灯搜索

Android 读取 HTML 文件

Android多列ListView