Java 如何在ListFragment中获取Listview的checked事件

Java 如何在ListFragment中获取Listview的checked事件,java,android,checkbox,android-listfragment,Java,Android,Checkbox,Android Listfragment,我有一个JSONArray,它将数据添加到list=newArrayList() 列表显示工作正常。数据被完美地接收和显示 但是当我为它添加一个复选框时,复选框就不起作用了。我查看了一些教程,它们都使用了ListFragment,但在我的教程中不起作用 代码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sav

我有一个JSONArray,它将数据添加到
list=newArrayList()

列表显示工作正常。数据被完美地接收和显示

但是当我为它添加一个复选框时,复选框就不起作用了。我查看了一些教程,它们都使用了ListFragment,但在我的教程中不起作用

代码:

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            rootView = inflater.inflate(R.layout.fragment_list, container, false);

            // Hashmap for ListView
            list = new ArrayList<HashMap<String, String>>();

            listview = (ListView) rootView.findViewById( R.id.list );  

            // When item is tapped, toggle checked properties of CheckBox and Planet.  
            listview .setOnItemClickListener(new AdapterView.OnItemClickListener() {  
              @Override  
              public void onItemClick( AdapterView<?> parent, View item,   
                                       int position, long id) {  
                .......
              }  
            });  


            return rootView;

        }

        class LoadMatches extends AsyncTask<String, String, String> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("Loading Matches..");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected String doInBackground(String... arg0) {
                new ArrayList<NameValuePair>();
                new DefaultHttpClient(new BasicHttpParams());
                DefaultHttpClient defaultClient = new DefaultHttpClient();
                            // Setup the get request
                HttpGet httpGetRequest = new HttpGet(URL_MATCHES);
                            try{
                                // Execute the request in the client
                                HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
                                // Grab the response
                                BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
                                json = reader.readLine();                       
                                }catch(Exception e){
                                    Log.d("Error in JSON",e.toString());
                                }

                try {               
                    istArray= new JSONArray(json);

                    if (matches != null) {
                        // looping through All albums
                        for (int i = 0; i < listArray.length(); i++) {
                            JSONObject c = istArray.getJSONObject(i);

                            // Storing each json item values in variable
                            id = c.getString(TAG_ID);
                            schname = c.getString(TAG_SCHNAME);
                        place= c.getString(TAG_PLACE);
                            datetime = c.getString(TAG_DATETIME);

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

                            // adding each child node to HashMap key => value
                            map.put(TAG_ID, id);
                            map.put(TAG_SCHNAME, schname);
                            map.put(TAG_PLACE, place);
                            map.put(TAG_DATETIME, datetime);



                            // adding HashList to ArrayList
                            list.add(map);
                        }
                    }else{


                        Log.d("Matches: ", "null");
                    }

                } catch (JSONException e) {

                    e.printStackTrace();
                }

                return null;
            }
            @Override
            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all albums
                pDialog.dismiss();
                // updating UI from Background Thread
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        /**
                         * Updating parsed JSON data into ListView
                         * */

                         ListAdapter adapter = new SimpleAdapter(
                                getActivity(), list
                                R.layout.s_list, new String[] { TAG_ID,
                                    TAG_SCHNAME, TAG_PLACE, TAG_DATETIME, }, new int[] {
                                        R.id.list_id, R.id.schname, R.id.place, R.id.datetime });
                        // updating listview
                        list.setAdapter(adapter);
                    }
                });

            }
        }    
  }  
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
rootView=充气机。充气(R.layout.fragment\u列表,容器,false);
//ListView的Hashmap
列表=新的ArrayList();
listview=(listview)rootView.findViewById(R.id.list);
//点击项目时,切换复选框和行星的选中属性。
listview.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父项、视图项、,
int位置,长id){
.......
}  
});  
返回rootView;
}
类LoadMatches扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());
pDialog.setMessage(“加载匹配项…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
新的ArrayList();
新的DefaultHttpClient(新的BasicHttpParams());
DefaultHttpClient defaultClient=新的DefaultHttpClient();
//设置get请求
HttpGet httpGetRequest=新的HttpGet(URL_匹配);
试一试{
//在客户机中执行请求
HttpResponse HttpResponse=defaultClient.execute(httpGetRequest);
//抓住回应
BufferedReader=新的BufferedReader(新的InputStreamReader(httpResponse.getEntity().getContent(),“UTF-8”);
json=reader.readLine();
}捕获(例外e){
d(“JSON中的错误”,例如toString());
}
试试{
istArray=新的JSONArray(json);
如果(匹配!=null){
//循环浏览所有相册
对于(int i=0;ivalue
地图放置(标签标识,标识);
地图放置(标签_SCHNAME,SCHNAME);
地图放置(标记位置、位置);
map.put(TAG_DATETIME,DATETIME);
//将哈希列表添加到ArrayList
列表。添加(地图);
}
}否则{
Log.d(“匹配项:”,“空”);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//获取所有相册后关闭对话框
pDialog.disclose();
//从后台线程更新UI
getActivity().runOnUiThread(新的Runnable()){
@凌驾
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
getActivity(),列表
R.layout.s_列表,新字符串[]{TAG_ID,
TAG_SCHNAME,TAG_PLACE,TAG_DATETIME,},new int[]{
R.id.list_id,R.id.schname,R.id.place,R.id.datetime});
//更新列表视图
list.setAdapter(适配器);
}
});
}
}    
}  
列表布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg">


<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_marginTop="40dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@color/white_overlay_list"
    android:divider="@color/lists_divider"
    android:dividerHeight="1dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="0dp" />

</RelativeLayout>

单列表布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- Album id / Hidden by default -->
    <TextView
        android:id="@+id/list_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:id="@+id/schname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        ndroid:layout_marginLeft="20dp"
        android:layout_marginTop="35dp"
        android:gravity="left"
       android:textColor="@color/black_font_color"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:id="@+id/datetime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"
        android:textColor="@color/black_font_color"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@place
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/datetime"
        android:layout_below="@+id/datetime"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="15dp"

        android:textColor="@color/black_font_color"
        android:textAppearance="?android:attr/textAppearanceMedium" />
     <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="CheckBox" />      


</RelativeLayout>


单击侦听器是在listview上设置的,而不是实际的复选框。您需要在onItemClick中找到复选框并选中它,例如:

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

    CheckBox cb = (CheckBox)view.findViewById(R.id.checkBox);
    cb.setChecked(true);

 }

请注意,然后您必须手动设置onclicklistener中的复选框,如我的原始答案所述

复选框有什么问题?实际上,我不知道是否是我在click listener应用程序中设置的复选框没有响应。您设置的侦听器是针对listview项目的,而不是复选框。但是没有效果,我的意思是没有结果。我用Log.d来测试它。它赢了;当项目单击时不显示我看到了!好的,那么当你说“列表显示工作正常”时,你的意思是listView实际上正确地显示了所有的数据,只是当你点击它时什么也没有发生
android:focusable="false"
android:focusableInTouchMode="false"