android - 如何通过checkbok发送自定义列表视图选定的项目ID
问题描述
我是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);
}
}
解决方案
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;
}
推荐阅读
- angular - 批处理文件运行 npm start
- java - 使用 libphonenumber 时使用 Iterator.next() 获取 java.util.NoSuchElementException
- c# - 如何防止 TextBlock 占用所有可用空间
- node.js - 如何在 Google Cloud PubSub 中为订阅提供过期时间
- javascript - 如何使用Javascript从未知大小的数组中获取未知大小的数据?
- php - 有没有比这更好的解决方案来打印结果
- python-3.x - --latency-wait 错误 MissingOutputException
- php - PHP,使用后需要覆盖或删除文件
- php - 提供的令牌格式错误或无效
- php - 启用以在产品描述页面中获取自定义字段值