我在MyDBHandler类中有两个方法,一个用于添加详细信息,另一个用于更新详细信息。用户填写表单并将其详细信息添加到数据库中并作为listView输出。我希望用户能够再次填写相同的表单来更新他们的详细信息。我的问题是,当我第一次安装应用程序时,没有更新的记录,所以我必须使用addDetails方法,当我想要做的是使用updateDetails方法更新细节时,不断向数据库添加新行。如果首次安装应用程序时无法更新,我无法更新详细信息。
addDetails方法
//Add a new row to the database
public void addDetails(Details details) {
ContentValues values = new ContentValues();
values.put(COLUMN_FIRSTNAME, details.getFirstname());
values.put(COLUMN_SURNAME, details.getSurname());
values.put(COLUMN_PHONE, details.getPhone());
values.put(COLUMN_EMAIL, details.getEmail());
values.put(COLUMN_ADDRESS1, details.getAddress1());
values.put(COLUMN_ADDRESS2, details.getAddress2());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_DETAILS, null, values);
db.close();
}
updateDetails方法
//Updates user details
public void updateDetails(Details details){
ContentValues values = new ContentValues();
//Take new values from the details object provided
values.put(COLUMN_FIRSTNAME, details.getFirstname());
values.put(COLUMN_SURNAME, details.getSurname());
values.put(COLUMN_PHONE, details.getPhone());
values.put(COLUMN_EMAIL, details.getEmail());
values.put(COLUMN_ADDRESS1, details.getAddress1());
values.put(COLUMN_ADDRESS2, details.getAddress2());
SQLiteDatabase db = this.getWritableDatabase();
//Update the details object where id matches
db.update(TABLE_DETAILS, values, COLUMN_ID + "='" + details._id + "'", null);
db.close();
}
FragmentTab1类
import com.astuetz.viewpager.extensions.sample.*;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.widget.Toast;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class FragmentTab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
super.onCreate(savedInstanceState);
//setContentView(R.layout.fragment1);
firstName = (TextView) rootView.findViewById(R.id.firstName);
editTextName = (EditText) rootView.findViewById(R.id.editTextName);
textView5 = (TextView) rootView.findViewById(R.id.surName);
editTextSurname = (EditText) rootView.findViewById(R.id.editTextSurname);
textView4 = (TextView) rootView.findViewById(R.id.mobile);
editTextMobile = (EditText) rootView.findViewById(R.id.editTextMobile);
textView2 = (TextView) rootView.findViewById(R.id.Email);
editTextEmail = (EditText) rootView.findViewById(R.id.editTextEmail);
textView3 = (TextView) rootView.findViewById(R.id.address1);
editTextAddress1 = (EditText) rootView.findViewById(R.id.editTextAddress1);
textView6 = (TextView) rootView.findViewById(R.id.address2);
editTextAddress2 = (EditText) rootView.findViewById(R.id.editTextAddress2);
dbHandler = new MyDBHandler(getActivity(), null, null, 1);
Button addButtonClicked = (Button)rootView.findViewById(R.id.addButtonClicked);
addButtonClicked.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
addButtonClicked(v);
}
});
return rootView;
}
TextView firstName;
EditText editTextName;
TextView textView5;
EditText editTextSurname;
TextView textView4;
EditText editTextMobile;
TextView textView2;
EditText editTextEmail;
TextView textView3;
EditText editTextAddress1;
TextView textView6;
EditText editTextAddress2;
MyDBHandler dbHandler;
//validate email provided by user
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
//Add details to the database and shared preferences
public void addButtonClicked(View view) {
final String email = editTextEmail.getText().toString();
if (!isValidEmail(email))
{
editTextEmail.setError("Invalid Email");
Toast.makeText(getActivity().getApplicationContext(), "Invalid email format",
Toast.LENGTH_LONG).show();
}
else if( editTextName.getText().toString().length() == 0 )
{
editTextName.setError("First name is required!");
Toast.makeText(getActivity().getApplicationContext(), "First name is required",
Toast.LENGTH_LONG).show();
}
else if( editTextSurname.getText().toString().length() == 0 )
{
editTextSurname.setError("Surname is required!");
Toast.makeText(getActivity().getApplicationContext(), "Surname is required",
Toast.LENGTH_LONG).show();
}
else if( editTextMobile.getText().toString().length() == 0 || editTextMobile.getText().toString().length() < 10 )
{
editTextMobile.setError("Not a valid number!");
Toast.makeText(getActivity().getApplicationContext(), "Not a valid number",
Toast.LENGTH_LONG).show();
}
else if( editTextAddress1.getText().toString().length() == 0 )
{
editTextAddress1.setError("Address Line 1 is required!");
Toast.makeText(getActivity().getApplicationContext(), "Address Line 1 is required",
Toast.LENGTH_LONG).show();
}
else if( editTextAddress2.getText().toString().length() == 0 )
{
editTextAddress2.setError("Address Line 2 is required!");
Toast.makeText(getActivity().getApplicationContext(), "Address Line 2 is required",
Toast.LENGTH_LONG).show();
}
else
{
Details details = new Details();
details.set_id(1);
details.setFirstname(editTextName.getText().toString());
details.setSurname(editTextSurname.getText().toString());
details.setPhone(editTextMobile.getText().toString());
details.setEmail(editTextEmail.getText().toString());
details.setAddress1(editTextAddress1.getText().toString());
details.setAddress2(editTextAddress2.getText().toString());
dbHandler.addDetails(details);
Toast.makeText(getActivity().getApplicationContext(), "Details saved successfully",
Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
你必须检查表是否为空,为此,在'MyDBHandler'中创建一个名为'Exists'的方法:
public boolean Exists {
SQLiteDatabase db = this.getWritableDatabase();
String Query = "SELECT * FROM TABLE_DETAILS " ;
Cursor cursor = db.rawQuery(Query,null);
if(cursor.moveToFirst()){
Log.i("CHECK", "true");
return true;
}
return false;
}
然后在您的活动通话中存在类似的内容:
MyDBHandler db;
db = new MyDBHandler(getApplicationContext());
db.getWritableDatabase();
if ((db.Exists( ))) {
Toast.makeText(getApplicationContext(), "Please add first your Details", Toast.LENGTH_SHORT).show();
}else{
//update your Database
}
希望这有帮助