首页 > 解决方案 > 如何通过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);
    }
}

标签: android

解决方案


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;
}

推荐阅读