【发布时间】: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