关于ListView 刷新的问题

package com.box.page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.RequestQueue;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.box.datamodel.CartGood;
import com.box.sjfood_v_1_0.MainActivity;
import com.box.sjfood_v_1_0.R;
import com.box.tools.UrlString;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.LinearGradient;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShoppingCart extends Fragment{
private boolean hasitem = false;//进入界面时,接受服务器数据,表示购物车内是否有物品
private Button goForBuybutton;//去逛逛
private Button calculate;//结算按钮
private RequestQueue mQueue = null;
private boolean isLogin;//是否已经登陆
private String phoneString;//用户手机号
private SharedPreferences sp;
private Handler mHandler;
private ArrayList cartGoods;
private RelativeLayout shopping_somethingLayout;
private LinearLayout shopping_nothingLayout;
private ListView listView;//购物车listview
private Button allSelectImageView;//全选图标
private TextView totalPriceTextView;//总价
private ShoppingCartAdapter myAdapter;
private float totalPrice = 0;//记录总价
private ArrayList isselected = new ArrayList();
public View onCreateView( final LayoutInflater inflater,
@Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
//获取用户信息
getUserInfo();

    View view = inflater.inflate(R.layout.shoppingcart, container,false); 
    //获取页面
    initView(view);
    //获取用户的订单信息
    mQueue = Volley.newRequestQueue(getActivity());
    GetOrderList();

    mHandler = new Handler()
    {


        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub

            if(msg.obj.toString().equals("-1"))
            {
                isLogin = false;
                hasitem = false;

                Log.i("isLogin1", String.valueOf(isLogin));
                Log.i("hasitem1", String.valueOf(hasitem));

                setAllGone();
                shopping_nothingLayout.setVisibility(View.VISIBLE);
            }
            else {

                JSONObject jsonObject= new JSONObject();

                jsonObject = (JSONObject) msg.obj;

                cartGoods = (ArrayList<CartGood>) JSONArray.parseArray(jsonObject.getJSONArray("orderList").toString(), CartGood.class);
                isLogin = true;
                hasitem = true;
                Log.i("isLogin1", String.valueOf(isLogin));
                Log.i("hasitem1", String.valueOf(hasitem));
                setAllGone();

                shopping_somethingLayout.setVisibility(View.VISIBLE);

                dataToListView();

            }       

        }
        /*
         * 把cartgoods中的数据传到适配器中
         */
        private void dataToListView() {
            // TODO Auto-generated method stub

            for(int i = 0;i<cartGoods.size();i++)
            {
                Log.i("cartGoods",cartGoods.get(i).getName()+ i);
                isselected.add(false);
            }

            myAdapter = new ShoppingCartAdapter(getActivity(), cartGoods,isselected);
            listView.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();
        }

        private void setAllGone() {
            // TODO Auto-generated method stub
            shopping_somethingLayout.setVisibility(View.GONE);
            shopping_nothingLayout.setVisibility(View.GONE);
        }

    };


    return view;


}

private void initView(View view) {
    // TODO Auto-generated method stub
    shopping_nothingLayout = (LinearLayout) 
            view.findViewById(R.id.shoppingcart_nothing_layout);
    shopping_somethingLayout = (RelativeLayout) 
            view.findViewById(R.id.shoppingcart_something_layout);

    initNothingView(view);

    initSomethingView(view);
}

//初始化有item时的界面

private void initSomethingView(View view) {
    // TODO Auto-generated method stub
    calculate = (Button) view.findViewById
            (R.id.shoppingcart_something_calcu);//结算按钮
    allSelectImageView = (Button) view.findViewById
            (R.id.shoppingcart_allselect);//全选
    totalPriceTextView = (TextView)view.findViewById
            (R.id.shoppingcart_totalprice);//总价
    listView = (ListView)view.findViewById(R.id.shoppingcart_listview);

    allSelectImageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            isselected.add(true);

            if (allSelectImageView.isSelected()) {
                for(int i = 0;i<isselected.size();i++)
                {
                    isselected.set(i, false);
                }
                allSelectImageView.setSelected(false);
            }
            else {
                for(int i = 0;i<isselected.size();i++)
                {
                    isselected.set(i, true);
                }
                allSelectImageView.setSelected(true);
            }
            CalculatePrice();

            myAdapter.notifyDataSetChanged();
        }
    });

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            final int mposition = position;
            final CartGood cartGood = cartGoods.get(position);
            final Button button = (Button) view.findViewById(R.id.shoppingcart_item_select);
            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (isselected.get(mposition)) {
                        allSelectImageView.setSelected(false);
                    }
                    isselected.set(mposition, !isselected.get(mposition));
                    Log.i("isselected", String.valueOf(isselected.get(mposition)));
                    //Log.i("isselectedadapter",String.valueOf(myAdapter.getList().get(mposition)));


                    myAdapter.notifyDataSetChanged();

                    CalculatePrice();

                }


            });
        }
    });
}


/*
 * 获取用户存储在手机上的ID
 * 若未登录则Id置为-1,另外处理
 */

private void CalculatePrice() {
    // TODO Auto-generated method stub
    totalPrice = 0;
    for(int i = 0;i<isselected.size();i++)
    {
        if(isselected.get(i))
        {
            if(cartGoods.get(i).getIsDiscount() == 1)
            totalPrice = totalPrice+cartGoods.get(i).getDiscountPrice();
            else {
                totalPrice = totalPrice + cartGoods.get(i).getPrice();
            }
        }
    }

    totalPriceTextView.setText("¥ "+String.valueOf(totalPrice));
}

private void getUserInfo() {
    // TODO Auto-generated method stub
    sp = getActivity().getSharedPreferences("login", 0);

    isLogin = sp.getBoolean("Login", false);
    phoneString = sp.getString("phone", "-1");

    if(!isLogin)
    {
        hasitem = false;
        phoneString = "-1";
    }
}

/*
 * 获取用户订单信息
 */
private void GetOrderList() {
    // TODO Auto-generated method stub
    StringRequest stringRequest = new StringRequest(Method.POST, UrlString.GetOrderString, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            // TODO Auto-generated method stub

            Log.i("用户订单信息", response);

            JSONObject jsonObject =JSONObject.parseObject(response);

            String statusString = jsonObject.getString("status");

            if(statusString.equals("success"))
            {

                Message message = mHandler.obtainMessage();


                if(!jsonObject.getString("message").equals("获取购物车订单成功"))
                {
                    message.obj = -1;
                }
                else {
                    message.obj = jsonObject;
                }
                mHandler.sendMessage(message);

            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            // TODO Auto-generated method stub

        }
    })
    {
        protected Map<String, String> getParams(){

            Map<String, String> params = new HashMap<String, String>();
            params.put("phoneId", phoneString);


            return params;

        }
    };

    mQueue.add(stringRequest);

}

private void initNothingView(View view) {
    // TODO Auto-generated method stub
    goForBuybutton = (Button)view.findViewById(R.id.shoppingcart_goforbuy);

    goForBuybutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getActivity(),MainActivity.class);
            startActivity(intent);
            getActivity().finish();
        }
    });
}

}


为什么listview不会自动刷新?
是不是只要adapter绑定的数据源发生变化了,调用notifydatasetonchanged就会刷新?
notifydatasetonchanged()调用有什么条件?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问