Bay_Basti 2015-03-09 03:17 采纳率: 0%
浏览 1778

关于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()调用有什么条件?

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 19:55
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