Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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 如何选中和取消选中复选框?_Android_Json_Dynamic_Checkbox_Sharedpreferences - Fatal编程技术网

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

Android 如何选中和取消选中复选框?,android,json,dynamic,checkbox,sharedpreferences,Android,Json,Dynamic,Checkbox,Sharedpreferences,我正在创建一个包含XAMPP和JSON的清单。作为将复选框状态保存到SharedReferences之前的第一步,是否有人可以帮助我在setOnItemClickListener中选中/取消选中复选框?我在我的onPostExecute中使用SimpleAdapter,因此我不知道如何选中/取消选中复选框。我发现的所有示例都经常使用viewHolder和自定义适配器。我无法覆盖SimpleAdapter的getView 我的AllDetails类扩展了ListActivity private P

我正在创建一个包含XAMPP和JSON的清单。作为将复选框状态保存到SharedReferences之前的第一步,是否有人可以帮助我在
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();
private ProgressDialog pDialog;
JSONParser jParser=新的JSONParser();
ArrayList productsList;
//获取所有产品列表的url
私有静态字符串url\u所有产品=”http://10.207.200.73/list/get_details.php";
///JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“list”;
私有静态最终字符串标记_PID=“数量”;
私有静态最终字符串标记_NAME=“items”;
//产品JSONArray
JSONArray产品=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.all_产品);
ListView lv=getListView();
//单击项目上的Listview侦听器
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
最终复选框cb=(复选框)view.findViewById(R.id.CheckBox);
}
});
//在后台线程中加载产品
新建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);
           }
       });     
    }}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
**/class LoadAllProducts扩展异步任务{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(AllDetails.this);
pDialog.setMessage(“正在加载数据,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*从url获取所有产品
* */
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_all_products,“GET”,params);
//检查日志cat中的JSON响应
Log.d(“所有产品:,json.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
products=json.getJSONArray(TAG_products);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(标签PID,id);
地图放置(标签名称、名称);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
} 
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//获取所有产品后关闭对话框
pDialog.disclose();
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
AllDetails.this,productsList,
R.layout.list_项,新字符串[]{TAG_PID,
TAG_NAME},
新int[]{R.id.quantity,R.id.items});
//更新列表视图
setListAdapter(适配器);
}
});     
}}
这将引导您从自定义适配器获取复选框值并保存到共享首选项。修改此代码以从json提供信息

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

在您的onPostExecute()和change
checkButtonClick()
中,从dataAdapter保存您的共享首选项

您好首先感谢您的回答,是的,我需要您从Json获取数据的想法。我可以知道change checkButtonClick()是什么意思吗?我需要替换onClick()吗函数?无需替换为onclick函数。您可以使用代码。它只是使代码干净的私有方法。功能相同。我编辑了代码,在loadPrefs()的第lv.getAdapter.getCount()行出现错误。我试图删除getAdapter一词,现在我的检查表不会显示数据库中的数据(数据已被替换为单词items,quantity,quantity,单行)并且我的onClick()函数也不起作用。很抱歉,如果我让您感到困惑,但您的代码是错误的。不要担心,我将引导并更新答案
//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);