Android中CheckBox多项选择当前的position信息提交的示例代码

先给大家展示下效果图:

潜山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

Android 中CheckBox多项选择当前的position信息提交的示例代码

废话不多说了,下面通过示例代码给大家介绍checkbox 多项选择当前的position信息提交,具体代码如下所示:

package com.dplustours.b2c.View.activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.Toast; 
import com.dplustours.b2c.R; 
import com.dplustours.b2c.View.application.MyApplication; 
import com.dplustours.b2c.View.application.UIHelper; 
import java.util.ArrayList; 
import java.util.HashMap; 
/** 
 * Created by zhq_zhao on 2017/7/7. 
 */ 
public class ElseSelectCarinfosActivity extends BaseActivity implements View.OnClickListener { 
 private com.dplustours.b2c.View.view.MyListView else_listview; 
 private Button next_step; 
 private static CheckBox iv_select; 
 private ArrayList elsetcarDetails; 
 @Override 
 protected String setHeadStyleTitle() { 
  return "其他选择"; 
 } 
 @Override 
 protected void requestData() { 
  //租车信息列表 
  elsetcarDetails = new ArrayList(); 
   MulAdapter mRentcarDetailsAdapter = new MulAdapter(this, elsetcarDetails); 
  else_listview.setAdapter(mRentcarDetailsAdapter); 
  elsetcarDetails.add(1); 
  elsetcarDetails.add(1); 
  elsetcarDetails.add(1); 
  mRentcarDetailsAdapter.notifyDataSetChanged(); 
  else_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
   @Override 
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤 
    MulAdapter.ViewHolder viewHolder = (MulAdapter.ViewHolder) view.getTag(); 
    viewHolder.iv_select.toggle();// 把CheckBox的选中状态改为当前状态的反,gridview确保是单一选中 
    MulAdapter.getIsSelected().put(position, viewHolder.iv_select.isChecked());////将CheckBox的选中状况记录下来 
    if (viewHolder.iv_select.isChecked() == true) { 
     Toast.makeText(ElseSelectCarinfosActivity.this,"对勾"+position,Toast.LENGTH_LONG).show(); 
    } else { 
     Toast.makeText(ElseSelectCarinfosActivity.this,"取消"+position,Toast.LENGTH_LONG).show(); 
    } 
    Toast.makeText(ElseSelectCarinfosActivity.this,"当前"+position,Toast.LENGTH_LONG).show(); 
   } 
  }); 
 } 
 @Override 
 protected View getSuccessView() { 
  View view = View.inflate(MyApplication.context, R.layout.activity_else_car, null); 
  else_listview = (com.dplustours.b2c.View.view.MyListView) view.findViewById(R.id.else_listview); 
  next_step = (Button) view.findViewById(R.id.next_step); 
  next_step.setOnClickListener(this); 
  return view; 
 } 
 @Override 
 public void onClick(View v) { 
  switch (v.getId()) { 
   case R.id.next_step: 
    UIHelper.Go(ElseSelectCarinfosActivity.this, CarInfosOkActivity.class); 
    break; 
   default: 
    break; 
  } 
 } 
 public static class MulAdapter extends BaseAdapter { 
  private LayoutInflater inflater = null;//导入布局 
  private Context context; 
  //上下文 
  private ArrayList list; 
  // 控制CheckBox选中情况 
  private static HashMap isSelected; 
  //导入布局 
  public MulAdapter(Context context, ArrayList list) { 
   this.context = context; 
   this.list = list; 
   inflater = LayoutInflater.from(context); 
   isSelected = new HashMap(); 
   initData(); 
  } 
  private void initData() { 
   //初始化isSelected的数据 
   for (int i = 0; i < list.size(); i++) { 
    getIsSelected().put(i, false); 
   } 
  } 
  @Override 
  public int getCount() { 
   return list.size(); 
  } 
  @Override 
  public Object getItem(int position) { 
   return list.get(position); 
  } 
  @Override 
  public long getItemId(int position) { 
   return position; 
  } 
  //listview每显示一行数据,该函数就执行一次 
  @Override 
  public View getView(int position, View convertView, ViewGroup parent) { 
   ViewHolder holder = null; 
   if (convertView == null) { 
    //当第一次加载ListView控件时 convertView为空 
    convertView = inflater.inflate( R.layout.activity_elsecar_details, null); 
    //所以当ListView控件没有滑动时都会执行这条语句 
    holder = new ViewHolder(); 
    holder.iv_select = (CheckBox) convertView.findViewById(R.id.iv_select); 
    convertView.setTag(holder);//为view设置标签 
   } else { 
    //取出holder 
    holder = (ViewHolder) convertView.getTag(); 
    //the Object stored in this view as a tag 
   } 
   if (getIsSelected().get(position)!=null) { 
    // 根据isSelected来设置checkbox的选中状况 
    holder.iv_select.setChecked(getIsSelected().get(position)); 
   } 
   return convertView; 
  } 
  public class ViewHolder { 
   CheckBox iv_select; 
  } 
  public static HashMap getIsSelected() { 
   return isSelected; 
  } 
  public void setIsSelected(HashMap isSelected) { 
   MulAdapter.isSelected = isSelected; 
  } 
 } 
} 
<?xml version="1.0" encoding="utf-8"?> 
 
  
   
    
     
     
    
    
    
    
    
    
     
    
    
   
  
 

以上所述是小编给大家介绍的Android 中CheckBox多项选择当前的position信息提交的示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


分享文章:Android中CheckBox多项选择当前的position信息提交的示例代码
网页网址:http://scyanting.com/article/pspesg.html