Android SQLite更新查询 - 我缺少什么

时间:2012-04-13 00:09:38

标签: java android sqlite

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类,他们应该看到更改。然而,当它们被收回时,之前的值仍然存在,更改没有显示,我错过了什么?

1 个答案:

答案 0 :(得分:0)

将displaydata()调用从onCreate移至onResume。