2012-04-13 1 views
0
package lab.ex1; 





import java.util.ArrayList; 
import java.util.List; 

import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteQueryBuilder; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import lab.ex1.HospitalDatabase.Patient; 

public class DisplayPatient extends HospitalActivity { 


    @Override 


    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.display); 
     displayData(); 




    } 




    private void displayData() 
    { 

     final List<Integer> myList = new ArrayList<Integer>(); 
     final Intent me = new Intent(DisplayPatient.this,UpdatePatient.class); 
     final Bundle b = new Bundle(); 


     //Build new Patient 
     PatientData build = new PatientData(); 

     final TableLayout viewData = (TableLayout)findViewById(R.id.tblDisplay); 

     SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); 
     queryBuilder.setTables(Patient.patientTableName); 


     SQLiteDatabase db = mDatabase.getReadableDatabase(); 

     String asColumnsToReturn[] = {Patient.patientTableName + "." + Patient.ID, 
             Patient.patientTableName + "." + Patient.firstName, 
             Patient.patientTableName + "." + Patient.lastName, 
             Patient.patientTableName + "." + Patient.room, 
             Patient.patientTableName + "." + Patient.department}; 




     Cursor c = queryBuilder.query(db,asColumnsToReturn,null,null,null,null,Patient.DEFAULT_SORT_ORDER); 



     if (c.moveToFirst()) 
     { 
      for (int i=0; i< c.getCount(); i++) 
      { 
       final TableRow newRow = new TableRow(this); 
       Button update = new Button(this); 
       TextView display = new TextView(this); 


       display.setTextSize(15); 

        update.setX(15); 
        update.setY(10); 
        update.setTextSize(10); 


       Button deleteButton = new Button(this); 

       deleteButton.setX(100); 
       deleteButton.setY(100); 
       deleteButton.setTextSize(10); 

       deleteButton.setText("Delete"); 
       update.setText("Update"); 
       deleteButton.setTag(c.getInt(c.getColumnIndex(Patient.ID))); 

       update.setTag(c.getInt(c.getColumnIndex(Patient.ID))); 
       newRow.setTag(c.getInt(c.getColumnIndex(Patient.ID)));  // set the tag field on the TableRow view so we know which row to delete 

       myList.add(c.getInt(c.getColumnIndex(Patient.ID))); 


       build.setID(Integer.parseInt(c.getString(c.getColumnIndex((Patient.ID))))); 
       build.setFirstName((c.getString(c.getColumnIndex(Patient.firstName)))); 
       build.setLastName(c.getString(c.getColumnIndex(Patient.lastName))); 
       build.setDepartment(c.getString(c.getColumnIndex(Patient.department))); 
       build.setRoom(Integer.parseInt(c.getString(c.getColumnIndex(Patient.room)))); 





       deleteButton.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         Integer id = (Integer) v.getTag(); 
         deletePatient(id); 
         final TableLayout patientTable = (TableLayout) findViewById(R.id.tblDisplay); 

         View viewToDelete = patientTable.findViewWithTag(id); 
         viewData.removeView(viewToDelete); 

        } 
       }); 

       update.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Integer id = (Integer)v.getTag(); 
         final TableLayout patientRmv = (TableLayout)findViewById(R.id.tblDisplay); 


         for (Integer delete : myList) 
         { 
          View viewToDelete = patientRmv.findViewWithTag(delete); 
          viewData.removeView(viewToDelete); 
         } 

         b.putString("key", Integer.toString(id)); 
         me.putExtras(b); 
         startActivity(me); 

        } 
       }); 


       display.setText(build.toString()); 
       newRow.addView(display); 
       newRow.addView(update); 
       //newRow.addView(deleteButton); 

       viewData.addView(newRow); 
       c.moveToNext(); 


      } 

     } 

     else 
     { 
      TableRow newRow = new TableRow(this); 
      TextView noResults = new TextView(this); 
      noResults.setText("No results to show."); 
      newRow.addView(noResults); 
      viewData.addView(newRow); 
     } 
     c.close(); 
     db.close(); 



    } 

    public void deletePatient(Integer id) 
    { 

      SQLiteDatabase db = mDatabase.getWritableDatabase(); 
      String astrArgs[] = { id.toString() }; 
      db.delete(Patient.patientTableName, Patient.ID+ "=?",astrArgs); 
      db.close(); 
    } 



} 



