【问题标题】:How to get checked event of Listview in ListFragment如何在 ListFragment 中获取 Listview 的检查事件
【发布时间】:2014-09-19 04:30:58
【问题描述】:

我有一个 JSONArray,它将数据添加到 list = new ArrayList<HashMap<String, String>>();

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

但是当我为其添加一个复选框时,该复选框将不起作用。我检查了一些教程都使用了 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);
                    }
                });

            }
        }    
  }  

列表布局

<?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>

【问题讨论】:

  • 您在使用复选框时遇到了什么问题?
  • 其实我不知道我在点击侦听器应用程序集中放置的复选框是否没有响应
  • 你设置的监听器是列表视图项而不是复选框。

标签: java android checkbox android-listfragment


【解决方案1】:

点击监听器设置在您的列表视图上,而不是实际的 CheckBox。您将需要以某种方式在 onItemClick 中获取复选框并选中它,例如:

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

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

 }

这是假设 View item 是您的个人 listView 项目

编辑:

当您在列表视图中有一个复选框时,它将拦截触摸事件。在列表项的 xml 文件中,将以下内容添加到元素中:

android:focusable="false"
android:focusableInTouchMode="false"

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

【讨论】:

  • 但是没有效果我的意思是没有结果。我用 Log.d 来测试它。它不会在单击项目时出现
  • 啊,我明白了!好的,所以当您说“列表显示工作正常”时,您的意思是 listView 实际上正确显示所有数据,只是单击时没有任何反应?
  • 显示正确。雅不在任何点击事件中工作。我使用带有列表视图的片段。当我使用 ListFragment 应用程序停止
  • 好的,如果列表显示正确,那么我认为这只是一个错字,但是在您的 onPostExecute() 中,您将适配器设置在列表上,并且应该在 listView 上进行设置。等我回家再看看,现在下班了耶
  • 好吧。如果你能帮我用上面代码中的复选框的切换按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
相关资源
最近更新 更多