我正在使用自定义复选框。我必须在复选框中设置两个复选框值。我在spot_types中得到两个值。
Logcat:
E / spot_types:1,5
SecondActivity.java:
CheckBox checkBox;
spot_types = bundle.getString("spot_types");
Log.e("spot_types",""+spot_types);
到目前为止,我已经尝试了checkBox.setChecked()
。但它无效。我不知道如何使用spot_types字符串值来检查复选框值。任何人都可以帮我解决这个问题。谢谢。
编辑:我从json响应中获取spot_types值,我必须在自定义复选框中检查这两个值。
答案 0 :(得分:0)
尝试这样: ExampleAdapter
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.creatrixone.R;
import com.creatrixone.ThemeColor;
import com.creatrixone.helpers.JSONHandler;
import com.creatrixone.helpers.TextDrawable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ExampleAdapter extends BaseAdapter {
private JSONArray data;
private Context mContext;
private boolean[] checkBoxState;
private String[] selectedpositions;
private TextDrawable check, uncheck;
public ExampleAdapter(Activity activity, JSONArray data, String[] selectedpositions) {
this.mContext = activity;
this.data = data; //no.of rows in your listview
this.selectedpositions=selectedpositions; // you need to pass your spot_types in array
checkBoxState = new boolean[data.length()];
}
public int getCount() {
return data.length();
}
public JSONObject getItem(int position) {
return JSONHandler.getJSONObjectFromJSONArray(data, position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(
R.layout.co_filter_row, null);
holder.textview = (TextView) convertView
.findViewById(R.id.title_content);
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview.setText("Title");
for (int i = 0; i < data.length(); i++) {
String Id = JSONHandler.getStringFromJSONObject(getItem(i), "Id");
for (int j = 0; j < selectedpositions.length; j++) {
try {
if (Id.equals(jsonArray.get(j))) { //implement your logic here
checkBoxState[i] = true;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
holder.checkbox.setSelected(checkBoxState[position]);
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
checkBoxState[position] = isChecked;
}
});
return convertView;
}
static class ViewHolder {
TextView textview;
CheckBox checkbox;
}
}