如何选中和取消选中复选框?

时间:2020-12-29 10:04:02

I am creating a checklist that involves XAMPP and JSON. As my first step before saving the Checkbox state into sharedPreferences, can somebody help me on how to check/uncheck the Checkbox in the setOnItemClickListener? I'm using SimpleAdapter in my onPostExecute therefore I do not know how to check/uncheck the Checkbox. All example that I found often use viewHolder and custom adapter. I can't override thegetViewfor SimpleAdapter.

我正在创建一个涉及XAMPP和JSON的清单。作为将Checkbox状态保存到sharedPreferences之前的第一步,有人可以帮助我检查/取消选中setOnItemClickListener中的Checkbox吗?我在onPostExecute中使用SimpleAdapter,因此我不知道如何选中/取消选中Checkbox。我发现的所有示例经常使用viewHolder和自定义适配器。我无法覆盖getViewfor SimpleAdapter。

My AllDetails class extends a ListActivity.

我的AllDetails类扩展了ListActivity。

private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;

// url to get all products list
private static String url_all_products = "http://10.207.200.73/list/get_details.php";

/// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "list";
private static final String TAG_PID = "quantity";
private static final String TAG_NAME = "items";

// products JSONArray
JSONArray products = null; 

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.all_products);

  ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

             final CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);

        }
    });

   // Loading products in Background Thread
   new LoadAllProducts().execute();

My JSON part

我的JSON部分

/**
* Background Async Task to Load all product by making HTTP Request
* */class LoadAllProducts extends AsyncTask<String, String, String> {
   /**
    * Before starting background thread Show Progress Dialog
    * */
   @Override
   protected void onPreExecute() {
       super.onPreExecute();
       pDialog = new ProgressDialog(AllDetails.this);
       pDialog.setMessage("Loading Data. Please wait...");
       pDialog.setIndeterminate(false);
       pDialog.setCancelable(false);
       pDialog.show();
   }

   /**
    * getting All products from url
    * */
   protected String doInBackground(String... args) {
       // Building Parameters
       List<NameValuePair> params = new ArrayList<NameValuePair>();

       // getting JSON string from URL
       JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);


       // Check your log cat for JSON reponse
       Log.d("All Products: ", json.toString());

       try {
           // Checking for SUCCESS TAG
           int success = json.getInt(TAG_SUCCESS);

           if (success == 1) {
               // products found
               // Getting Array of Products
               products = json.getJSONArray(TAG_PRODUCTS);

               // looping through All Products
               for (int i = 0; i < products.length(); i++) {
                   JSONObject c = products.getJSONObject(i);

                   // Storing each json item in variable
                   String id = c.getString(TAG_PID);
                   String name = c.getString(TAG_NAME);

                   // creating new HashMap
                   HashMap<String, String> map = new HashMap<String, String>();

                   // adding each child node to HashMap key => value
                   map.put(TAG_PID, id);
                   map.put(TAG_NAME, name);

                   // adding HashList to ArrayList
                   productsList.add(map);
               }
           } 
       } catch (JSONException e) {
           e.printStackTrace();
       }

       return null;
   }

   /**
    * After completing background task Dismiss the progress dialog
    * **/
   protected void onPostExecute(String file_url) {
       // dismiss the dialog after getting all products
       pDialog.dismiss();
       // updating UI from Background Thread
       runOnUiThread(new Runnable() {
           public void run() {
               /**
                * Updating parsed JSON data into ListView
                * */
               ListAdapter adapter = new SimpleAdapter(
                       AllDetails.this, productsList,
                       R.layout.list_item, new String[] { TAG_PID,
                               TAG_NAME},
                       new int[] { R.id.quantity, R.id.items });

               // updating listview
               setListAdapter(adapter);
           }
       });     
    }}

1 个解决方案

#1


0  

This link will guide you to get checkbox value from custom adapter and save to shared preference. Modify this code to feed the info from json

此链接将指导您从自定义适配器获取复选框值并保存到共享首选项。修改此代码以从json提供信息

ArrayList<Country> countryList = new ArrayList<Country>();
Country country = new Country("AFG","Afghanistan",false);
countryList.add(country);
...

with your product list and pass to custom adapter

使用您的产品列表并传递给自定义适配器

//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,
R.layout.country_info, countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);

in your onPostExecute() and change checkButtonClick() to save your shared preference from dataAdapter

在onPostExecute()中并更改checkButtonClick()以从dataAdapter保存共享首选项

#1


0  

This link will guide you to get checkbox value from custom adapter and save to shared preference. Modify this code to feed the info from json

此链接将指导您从自定义适配器获取复选框值并保存到共享首选项。修改此代码以从json提供信息

ArrayList<Country> countryList = new ArrayList<Country>();
Country country = new Country("AFG","Afghanistan",false);
countryList.add(country);
...

with your product list and pass to custom adapter

使用您的产品列表并传递给自定义适配器

//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,
R.layout.country_info, countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);

in your onPostExecute() and change checkButtonClick() to save your shared preference from dataAdapter

在onPostExecute()中并更改checkButtonClick()以从dataAdapter保存共享首选项