Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过checkbok发送自定义列表视图所选项目id_Android - Fatal编程技术网

Android 如何通过checkbok发送自定义列表视图所选项目id

Android 如何通过checkbok发送自定义列表视图所选项目id,android,Android,我是android新手。我想通过复选框发送列表项id。我有一个带有复选框的自定义列表视图。我想在单击复选框时发送,选中的id通过数组将这些id发送到我的mysql数据库。但我不知道如何做 自定义行 我想通过复选框通过此测试id 列表活动 class task2扩展了AsyncTask{ InputStream=null; 字符串结果=”; @凌驾 受保护的void onPreExecute(){ findViewById(R.id.pb).setVisibility(View.VISIBLE);

我是android新手。我想通过复选框发送列表项id。我有一个带有复选框的自定义列表视图。我想在单击复选框时发送,选中的id通过数组将这些id发送到我的mysql数据库。但我不知道如何做

自定义行

我想通过复选框通过此测试id

列表活动

class task2扩展了AsyncTask{
InputStream=null;
字符串结果=”;
@凌驾
受保护的void onPreExecute(){
findViewById(R.id.pb).setVisibility(View.VISIBLE);
}
@凌驾
受保护的Void doInBackground(字符串…字符串){
字符串url_select=”http://mybusket.com/webapi/dgc/dgcbcl/get_all_patient.php";
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(url\u选择);
ArrayList param=新的ArrayList();
试一试{
setURI(新URI(url_select));
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
//阅读内容
is=httpEntity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
试一试{
BufferedReader br=新的BufferedReader(
新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=”;
而((line=br.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
//TODO:处理异常
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
试一试{
lv2=(ListView)findViewById(R.id.testList);
lv2.setAdapter(customTestAdd);
JSONObject对象=新的JSONObject(结果);
JSONArray JSONArray=object.getJSONArray(“pat”);
for(int i=0;i
私人活动;
私人清单项目;
私人充气机;
公共CustomTestAdd(活动、列表项){
这个。活动=活动;
项目=项目;
}
@凌驾
public int getCount(){
返回Items.size();
}
@凌驾
公共对象getItem(int位置){
返回项目。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
convertView=充气机。充气(R.layout.activity\u custom\u test\u add,null,true);
TextView testname=(TextView)convertView.findViewById(R.id.testname);
复选框testid=(复选框)convertView.findViewById(R.id.testid);
测试设置标签(位置);
setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
int pos=(int)buttonView.getTag();
测试设置已检查(已检查);
int id=Items.get(pos.id);
}
}
);
//获取数据
TestAddData m=Items.get(位置);
//设置数据
setText(m.getTestName());
返回视图;
}

从您的代码中,我看到您无法获取选中的复选框,我也处理过类似的问题,因此在您担心向数据库提交数据之前,首先获取复选框的值

在TestAddData中需要一个
数组来保存复选框,在我的例子中,我创建了一个
字符串类型的数组,并传递了复选框的
文本,因为获取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中的数组中,每当您要向数据库提交数据时,只需从数组中提取数据即可。
我希望这有帮助。

这里有一个PHP标记,但显然没有PHP或MySQL代码?这似乎不见了。这里还有大量的Java代码似乎与您的数据库问题无关。@tadman抱歉……我的错误……这是android项目。这点很清楚。问题是MySQL是如何影响的。你能详细说明你到底想做什么吗?
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;
}
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;
}
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] = "";
        }

    }

});