2017-08-28 79 views
1

这段代码一切正常,但有时会将数据添加到listview中,有时不会。请告诉我我错过了什么。我曾在不同的项目中尝试过。总是相同的结果。有时可以工作,有时不会。有很多方法可以轻松地与服务器连接。几周前我开始了Android编程。Volley正在提取数据,但有时它会在listview中显示数据,有时不会

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; 

     } 


    } 

} 

回答

1

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

adapter.notifyDataSetChanged(); 
0

的问题似乎是,你不知道的分析是否彻底完成。
由于myFirstListView.setAdapter(adapter);不在
onResponse(JSONObject response)方法。所以,你不知道列表无论你是空还是满,当你调用myFirstListView.setAdapter(adapter);

原因:

在不同的线程,以便有你乃至之前调用myFirstListView.setAdapter(adapter)机会都跑解析完成。这就是为什么你有时看不到任何结果在你的列表中填充。

1)有两个解决方案要么移动myFirstListView.setAdapter(适配器)的内部onResponse()方法。
2)或者你在解析完成时发送一些广播消息。并接收广播消息并在接收器的onReceive()方法内设置适配器。
即使您有任何布局不仅仅是一个列表视图,这种方法将有所帮助)

相关问题