How to send custom list view selected item id through checkbok

49
May 16, 2018, at 5:40 PM

I am very new in android.I want to send list item id through checkbox.I have custom list view with a checkbox.I want to send when checkbox click, selected id send those id through array to my mysql database.but I don't understand how to do that.

custom row

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

this class extends BaseAdapter..

data save class

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

I want to pass this test id via checkbox..

list activity

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);
    }
}
Answer 1
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;
}
Answer 2

From your code I see you are not able to get which checkbox is selected, I have worked on a similar issue, so before you worry about submitting data to the database, first get the value of the checkbox

you need an array in your TestAddData to hold your checked boxes, in my case I created an array of type String and passed the text of the checkbox, because getting an id would be tedious.

Note: useing an array because it's possible to check many checkboxes

here is my code inside the getView() method

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] = "";
        }
    }
});

now on every box checked, its text will be added to the array in TestAppData, whenever you want to submit data to the data base, just extract the data from the array. I hope this helps.

READ ALSO
android gradle com.android.support libraries version issue

android gradle com.android.support libraries version issue

When i build my gradle, i got a errorHint shows:

60
onAdImpression() not getting Called in Admob Listener

onAdImpression() not getting Called in Admob Listener

In Banner ad onAdImpression() not getting called any more

52
add/remove css class to div via shiny

add/remove css class to div via shiny

I have a select menu in shiny like below

35