2016-02-13 61 views
0

我有一個類,它擴展了listactivity。我正在解析json文件中的數據並將數據放入列表中。我也創建了一個複選框來檢查學生是否存在,如果它檢查。爲此,我創建了一個數組來獲取列表中的位置。我得到的學生名稱,但複選框給我空值。請幫忙!!!這裏是我的代碼,複選框isclicked給空值

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

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

請出示data_item.xml –

+0

編輯我的問題@DougStevenson – huk

+0

我在任何地方都看不到CheckBox視圖。它在哪裏? –

回答

0

試試這個

if (((CheckBox) arg1).isChecked()) { 
      //your code 
} 
+1

那麼這個CheckBox chkbox =(CheckBox)arg1; if(chkbox.isChecked()){}是什麼意思? – huk

+0

它爲我工作 – Dayvon

+0

如果您要添加一些說明和文檔鏈接,答案會變得更有價值,因此原始海報和其他用戶可以從中學習。 –