【问题标题】:How to check and uncheck the Checkbox?如何选中和取消选中复选框?
【发布时间】:2015-08-02 12:25:50
【问题描述】:

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

我的 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();

我的 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);
           }
       });     
    }}

【问题讨论】:

    标签: android json dynamic checkbox sharedpreferences


    【解决方案1】:

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

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

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

    //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);
    

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

    【讨论】:

    • 您好首先感谢您的回答,是的,您从 Json 获取数据的想法正是我所需要的。我可以知道更改 checkButtonClick() 是什么意思吗?我需要用它替换 onClick() 函数吗?
    • 无需替换为您的 onclick 功能。您可以使用您的代码。它只是使代码干净的私有方法。功能相同。
    • 我已经编辑了我的编码,并且在 lv.getAdapter.getCount() 行的 loadPrefs() 处出现错误。我试图删除 getAdapter 这个词,现在我的清单不会显示我数据库中的数据(数据已被替换为单行上的单词 items、数量、数量),并且我的 onClick() 函数也不起作用。跨度>
    • 对不起,如果我让你感到困惑,但你的代码是错误的。不用担心我会指导并更新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多