这段代码的一切都很好,但有时它会将数据添加到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;
}
}
}
答案 0 :(得分:1)
将项目添加到productFeed后,您需要致电:
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
问题似乎是你不知道解析是否完全结束
由于myFirstListView.setAdapter(adapter);
在...之外
onResponse(JSONObject response)
方法。因此,当您致电myFirstListView.setAdapter(adapter);
原因:
两者都在不同的主题中运行,所以即使整个解析完成之前你也有可能调用myFirstListView.setAdapter(adapter)
。这就是为什么你有时候看不到列表中填充的结果。
解决方案
1)有两种解决方案可以在onResponse()方法中移动myFirstListView.setAdapter(适配器)。
2)或者在解析完成后发送一些广播消息。并接收广播消息并在接收器的onReceive()方法中设置适配器
(即使你有任何布局而不仅仅是listView,这种方法将来会有所帮助)