2016-09-28 66 views
1

我正在嘗試在android中執行考勤應用程序。當用戶在編輯文本組件中輸入節時,學生的列表是從mysql數據庫以及複選框動態生成的複選框沒有id),這些已經被選中。當用戶取消選中複選框時,學生必須存儲爲不存在,否則必須將學生存儲爲存檔。任何人都可以幫助我實現用於存儲細節的代碼mysql數據庫中的複選框(即存在或不存在)。如何在mysql中存儲動態生成的複選框的詳細信息

在此先感謝。

public class main extends ListActivity implements android.widget.CompoundButton.OnCheckedChangeListener { 

    SQLiteDatabase dBase; 
    private CheckBox checkBox1 
    ListView lv; 
    ArrayList<Student>studentList; 
    StudentAdapter slAdapter; 
    private Context context; 
    String returnString; 

    private String URL_ITEMS; 

    private String section; 

    ProgressDialog progressdialog; 


    private static final String TAG_FIXTURE = "fixture"; 
     private static final String TAG_ID = "id"; 
     private static final String TAG_NAME = "name"; 

     JSONArray matchFixture = null;// to store the result of MySQL query after decoding JSON 
     ArrayList<HashMap<String, String>> matchFixtureList = new ArrayList<HashMap<String, String>>(); 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 




     // GET THE DATA FROM OLD INTENT 
     Intent previous = getIntent(); 
     section = previous.getStringExtra("section"); 
     Log.i("Section: ", " " + section); 

     // Now generate the URL 
     URL_ITEMS = "http://10.0.2.2/attend/getFixture.php?section=" + section; 

     new GetFixture().execute(); 
} 



    // declare parameters that are passed to PHP script i.e. the name "section" and its value submitted by user 
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
    private class GetFixture extends AsyncTask<Void, Void, Void> { 
     @Override 
      protected void onPreExecute() { 
      super.onPreExecute(); 
      progressdialog = new ProgressDialog(main.this); 
      progressdialog.setTitle("Please Wait"); 
      progressdialog.setMessage("Getting Information from Server"); 
      progressdialog.setCancelable(false); 
      progressdialog.show(); 


     } 
     @Override 
      protected Void doInBackground(Void... arg) { 
      ServiceHandler serviceClient = new ServiceHandler(); 
      Log.d("url: ", "> " + URL_ITEMS); 
      String json = serviceClient.makeServiceCall(URL_ITEMS,ServiceHandler.GET); 
      // print the json response in the log 
      Log.d("main.class", " Get match fixture response: > " + json); 
      if (json != null) { 
       try { 
        Log.d("try", "in the try"); 
        JSONObject jsonObj = new JSONObject(json); 
        Log.d("jsonObject", "new json Object"); 
        // Getting JSON Array node 
        matchFixture = jsonObj.getJSONArray(TAG_FIXTURE); 
        Log.d("json aray", "user point array"); 
        int len = matchFixture.length(); 
        Log.d("len", "get array length"); 


        if(matchFixture.length() == 0) { 
         runOnUiThread(new Runnable() { 
          public void run() { 
           Toast.makeText(main.this, "No Data Available for Section " + section, Toast.LENGTH_SHORT).show(); 

          } 
         }); 
        } 



        for (int i = 0; i < matchFixture.length(); i++) { 
         JSONObject c = matchFixture.getJSONObject(i); 
         String id = c.getString(TAG_ID); 
         Log.d("id", id); 

         String name = c.getString(TAG_NAME); 
         Log.d("name", name); 

         // hashmap for single match 
         HashMap<String, String> matchFixture = new HashMap<String, String>(); 
         // adding each child node to HashMap key => value 
         matchFixture.put(TAG_ID, id); 
         matchFixture.put(TAG_NAME, name); 
         matchFixtureList.add(matchFixture); 
        } 
       } 

       catch (JSONException e) { 
        Log.d("catch", "in the catch"); 
        e.printStackTrace(); 
       } 
      } 


      else { 
       Log.e("JSON Data", "Didn't receive any data from server!"); 
       Toast.makeText(main.this, "Check Internet Connection", Toast.LENGTH_SHORT).show(); 
    } 


    return null; 
} 
@Override 
    protected void onPostExecute(Void result) { 
    super.onPostExecute(result); 
    ListAdapter adapter = new SimpleAdapter(
        main.this, matchFixtureList, 
        R.layout.list1_item, new String[] { 
     TAG_ID, TAG_NAME   } 
    , new int[] { 
     R.id.id,R.id.name 

    } 
    ); 
    setListAdapter(adapter); 

    // lv.setAdapter(adapter); 

//lv.setAdapter(slAdapter); 
    if(progressdialog.isShowing()) { 
     progressdialog.dismiss(); 
    } 
} 
    } 
    @Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     Toast.makeText(main.this, "into the on checked change listener..", Toast.LENGTH_SHORT).show(); 



     int pos = lv.getPositionForView(buttonView); 
     if(pos !=ListView.INVALID_POSITION){ 

       for (int i = 0; i < matchFixture.length(); i++) { 


        JSONObject c = null; 
        try { 
         c = matchFixture.getJSONObject(i); 

        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        String id = null; 
        try { 
         id = c.getString(TAG_ID); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        String name = null; 
        try { 
         name = c.getString(TAG_NAME); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
     SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null); 
           database.execSQL("CREATE TABLE IF NOT EXISTS attendance(id integer primary key autoincrement,name VARCHAR(150),id INT(10)"); 
           database.execSQL("INSERT INTO attendance(name,id) VALUES('" + name + "'," + id+");"); 
           Toast.makeText(main.this, "record inserted..", Toast.LENGTH_SHORT).show(); 
           database.close(); 
       } 


                 } 
    } 



     /** You can not insert empty values and you should create Database and tables first **/ 


    //ContentValues values= new ContentValues(); 
// dBase.insert("attendance", null, values); 
// Toast.makeText(getApplicationContext(), "Record Inserted..", 2000).show(); 

回答

0
  1. 使用setTag()和getTag()方法用於識別對應的複選框。

     // set the ID before rendering 
         chcekbox.setTag(ID); 
    
         // get the ID when this method called "onCheckedChanged"(before storing into the database) 
         buttonView.getTag(); 
    
+0

在我的代碼學生姓名和複選框被動態地從MySQL數據庫生成的。那麼我如何在代碼中使用setTag()和getTag()。 – bindhu

+0

請給我你的電子郵件id.I將發送我的代碼。 – bindhu

相關問題