我正在尝试从Adapter类本身刷新
listView
,但它并不令人耳目一新,不知道错误在哪里。
public class DeliveryListAdapter extends BaseAdapter
{
DeliveryListAdapter ald;
Context ct;
private List<DeliveryListBean> deliveryListBeans;
SharedPreferences companyName;
public DeliveryListAdapter(Context ct,List<DeliveryListBean> deliveryListBeans)
{
this.ct=ct;
this.deliveryListBeans=deliveryListBeans;
}
@Override
public int getCount() {
return deliveryListBeans.size();
}
private void updateResults(List<DeliveryListBean> dlb)
{
deliveryListBeans=dlb;
notifyDataSetChanged();
}
//I skipped code for inflating because it is working fine.
delivered.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String serverURL= PathUrls.pathUrl+"evs_updatedeliverystatus.php?db="+companyName.getString("companyName","")+"&invoiceid="+dlb.getInvoiceNo()+"&deliverystatus=2";
JsonObjectRequest jsonArrayRequest=new JsonObjectRequest(serverURL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response.length()>0)
{
ald=new DeliveryListAdapter(ct,deliveryListBeans);
VolleyLog.v("update delivery list: %n %s ", response.toString());
try{
int status = response.getInt("status");
if (status==1)
{
Log.d("updated delivery",response.toString());
ald.updateResults(deliveryListBeans);
Toast.makeText(ct, "Delivery Successful", Toast.LENGTH_SHORT).show();
}
else if (status==0)
{
Toast.makeText(ct, "Delivery Has been failed", Toast.LENGTH_LONG).show();
}
}catch (JSONException e)
{
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error DeliveryList:%n %s ", error);
Toast.makeText(ct,"NetworkError Not Responding", Toast.LENGTH_SHORT).show();
}
});
//Toast.makeText(ct, "companyName"+companyName.getString("companyName","").toString(), Toast.LENGTH_LONG).show();
VolleySingleton.getsInstance().getRequestQueue().add(jsonArrayRequest);
}
});
我正在使用setOnclickListner来传递按钮来更新状态。 状态正在更新,但单击交付按钮时列表不会刷新。
答案 0 :(得分:1)
正如我在评论中所说,你搞砸了参考资料。见下面的代码:
private void updateResults(List<DeliveryListBean> dlb) {
deliveryListBeans=dlb;
notifyDataSetChanged();
}
您在此处将新列表分配给上一个列表,以便更改参考。适配器永远不会得到通知导致适配器具有先前的引用。所以解决方案有两种情况: 如果您想要旧数据,那么如果您只需要新数据,则需要第二个:
private void updateResults(List<DeliveryListBean> dlb) {
deliveryListBeans.addAll(dlb);
notifyDataSetChanged();
}
private void updateResults(List<DeliveryListBean> dlb) {
deliveryListBeans.clear();
deliveryListBeans.addAll(dlb);
notifyDataSetChanged();
}
答案 1 :(得分:0)
为什么要创建新的适配器?
DeliveryListAdapter ald=new DeliveryListAdapter(ct,deliveryListBeans);
我认为您应该只更新deliveryListBeans
并使用ADM用户的这种方法。
private void updateResults(List<DeliveryListBean> dlb) {
deliveryListBeans.clear();
deliveryListBeans.addAll(dlb);
notifyDataSetChanged();
}
我不认为你使用'回复'。您只需检查此var&lt;中的状态 - 它是来自您服务器的数据
答案 2 :(得分:0)
我找到了另一种方法。我刚刚从
position
删除了list
,错误就是创建了新对象ald=new DeliveryListAdapter(ct,deliveryListBeans);
你不必创建任何新对象。我做的是
if (status==1)
{
Log.d("updated delivery",response.toString());
deliveryListBeans.remove(position);
notifyDataSetChanged();
Toast.makeText(ct, "Delivery Successful", Toast.LENGTH_SHORT).show();
}