private static HashMap<Integer,Boolean> isSelected;
private static HashMap<Integer, Integer> number;
private static HashMap<Integer, Boolean> isSelected2;
public GouwucheAdapter(Context context, List<DetailShop> list) {
super(context, list);
isSelected2=new HashMap<Integer, Boolean>();
isSelected=new HashMap<Integer, Boolean>();
number=new HashMap<Integer, Integer>();
setdate();
}
public void setdate(){
for(int i=0;i<list.size();i++){
getIsSelected().put(i,false);
getIsSelected2().put(i,false);
}
for(int i=0;i<list.size();i++){
number.put(i,list.get(i).getNumber());
}
}
@Override
public View getItemView(final int position, View convertView, ViewGroup parent) {
int a=0;
//初始化view 复用
for(int i=0;i<getCount();i++){if(list.get(i).getName().equals(list.get(position).getName())){
a=i;
break;
}
}
vh.numb.setText(""+number.get(position));
vh.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("DELETE_GOUWUCHE");
intent.putExtra("position", position);
context.sendBroadcast(intent);
}
});
vh.jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int n=number.get(position);
if(n>0){
n--;
number.put(position, n);
vh.numb.setText(""+n);
Intent intent=new Intent("UP_NUMBER");
intent.putExtra("position", position);
intent.putExtra("flag", true);
context.sendBroadcast(intent);
}
}
});
vh.jia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int n=number.get(position);
n++;
number.put(position, n);
vh.numb.setText(""+n);
Intent intent=new Intent("UP_NUMBER");
intent.putExtra("position", position);
intent.putExtra("flag", false);
context.sendBroadcast(intent);
}
});
if(a==position){
vh.lin.setVisibility(View.VISIBLE);
if(a==0){
vh.view.setVisibility(View.GONE);
}else{
vh.view.setVisibility(View.VISIBLE);
}
}else{
vh.lin.setVisibility(View.GONE); //这个应该是 gone
vh.view.setVisibility(View.GONE);
}
vh.iv1.setChecked(getIsSelected2().get(position));
vh.iv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(getIsSelected2().get(position)){
Intent intent=new Intent("JIA_CHECK_UP_NUMBER");
intent.putExtra("flag",false);
intent.putExtra("position", position);
context.sendBroadcast(intent);
vh.iv1.setChecked(false);
}else{
Intent intent=new Intent("JIA_CHECK_UP_NUMBER");
intent.putExtra("flag",true);
intent.putExtra("position", position);
context.sendBroadcast(intent);
vh.iv1.setChecked(true);
}
}
});
vh.iv2.setChecked(getIsSelected().get(position));
vh.iv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(getIsSelected().get(position)){
vh.iv2.setChecked(false);
getIsSelected().put(position, false);
Intent intent=new Intent("CHECK_UP_NUMBER");
intent.putExtra("flag", false);
intent.putExtra("position", position);
context.sendBroadcast(intent);
}else{
Intent intent=new Intent("CHECK_UP_NUMBER");
intent.putExtra("flag", true);
intent.putExtra("position", position);
context.sendBroadcast(intent);
vh.iv2.setChecked(true);
getIsSelected().put(position, true);
}
}
});
return convertView;
}
class ViewHolder{
View view;
LinearLayout lin;;
ImageView photo;
CheckBox iv2,iv1;
TextView name,money,color,chicun;
ImageView jia,jian;
TextView numb;
ImageView delete;
}
public static HashMap<Integer, Boolean> getIsSelected(){
return isSelected;
}
public static HashMap<Integer, Boolean> getIsSelected2(){
return isSelected2;
}
public static HashMap<Integer, Integer> getNumber(){
return number;
}
public static void setIsSelected(HashMap<Integer,Boolean> isSelected){
GouwucheAdapter.isSelected=isSelected;
}
public static void setIsSelected2(HashMap<Integer,Boolean> isSelected){
GouwucheAdapter.isSelected2=isSelected;
}
}
//main 函数中 view 的改变方法 和 消息的传递 广播
private void setView() {
broder=new MyBroader();
IntentFilter filter=new IntentFilter();
filter.addAction("UP_NUMBER");
filter.addAction("DELETE_GOUWUCHE");
filter.addAction("CHECK_UP_NUMBER");
filter.addAction("JIA_CHECK_UP_NUMBER");
getActivity().registerReceiver(broder, filter);
hash=new HashMap<Integer, Boolean>();
hash_jia=new HashMap<Integer, Boolean>();
gouwuche_money=(TextView) v.findViewById(R.id.gouwuche_money);
chb=(CheckBox) v.findViewById(R.id.gouwuche_all);
chb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(ch){
chb.setChecked(false);
ch=false;
for(int i=0;i<list2.size();i++){
hash_jia.put(i, false);
hash.put(i, false);
GouwucheAdapter.getIsSelected2().put(i, false);
GouwucheAdapter.getIsSelected().put(i, false);
number=0;
float_number=0;
}
gouwuche_money.setText("¥"+float_number+"("+number+")");
}else{
chb.setChecked(true);
number=0;
float_number=0;
for(int i=0;i<list2.size();i++){
GouwucheAdapter.getIsSelected().put(i, true);
GouwucheAdapter.getIsSelected2().put(i, true);
number+=GouwucheAdapter.getNumber().get(i);
float_number+=GouwucheAdapter.getNumber().get(i)*list2.get(i).getMoney();
hash.put(i, true);
hash_jia.put(i, true);
}
ch=true;
gouwuche_money.setText("¥"+float_number+"("+number+")");
}
adapter.notifyDataSetChanged();
if(adapter.getCount()==0){
lv.setVisibility(View.GONE);
lin.setVisibility(View.GONE);
rel.setVisibility(View.VISIBLE);
}else{
rel.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
lin.setVisibility(View.VISIBLE);
}
}
});
View header=v.findViewById(R.id.gouwuche_header);
back=(ImageView) header.findViewById(R.id.header2_back);
back.setVisibility(View.GONE);
shoping=(Button) v.findViewById(R.id.gouwuche_goumai);
shoping.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(number>0){
Intent intent=new Intent(getActivity(), QueRenActivity.class);
//TODO 传递选中的订单
List<DetailShop> list3=new ArrayList<DetailShop>();
for(int i=0;i<list2.size();i++){
if(hash.get(i)){
list3.add(list2.get(i));
}
}
intent.putExtra("list", (ArrayList<DetailShop>)list3);
startActivityForResult(intent,111);
}else{
Toast.makeText(getActivity(), "亲,没有选中的商品奥^_^", Toast.LENGTH_SHORT).show();
}
}
});
List<DetailShop> list=new ArrayList<DetailShop>();
for(int i=0;i<10;i++){
DetailShop shangpin=new DetailShop();
shangpin.setChicun("尺寸:S"+i);
shangpin.setColor("颜色:红色"+i);
shangpin.setId(i+500);
shangpin.setMoney(i^8);
shangpin.setName("谁谁谁家的东东1");
shangpin.setNumber(i);
list.add(shangpin);
}
for(int i=0;i<10;i++){
DetailShop shangpin=new DetailShop();
shangpin.setChicun("尺寸:M"+i);
shangpin.setColor("颜色:水蓝色"+i);
shangpin.setId(i+500);
shangpin.setMoney(i^8);
shangpin.setName("谁谁谁家的东东2");
shangpin.setNumber(i);
list.add(shangpin);
}
//TODO 排序 按 卖家的不同排序
list2=new ArrayList<DetailShop>();
while(list.size()>0){
list2.add(list.get(0));
list.remove(0);
for(int j=0;j<list.size();j++){
if(list.get(j).equals(list2.get(list2.size()-1))){
list2.add(list.get(j));
list.remove(j);
}
}
}
for(int i=0;i<list2.size();i++){
hash.put(i,false);
hash_jia.put(i,false);
}
adapter=new GouwucheAdapter(getActivity(), list2);
lv=(ListView) v.findViewById(R.id.gouwuche_lv);
lv.setAdapter(adapter);
if(adapter.getCount()==0){
lv.setVisibility(View.GONE);
lin.setVisibility(View.GONE);
rel.setVisibility(View.VISIBLE);
}else{
rel.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
lin.setVisibility(View.VISIBLE);
}
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent inten=new Intent(getActivity(), JiayuanZhoubianDetailActivity.class);
startActivity(inten);
}
});
}
class MyBroader extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("DELETE_GOUWUCHE")){
if(hash.get(intent.getIntExtra("position", 0))){
number-=list2.get(intent.getIntExtra("position", 0)).getNumber();
float_number-=adapter.getNumber().get(intent.getIntExtra("position", 0))*list2.get(intent.getIntExtra("position", 0)).getMoney();
gouwuche_money.setText("¥"+float_number+"("+number+")");
}
for(int i=intent.getIntExtra("position", 0);i<list2.size();i++){
GouwucheAdapter.getIsSelected().put(i, GouwucheAdapter.getIsSelected().get(i+1));
GouwucheAdapter.getNumber().put(i, GouwucheAdapter.getNumber().get(i+1));
hash.put(i, hash.get(i+1));
}
adapter.removeone(intent.getIntExtra("position", 0));
adapter.notifyDataSetChanged();
//TODO 移除服务器中对应的数据
}
if(intent.getAction().equals("UP_NUMBER")){
Boolean flag=intent.getBooleanExtra("flag", false);
int position=intent.getIntExtra("position", 0);
if(!flag){
//TODO 计算订单信息
if(hash.get(position)){
float_number+=list2.get(position).getMoney();
number+=1;
}
list2.get(position).setNumber(list2.get(position).getNumber()+1);
//float_number加上 此个订单的数据
}else{
//float_number减去 此个订单的数据
list2.get(position).setNumber(list2.get(position).getNumber()-1);
if(hash.get(position)){
number-=1;
float_number-=list2.get(position).getMoney();
}
}
gouwuche_money.setText("¥"+float_number+"("+number+")");
}
if(intent.getAction().equals("CHECK_UP_NUMBER")){
int position=intent.getIntExtra("position", 0);
Boolean flag=intent.getBooleanExtra("flag", false);
hash.put(position, flag);
if(!hash.get(position)){
number-=GouwucheAdapter.getNumber().get(position);
float_number-=GouwucheAdapter.getNumber().get(position)*list2.get(position).getMoney();
hash.put(position, false);
for(int i=0;i<list2.size();i++){
if(list2.get(i).getName().equals(list2.get(position).getName())){
hash_jia.put(i, false);
GouwucheAdapter.getIsSelected2().put(i, false);
}
}
chb.setChecked(false);
ch=false;
}else{
number+=GouwucheAdapter.getNumber().get(position);
float_number+=GouwucheAdapter.getNumber().get(position)*list2.get(position).getMoney();
hash.put(position, true);
}
gouwuche_money.setText("¥"+float_number+"("+number+")");
adapter.notifyDataSetChanged();
}
if(intent.getAction().equals("JIA_CHECK_UP_NUMBER")){
int position=intent.getIntExtra("position", 0);
Boolean flag=intent.getBooleanExtra("flag", false);
hash_jia.put(position, flag);
if(!hash_jia.get(position)){
for(int i=position;i<list2.size();i++){
if(list2.get(i).getName().equals(list2.get(position).getName())){
hash_jia.put(i, false);
GouwucheAdapter.getIsSelected2().put(i, false);
boolean old=hash.get(i);
if(old){
GouwucheAdapter.getIsSelected().put(i, false);
number-=GouwucheAdapter.getNumber().get(i);
float_number-=GouwucheAdapter.getNumber().get(i)*list2.get(i).getMoney();
hash.put(i, false);
}
}
}
chb.setChecked(false);
ch=false;
}else{
for(int i=position;i<list2.size();i++){
if(list2.get(i).getName().equals(list2.get(position).getName())){
hash_jia.put(i, true);
GouwucheAdapter.getIsSelected2().put(i, true);
boolean old=hash.get(i);
if(!old){
GouwucheAdapter.getIsSelected().put(i, true);
number+=GouwucheAdapter.getNumber().get(i);
float_number+=GouwucheAdapter.getNumber().get(i)*list2.get(i).getMoney();
hash.put(i, true);
}
}else{
break;
}
}
}
gouwuche_money.setText("¥"+float_number+"("+number+")");
adapter.notifyDataSetChanged();
}
}
}
@Override
public void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
if(arg1==111&&arg0==111){
for(int i=(list2.size()-1);i>=0;i--){
if(hash.get(i)){
list2.remove(list2.get(i));
for(int j=i;j<list2.size()-1;j++){
GouwucheAdapter.getIsSelected().put(j, GouwucheAdapter.getIsSelected().get(j+1));
GouwucheAdapter.getIsSelected2().put(j, GouwucheAdapter.getIsSelected().get(j+1));
GouwucheAdapter.getNumber().put(j, GouwucheAdapter.getNumber().get(j+1));
hash.put(j, hash.get(j+1));
hash_jia.put(j, hash.get(j+1));
}
}
}
adapter.notifyDataSetChanged();
float_number=0;
number=0;
gouwuche_money.setText("¥"+float_number+"("+number+")");
}
}