package lab.ex1; 


import lab.ex1.HospitalDatabase.Patient; 
import android.content.ContentValues; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.Toast; 



public class UpdatePatient extends HospitalActivity { 







    Spinner spinner; 
    TextView update; 
    String up; 
    Button check; 

    EditText entry; 
    String setPosition; 
    TextView updateID; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.updatepatient); 

     check = (Button)findViewById(R.id.btnUpdate); 

     entry = (EditText)findViewById(R.id.editUpdate); 
     updateID = (TextView)findViewById(R.id.txtIDUpdate); 




     Bundle accept = getIntent().getExtras(); 

     up = accept.getString("key","0"); 

     updateID.setText(null); 
     updateID.setText("ID: " + up); 


     update = (TextView)findViewById(R.id.txtUpdateMessage); 
     update.setText(null); 
     // update.setText(up); 
     spinner = (Spinner) findViewById(R.id.spnChoice); 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.arrFields, android.R.layout.simple_spinner_item); 
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      spinner.setAdapter(adapter); 

      spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); 



      check.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 

        updateDB(entry.getText().toString(),setPosition,up); 

       } 
      }); 




    } 








    public class MyOnItemSelectedListener implements OnItemSelectedListener { 

     public void onItemSelected(AdapterView<?> adapterView, 
      View view, int pos, long id) { 

      update.setText(null); 
      update.setText("I will update On " + adapterView.getItemAtPosition(pos).toString()); 
      setPosition = adapterView.getItemAtPosition(pos).toString(); 


     } 

     public void onNothingSelected(AdapterView parent) { 
      // Do nothing. 
     } 


    } 




    public void updateDB(String valuesToUpdate, String field,String ID) 
    { 




     SQLiteDatabase db = mDatabase.getWritableDatabase(); 
     db.beginTransaction(); 
     try 
     { 
      ContentValues updatePatient = new ContentValues(); 

      if (field == "First Name") 
      { 
       updatePatient.put(Patient.firstName, valuesToUpdate); 
       String astrArgs[] = { up.toString() }; 
       db.update(Patient.patientTableName,updatePatient, Patient.ID+"=?", astrArgs); 
       // 
       db.setTransactionSuccessful(); 
      } 

     } 


     finally 
     { 
      db.endTransaction(); 
     } 


     db.close(); 


     Intent me = new Intent(UpdatePatient.this,DisplayPatient.class); 
     startActivity(me); 

    } 
} 

我正在使用Android SQLite數據庫。在我的顯示類中,它讀取數據庫中的所有條目,並在每個條目旁邊顯示更新按鈕。該條目更新按鈕的標籤具有條目Patient.ID。當用戶點擊更新時,他們被帶到更新類,他們可以選擇他們想要更新的內容,名字,姓氏,公寓,房間。之後,他們點擊更新,然後他們被帶回到Display類,他們應該看到更改。但是,當他們被收回時,以前的價值仍然存在,變化不會顯現,我錯過了什麼?Android SQLite更新查詢 - 我丟失了什麼

+0

你是什麼意思的「收回」?...你的意思是按下後退按鈕? – 2012-04-13 01:25:51

+0

是的。當用戶按下UpdatePatient表單上的更新時。 – Nexusfactor 2012-04-13 01:36:53

+0

使用startActivityForResults開始活動。將結果讀入onCreate,然後將值分配給您的視圖 – 2012-04-13 01:39:42

回答

0

將您的displaydata()調用從onCreate移動到onResume。

+0

這並沒有工作 – Nexusfactor 2012-04-13 01:14:32

+0

好的......數據庫更新實際上工作嗎? – Barak 2012-04-13 01:17:52

+0

讓我測試,我會回來。 5分鐘 – Nexusfactor 2012-04-13 01:35:29