我想存儲聯繫人姓名&通過PHP的MySQL數量。問題是數據插入空值。代碼是否正確? 主要Activity.java如何將聯繫人姓名和號碼發送到MySQL數據庫?
public class Activity extends NavigationActivity {
TextInputEditText name1,mobile1;
private static final String TAG = Activity.class.getSimpleName();
private static final int REQUEST_CODE_PICK_CONTACTS = 1;
private Uri uriContact;
private String contactID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offers);
name1 = (TextInputEditText) findViewById(R.id.reference_new_connection_name_1);
mobile1 = (TextInputEditText) findViewById(R.id.reference_new_connection_mobile_1);
name1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
{
name1.setText("");
mobile1.setText("");
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,PICK_CONTACT);
}
}
});
//This is a Button
public void sendReference(View view) {
sendContacts(retrieveContactName(),retrieveContactNumber());
sendReference();
}
public void sendContacts(final String name, final String mobile)
{
final ProgressDialog progressDialog = new MyCustomProgressDialog(Activity.this);
progressDialog.setCancelable(false);
progressDialog.show();
if (ConnectivityReceiver.isConnected()) {
deleteCache(Activity.this);
StringRequest login_request = new StringRequest(Request.Method.POST, Config.SEND_REFERENCE_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Response", response);
progressDialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
if (error instanceof NoConnectionError) {
noConnection();
} else {
Log.e("Error",error.toString());
}
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<>();
params.put("user_id",id);
params.put("Name",name);
params.put("Mobile",mobile);
return params;
}
};
RequestQueue login = Volley.newRequestQueue(Activity.this);
login.add(login_request);
}
else
{
noConnection();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();
retrieveContactName();
retrieveContactNumber();
name1.setText(retrieveContactName());
mobile1.setText(retrieveContactNumber());
name1.setEnabled(false);
mobile1.setEnabled(false);
}
}
private String retrieveContactNumber() {
String contactNumber = null;
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
return contactNumber;
}
private String retrieveContactName() {
String contactName = null;
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
return contactName;
}
public void sendReference()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Reference Sent..")
.setMessage("Thank You..")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Activity.this,SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
請提供您的餐桌數據。列類型可以設置爲int,這意味着0將從頭開始刪除。 –
你有清單權限嗎? –
如果你跑過棉花糖,你還應該給予運行時許可。 –