mardi 26 avril 2016

how to include checkbox in listview when content in listview is loaded by mysql databse in android

this is my activity code where i am fetching column from mysql databse . i have to include checkbox to this code .. plz help me to add check box to this code

public class AddMedicine extends ActionBarActivity {

String myJSON;
String m_name,m_id;

private static final String TAG_RESULTS="result";
private static final String TAG_MED = "m_name";
private static final String TAG_ID = "m_id";


JSONArray peoples = null;

ArrayList<HashMap<String, String>> personList;

ListView list;
private List<ItemObject> offers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_med);

    list = (ListView) findViewById(R.id.listView);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.listView);
    list.setAdapter(adapter);

    //list.setOnItemClickListener(new ListClickHandler());

    personList = new ArrayList<HashMap<String,String>>();
    getData();
}





protected void showList()
{
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String m_name = c.getString(TAG_MED);
            String m_id = c.getString(TAG_ID);


            System.out.println("m_name =" +m_name);
            System.out.println("m_id =" + m_id);

            HashMap<String,String> persons = new HashMap<String,String>();

            persons.put(TAG_MED, m_name);
            persons.put(TAG_ID, m_id);

            personList.add(persons);

        }

        ListAdapter adapter = new SimpleAdapter(
                AddMedicine.this, personList, R.layout.med_list,
                new String[]{TAG_MED},
                new int[]{R.id.m_name}
        );

        list.setAdapter(adapter);
        list.setItemsCanFocus(false);



    } catch (JSONException e) {
        e.printStackTrace();
    }

}

public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://ift.tt/1UepU0o");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}




Aucun commentaire:

Enregistrer un commentaire