开发者社区> 问答> 正文

如何将数据从各种片段传递到一个活动并存储在数据库中

我正在使用此库https://github.com/stepstone-tech/android-material-stepper创建分步注册系统

我有5个片段,每个片段都有一个EditText字段。我如何获取所有写入editText字段中的数据并存储在数据库中(我已经有了使用Volley Library通过JSON和PHP将数据存储到数据库中的方法。我可以通过一个活动和布局来完成此操作,但是我发现很难对片段应用相同的逻辑。)

用户名RegisterFragment

public class UsernameRegisterFragment extends Fragment implements Step {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_username_register, container, false);

    //initialize your UI

    return v;
}

@Override
public VerificationError verifyStep() {
    //return null if the user can go to the next step, create a new VerificationError instance otherwise
    return null;
}

@Override
public void onSelected() {
    //update UI when selected
}

@Override
public void onError(@NonNull VerificationError error) {
    //handle error inside of the fragment, e.g. show error on EditText
}
}

EmailRegisterFragment

public class FullnameRegisterFragment extends Fragment implements Step {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_fullname_register, container, false);

    //initialize your UI

    return v;
}

@Override
public VerificationError verifyStep() {
    //return null if the user can go to the next step, create a new VerificationError instance otherwise
    return null;
}

@Override
public void onSelected() {
    //update UI when selected
}

@Override
public void onError(@NonNull VerificationError error) {
    //handle error inside of the fragment, e.g. show error on EditText
}
}

还有大约4个片段,但它们都是相似的。

IntroActivity.java

这是设置适配器的主要活动,也是用户单击最后一个片段中的最后一个按钮(“完成”按钮)后将调用Register方法的活动。

public class IntroActivity extends AppCompatActivity implements StepperLayout.StepperListener{

private StepperLayout mStepperLayout;

private static String URL_REGISTER = "http://78d24f21.ngrok.io/misnap/register.php";
SessionManager sessionManager;

@Override
protected void onCreate(Bundle savedInstanceState) {


    //remove action and status bar

    if (getSupportActionBar() != null)
        getSupportActionBar().hide();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intro);


    mStepperLayout = (StepperLayout) findViewById(R.id.stepperLayout);
    mStepperLayout.setAdapter(new MyStepperAdapter(getSupportFragmentManager(), this));
    mStepperLayout.setListener(this);


}

@Override
public void onCompleted(View completeButton)
{
    Register();
}
.......
//This is where I placed the register method

注册方式

public void Register(){

   final String username = this.username.getText().toString().trim();
   final String email = this.email.getText().toString().trim();
   final String password = this.password.getText().toString().trim();

   StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGISTER,
           new Response.Listener<String>() {
               @Override
               public void onResponse(String response) {
                   try {
                       JSONObject jsonObject = new JSONObject(response);
                       String success = jsonObject.getString("success");
                       if(success.equals("1")){
                           sessionManager.createSession(username, email);

                           Intent intent = new Intent(IntroActivity.this, HomeActivity.class);
                           intent.putExtra("username", username);
                           intent.putExtra("email", email);
                           startActivity(intent);
                       }

                   } catch (JSONException e){
                       e.printStackTrace();
                       Toast.makeText(IntroActivity.this, "Unable to register" + e.toString(), Toast.LENGTH_SHORT).show();
                   }
               }
           },
           new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   Toast.makeText(IntroActivity.this, "Unable to register" + error.toString(), Toast.LENGTH_SHORT).show();
               }
           })
   {
       @Override
       protected Map<String, String> getParams() throws AuthFailureError {
           Map<String, String> params = new HashMap<>();
           params.put("username", username);
           params.put("email", email);
           params.put("password", password);
           return params;
       }
   };

   RequestQueue requestQueue = Volley.newRequestQueue(this);
   requestQueue.add(stringRequest);
}

}

我如何从各个片段中获取ediText数据并将其传递给此寄存器方法,以便可以保存在数据库中?

片段活动的布局只是一个EditText,而简介活动的布局是“下一步”和“后退”按钮。这里是。

activity_intro.xml

<?xml version="1.0" encoding="utf-8"?>
<com.stepstone.stepper.StepperLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/stepperLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:ms_stepperType="progress_bar"
app:ms_nextButtonColor="@color/white"
app:ms_completeButtonColor="@color/white"
app:ms_backButtonColor="@color/white"
/>

展开
收起
几许相思几点泪 2019-12-24 21:21:00 899 0
1 条回答
写回答
取消 提交回答
  • 启动Activity2 startActivityForResult并使用setResult方法将数据从Activity2发送回Activity1。在活动1,你需要重写onActivityResult用于更新TextView与EditText从活性2数据。

    例如:

    在Activity1中,将Activity2启动为:

    Intent i = new Intent(this, Activity2.class); startActivityForResult(i, 1); 在Activity2中,setResult用于发回数据:

    Intent intent = new Intent(); intent.putExtra("editTextValue", "value_here") setResult(RESULT_OK, intent);
    finish(); 在Activity1中,通过以下方式接收数据onActivityResult:

    public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if(resultCode == RESULT_OK) { String strEditText = data.getStringExtra("editTextValue"); }
    } }

    2019-12-26 14:07:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载