我是android中的新手。我想通过复选框发送列表项ID。我有一个带有复选框的自定义列表视图。我想在复选框点击时发送,选中的id将这些id通过数组发送到我的mysql数据库。但是我不明白该怎么做。
自定义行
private Activity activity;
private List<TestAddData> Items;
private LayoutInflater inflater;
public CustomTestAdd(Activity activity, List<TestAddData> items) {
this.activity = activity;
Items = items;
}
@Override
public int getCount() {
return Items.size();
}
@Override
public Object getItem(int location) {
return Items.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_custom_test_add, null,true);
TextView testname = (TextView)convertView.findViewById(R.id.testname);
CheckBox testid = (CheckBox) convertView.findViewById(R.id.testid);
//getting data
TestAddData m = Items.get(position);
//set data
testname.setText(m.getTestName());
return convertView;
}
此类扩展了BaseAdapter ..
数据保存类
private int testid;
private String testName;
private boolean isSelected;
public TestAddData(){}
public int getTestid() {
return testid;
}
public void setTestid(int testid) {
this.testid = testid;
}
public String getTestName() {
return testName;
}
public void setTestName(String testName) {
this.testName = testName;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
public TestAddData(int testid, String testName, boolean isSelected) {
this.testid = testid;
this.testName = testName;
this.isSelected = isSelected;
}
我想通过复选框传递此测试ID。
列出活动
class task2 extends AsyncTask<String, String, Void> {
InputStream is = null;
String result = "";
@Override
protected void onPreExecute() {
findViewById(R.id.pb).setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(String... strings) {
String url_select = "http://mybusket.com/webapi/dgc/dgcbcl/get_all_patient.php";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url_select);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
httpGet.setURI(new URI(url_select));
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
// read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
try {
lv2 = (ListView)findViewById(R.id.testList);
lv2.setAdapter(customTestAdd);
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("pat");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
TestAddData patientData = new TestAddData();
patientData.setTestName(obj.getString("name"));
patientData.setTestid(obj.getInt("patid"));
testLists.add(patientData);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
findViewById(R.id.pb).setVisibility(View.GONE);
}
}
答案 0 :(得分:0)
private Activity activity;
private List<TestAddData> Items;
private LayoutInflater inflater;
public CustomTestAdd(Activity activity, List<TestAddData> items) {
this.activity = activity;
Items = items;
}
@Override
public int getCount() {
return Items.size();
}
@Override
public Object getItem(int location) {
return Items.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_custom_test_add, null,true);
TextView testname = (TextView)convertView.findViewById(R.id.testname);
CheckBox testid = (CheckBox) convertView.findViewById(R.id.testid);
testid.setTag(position);
testid.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
int pos=(int)buttonView.getTag();
testid.setChecked(isChecked);
int id=Items.get(pos).id;
}
}
);
//getting data
TestAddData m = Items.get(position);
//set data
testname.setText(m.getTestName());
return convertView;
}
答案 1 :(得分:0)
从您的代码中我看到您无法获得选中的复选框,我已经处理过类似的问题,因此在您担心向数据库提交数据之前,请先获取复选框的值
你的TestAddData需要一个array
来保存你的复选框,在我的情况下,我创建了一个String
类型的数组并传递了text of the checkbox
,因为获取一个id会很乏味
注意:使用数组因为可以检查许多复选框
这是getView()
方法
final CheckBox testId= (CheckBox) v.findViewById(R.id.checkBoxS);
cb.setText(stringList.get(i));
testId.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){ //checked
TestAddData.checkedboxes[i]=testId.getText().toString();
}else { //unchecked
// pass an empty value
TestAddData.checkedboxes[i] = "";
}
}
});
现在每个选中的框都会将其文本添加到TestAppData中的数组中,只要您想将数据提交到数据库,只需从数组中提取数据即可。 我希望这会有所帮助。