我有一个扩展listactivity的类。我正在解析json文件中的数据并将数据放入列表中。我还创建了一个复选框,以检查学生是否在检查。为此我创建了一个数组来从列表中获取位置。我正在获取学生姓名,但复选框正在给我空值。请帮忙!!!这是我的代码,
public class Student_data extends ListActivity {
private ProgressDialog pDialog;
private Button button;
ListView lv;
// URL to get contacts JSON
private static String url = "http://10.0.2.2/studentdata.json";
private static int[] present, absent;
private static int i = 0, j = 0, x;
// JSON Node names
private static final String TAG_DEP_ID = "dep_id";
private static final String TAG_HEAD = "stdata";
private static final String TAG_NAME = "name";
private static final String TAG_ENROLL = "enrollment";
// contacts JSONArray
JSONArray data = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> studentList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_list);
studentList = new ArrayList<HashMap<String, String>>();
button = (Button) findViewById(R.id.saveData);
button.setOnClickListener(new ButtonClick());
lv = getListView();
new GetContacts().execute();
lv.setOnItemClickListener(new CheckBoxClick());
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Student_data.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
data = jsonObj.getJSONArray(TAG_HEAD);
// looping through All Contacts
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String id = c.getString(TAG_DEP_ID);
String name = c.getString(TAG_NAME);
String enrll = c.getString(TAG_ENROLL);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_DEP_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_ENROLL, enrll);
// adding contact to contact list
studentList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
*/
ListAdapter adapter = new SimpleAdapter(Student_data.this, studentList, R.layout.data_item,
new String[] { TAG_NAME }, new int[] { R.id.name });
setListAdapter(adapter);
}
}
public class CheckBoxClick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
CheckBox chkbox = (CheckBox) arg1;
if (chkbox.isChecked()) {
present[i] = arg2;
i++;
}
if (chkbox.isChecked()) {
absent[j] = arg2;
j++;
}
}
}
public class ButtonClick implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
}
}
data_item.xml ---&GT;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:weightSum="2" >
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/chkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
答案 0 :(得分:0)
试试这个
if (((CheckBox) arg1).isChecked()) {
//your code
}