2016-09-28 1 views
1

私はアンドロイドで出席アプリケーションを実行しようとしています。ユーザーがテキスト編集コンポーネントにセクションを入力すると、学生のリストがmysqlデータベースからチェックボックスチェックボックスにはすでにチェックされているチェックボックスがありません)チェックボックスをオフにすると、生徒は不在として保存する必要があります。そうでなければ生徒はpresent.Canの誰かに詳細を保存するコードを実装する方法を教えてくださいチェックボックス(つまり、存在するかどうか)を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

あなたのメールアドレスを教えてください。私は私のコードを送るでしょう。 – bindhu

関連する問題