当我在{arraylist中获取多个元素的assignbranch
的吐司值时,仅选中最后一个元素复选框。
assignbranch
中的值为[a,b,c]
。敬酒时assignbranch
我为什么要
[a,b,c]
[a,b,c,a,b,c]
[a,b,c,a,b,c,a,b,c]
代码
public class BranchUserAdapter extends ArrayAdapter<BranchModel> {
public ArrayList<BranchModel> EmployeesList;
boolean[] itemChecked;
LayoutInflater vi;
int Resource;
ViewHolder holder;
private Context mContext;
ArrayList<String> assignbranch=new ArrayList<String>();;
ArrayList<String> branch=new ArrayList<String>();
public BranchUserAdapter(Context context, int resource, ArrayList<BranchModel> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
EmployeesList = objects;
this.mContext = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
// new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
holder.branchcheck = (CheckBox) v.findViewById(R.id.checkbox);
holder.branch = (TextView) v.findViewById(R.id.branchuser);
holder.branchcheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
BranchModel planet = (BranchModel) cb.getTag();
planet.setSelected(cb.isChecked());
}
});
v.setTag(holder);
holder.branchcheck.setTag(EmployeesList.get(position));
} else {
holder = (ViewHolder) v.getTag();
((ViewHolder) v.getTag()).branchcheck.setTag(EmployeesList.get(position));
holder.branchcheck.setOnCheckedChangeListener(null);
}
BranchModel Employees = EmployeesList.get(position);
final String sample=EmployeesList.get(position).getName();
SessionManager session;
session = new SessionManager(getContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
final String id = user.get(SessionManager.KEY_EMAIL);
String tag_json_obj = "json_obj_req";
String url = "http://zamzamapp.azurewebsites.net/api/Dept?"+id;
StringRequest req = new StringRequest(Request.Method.GET,url,
new Response.Listener<String>() {
// final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
//"http://emservices.azurewebsites.net/Employee.asmx/CheckUserGet", new Response.Listener<JSONObject>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray empjson = obj.getJSONArray("DDL");
for (int i = 0; i < empjson.length(); i++) {
//Getting json object
JSONObject json = empjson.getJSONObject(i);
//Adding the name of the student to array list
assignbranch.add(json.getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
for(int i=0;i<assignbranch.size();i++){
for(int j=0;j<EmployeesList.size();j++){
if(assignbranch.get(i).equals(EmployeesList.get(j).getName())){
//that means object in second response exists in first response
holder.branchcheck.setChecked(true);
}
}
}
Toast.makeText(getContext(), "hello"+assignbranch.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.getMessage();
// hide the progress dialog
}
}) {
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req, tag_json_obj);
// assignedBranch();
holder.branch.setText(EmployeesList.get(position).getName());
return v;
}
static class ViewHolder {
public CheckBox branchcheck;
public TextView branch;
}
答案 0 :(得分:0)
你可以这样做:
简单移动 复选框点击监听器到 getView 之外的if else条件。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
// new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
holder.branchcheck = (CheckBox) v.findViewById(R.id.checkbox);
holder.branch = (TextView) v.findViewById(R.id.branchuser);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
((ViewHolder) v.getTag()).branchcheck.setTag(EmployeesList.get(position));
}
holder.branchcheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
BranchModel planet = (BranchModel) cb.getTag();
planet.setSelected(cb.isChecked());
}
});
BranchModel Employees = EmployeesList.get(position);
final String sample=EmployeesList.get(position).getName();
SessionManager session;
session = new SessionManager(getContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
final String id = user.get(SessionManager.KEY_EMAIL);
String tag_json_obj = "json_obj_req";
String url = "http://zamzamapp.azurewebsites.net/api/Dept?"+id;
StringRequest req = new StringRequest(Request.Method.GET,url,
new Response.Listener<String>() {
// final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
//"http://emservices.azurewebsites.net/Employee.asmx/CheckUserGet", new Response.Listener<JSONObject>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray empjson = obj.getJSONArray("DDL");
for (int i = 0; i < empjson.length(); i++) {
//Getting json object
JSONObject json = empjson.getJSONObject(i);
//Adding the name of the student to array list
assignbranch.add(json.getString("name"));
if (sample.contains(assignbranch.get(i))){
holder.branchcheck.setChecked(true);
}*/
}
} catch (JSONException e) {
e.printStackTrace();
}
for(int i=0;i<assignbranch.size();i++){
for(int j=0;j<EmployeesList.size();j++){
if(assignbranch.get(i).equals(EmployeesList.get(j).getName())){
//that means object in second response exists in first response
holder.branchcheck.setChecked(true);
}
}
}
Toast.makeText(getContext(), "hello"+assignbranch.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.getMessage();
// hide the progress dialog
}
}) {
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req, tag_json_obj);
// assignedBranch();
holder.branch.setText(EmployeesList.get(position).getName());
return v;
}
希望这会对你有所帮助。