Java 从android中的自定义适配器获取复选框的ID?
我有一个自定义ArrayList,如下所示Java 从android中的自定义适配器获取复选框的ID?,java,android,arrays,checkbox,android-arrayadapter,Java,Android,Arrays,Checkbox,Android Arrayadapter,我有一个自定义ArrayList,如下所示 public class sendivitesadapter extends ArrayAdapter<Item>{ private Context context; private ArrayList<Item> items; private qrusers qrusers; private LayoutInflater vi; public sendivitesadapter(Co
public class sendivitesadapter extends ArrayAdapter<Item>{
private Context context;
private ArrayList<Item> items;
private qrusers qrusers;
private LayoutInflater vi;
public sendivitesadapter(Context context,ArrayList<Item> items) {
super(context, 0,items);
this.context= context;
this.qrusers =(qrusers) context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
@Override
public Item getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
sendItem ei = (sendItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
final TextView title = (TextView)v.findViewById(R.id.contactname);
final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
final CheckBox checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
if (title != null)
title.setText(ei.contactname);
if(subtitle != null)
subtitle.setText(ei.companyname);
}
}
return v;
}
@Override
protected void onPostExecute(String result) {
JSONArray jarray;
try {
jarray= new JSONArray(result);
name= new String[jarray.length()];
company=new String[jarray.length()];
for (int i=0;i<jarray.length();i++){
JSONObject jobj = jarray.getJSONObject(i);
name[i]= jobj.getString("Name");
company[i]=jobj.getString("Company");
items.add(new sendItem(name[i], company[i], checkBox));
adapter = new sendivitesadapter(qrusers.this,items);
listView.setAdapter(adapter);
公共类sendivitesadapter扩展了ArrayAdapter{
私人语境;
私有ArrayList项;
私人用户;
私人停车场6号;
公共sendivitesadapter(上下文、ArrayList项){
超级(上下文,0,项);
this.context=context;
this.qrusers=(qrusers)上下文;
这个项目=项目;
vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回super.getCount();
}
@凌驾
公共项getItem(int位置){
//TODO自动生成的方法存根
返回super.getItem(位置);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
最终项目i=项目。获取(位置);
如果(i!=null){
if(i.isSection()){
SectionItem si=(SectionItem)i;
v=vi.充气(R.布局.复选框列表,空);
v、 setOnClickListener(空);
v、 setOnLongClickListener(空);
v、 设置长点击(假);
最终文本视图部分视图=(文本视图)v.findViewById(R.id.list\u item\u section\u text);
sectionView.setText(si.getTitle());
}否则{
sendItem ei=(sendItem)i;
v=vi.充气(R.布局.复选框列表,空);
最终文本视图标题=(文本视图)v.findviewbyd(R.id.contactname);
最终文本视图字幕=(文本视图)v.findViewById(R.id.companyname);
最终复选框CheckBox=(复选框)v.findviewbyd(R.id.checboxlist);
如果(标题!=null)
title.setText(即联系人姓名);
如果(副标题!=null)
副标题.setText(ei.companyname);
}
}
返回v;
}
如下图所示
我的java文件如下所示
public class sendivitesadapter extends ArrayAdapter<Item>{
private Context context;
private ArrayList<Item> items;
private qrusers qrusers;
private LayoutInflater vi;
public sendivitesadapter(Context context,ArrayList<Item> items) {
super(context, 0,items);
this.context= context;
this.qrusers =(qrusers) context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
@Override
public Item getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
sendItem ei = (sendItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
final TextView title = (TextView)v.findViewById(R.id.contactname);
final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
final CheckBox checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
if (title != null)
title.setText(ei.contactname);
if(subtitle != null)
subtitle.setText(ei.companyname);
}
}
return v;
}
@Override
protected void onPostExecute(String result) {
JSONArray jarray;
try {
jarray= new JSONArray(result);
name= new String[jarray.length()];
company=new String[jarray.length()];
for (int i=0;i<jarray.length();i++){
JSONObject jobj = jarray.getJSONObject(i);
name[i]= jobj.getString("Name");
company[i]=jobj.getString("Company");
items.add(new sendItem(name[i], company[i], checkBox));
adapter = new sendivitesadapter(qrusers.this,items);
listView.setAdapter(adapter);
@覆盖
受保护的void onPostExecute(字符串结果){
杰索纳雷·贾雷;
试一试{
jarray=新的JSONArray(结果);
name=新字符串[jarray.length()];
company=新字符串[jarray.length()];
对于(int i=0;i听起来它是View.setTag()
的一个很好的候选者。您可以将每个复选框上的标记设置为用户id[当您创建它时,或者分配名称和公司值]。然后在OnClick
或OnChecked
类型事件中,您可以调用View.getTag()
检索当前选中框的id。您需要使用OnCheckedChangeListener
获取选中的复选框id。这将帮助您-。您需要根据需要修改OnCheckedChangeListener。在适配器中设置复选框中的位置,如
checkBox.setTag(position);
正如我所认为的,在点击添加用户按钮时,必须添加选中的用户。所以,在点击该按钮时,编写以下代码
public void onClick(View v) {
// TODO Auto-generated method stub
String categoryArray = "";
String[] categoryId;
if(v == AddUser){
int count = 0;
for(int i = 0; i < listViewRightSlideMenu.getChildCount(); i ++){
RelativeLayout relativeLayout = (RelativeLayout)listViewRightSlideMenu.getChildAt(i);
CheckBox ch = (CheckBox) relativeLayout.findViewById(R.id.checkBoxCategory); //use same id of check box which you used in adapter
if(ch.isChecked()){
count++;
categoryArray = categoryArray+ch.getTag()+",";
}
}
if(categoryArray.length() > 0) {
categoryArray = categoryArray.substring(0, categoryArray.length() - 1);
String[] array = categoryArray.split(",");
categoryId = new String[array.length];
for(int i = 0; i< array.length; i++) {
categoryId[i] = listCategory.get(Integer.valueOf(array[i])).getId();
}
for(int i = 0; i < categoryId.length; i++){
String a = categoryId[i];
System.out.println("category id is: "+a);
}
System.out.println("array position: "+categoryId);
}
}
public void onClick(视图v){
//TODO自动生成的方法存根
字符串categoryArray=“”;
字符串[]类别ID;
if(v==AddUser){
整数计数=0;
对于(int i=0;i0){
categoryArray=categoryArray.substring(0,categoryArray.length()-1);
String[]数组=categoryArray.split(“,”);
categoryId=新字符串[array.length];
for(int i=0;i
什么是listViewRightSlideMenu?此步骤中包含复选框的listview和什么是listCategory?