从textView获取用户名并插入数据库

时间:2016-12-11 18:54:30

标签: android

我想将username插入MySQL数据库。但是,我得到了#Welcome;用户名'在数据库中。我只想插入用户名。我从登录活动中获取用户名。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);

    textView = (TextView) findViewById(R.id.textViewUserName);

    Intent intent = getIntent();

    String username = intent.getStringExtra(ActivityLogin.USER_NAME);

    textView.setText("Welcome" + username);


    buttonSubmit = (Button) findViewById(R.id.buttonSubmit);

    buttonSubmit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if(v == buttonSubmit){
        meetDetails();
    }
}

private void meetDetails() {
    String username=textView.getText().toString().trim().toLowerCase();
    register(username);
}

private void register(String username) {
    class SubmitDetails extends AsyncTask<String, Void, String>{
        ProgressDialog loading;
        RegisterUserClass ruc = new RegisterUserClass();


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(UserProfile.this, "Please Wait",null, true, true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(String... params) {

            HashMap<String, String> data = new HashMap<String,String>();

            data.put("username",params[0]);
            String result = ruc.sendPostRequest(REGISTER_URL,data);

            return  result;
        }
    }

    SubmitDetails ru = new SubmitDetails();
    ru.execute(username);

}

}

3 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为你正在联合欢迎&#39;到onCreate()中的这个TextView。

textView.setText("Welcome" + username);

meetDetails()方法中执行以下操作

更改

String username = textView.getText().toString().trim().toLowerCase();

String username = textView.getText().toString().trim().toLowerCase().replace("Welcome", "");

它取代了欢迎&#39;什么都没有。

答案 1 :(得分:0)

为&#34;欢迎&#34;创建另一个TextView。字符串并将其从行中删除:

textView.setText("Welcome" + username);

答案 2 :(得分:0)

将用户名变量设为本地。或者你可以复制粘贴。

String username;    
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_user_profile); 
     
        textView = (TextView) findViewById(R.id.textViewUserName); 
     
        Intent intent = getIntent(); 
     
        username = intent.getStringExtra(ActivityLogin.USER_NAME); 
     
        textView.setText("Welcome" + username); 
     
     
        buttonSubmit = (Button) findViewById(R.id.buttonSubmit); 
     
        buttonSubmit.setOnClickListener(this); 
    }
    @Override 
    public void onClick(View v) { 
        if(v == buttonSubmit){ 
            meetDetails(); 
        } 
     
    } 
    private void meetDetails() 
    {  

       username.toLowerCase();
        register(username); 
    } 
    private void register(String username) { 
        class SubmitDetails extends AsyncTask<String, Void, String>{
            ProgressDialog loading;
            RegisterUserClass ruc = new RegisterUserClass();
     
     
            @Override 
            protected void onPreExecute() { 
                super.onPreExecute(); 
                loading = ProgressDialog.show(UserProfile.this, "Please Wait",null, true, true);
            } 
     
            @Override 
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
            } 
     
            @Override 
            protected String doInBackground(String... params) {
     
                HashMap<String, String> data = new HashMap<String,String>();
     
                data.put("username",params[0]);
                String result = ruc.sendPostRequest(REGISTER_URL,data);
     
                return  result;
            } 
        } 
        SubmitDetails ru = new SubmitDetails();
        ru.execute(username); 
    }
    }