Android如何将数据从类传递到活动?

时间:2016-11-15 16:08:51

标签: android

我在将数据(值)从java类传递给activity时遇到问题,并在textView中显示它。共享偏好无效。 这是我班上的片段:

else if (type.equals("getUser")) {
            try {
                String user_name = params[1];
//                String password = params[2];
                URL url = new URL(getUser_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
//                String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
//                        + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                System.out.println(result);
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
@Override
    protected void onPostExecute(String result) {
        String temp = "Login success. Welcome!";

        if (result.equals(temp)) {
            Intent intent = new Intent(context, ProjectsActivity.class);
            context.startActivity(intent);
        } else if (result.contains("[{")) {

        } else {
            alertDialog.setMessage(result);
            alertDialog.show();
        }
    }

我希望将结果传递给textview in activity。此活动(UserAreaActivity)。

public class UserAreaActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_area);


        TextView textViewUserArea = (TextView) findViewById(R.id.textViewUserArea);
        EditText editTextName = (EditText) findViewById(R.id.editTextName);
        EditText editTextSurname = (EditText) findViewById(R.id.editTextrSurname);
        EditText editTextUsername = (EditText) findViewById(R.id.editTextUsername);
        EditText editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        EditText editTextAge = (EditText) findViewById(R.id.editTextAge);

//
        String type = "getUser";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username);
    }

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

 SharedPreferences myprefs = getSharedPreferences("user", MODE_WORLD_READABLE);
    String username = myprefs.getString("username", null);
    String password = myprefs.getString("password", null); //Typo, you had used "username" here

在调用此方法之前,您不会在sharedpreferences中保存数据,因此您的用户名和密码都会为空,并且您看不到任何内容。

您需要先保存数据,然后才能随时访问它们。

 //Code for writing into shared preferences

  SharedPreferences myprefs = getSharedPreferences("user", MODE_WORLD_READABLE);
   myprefs.edit().putString("username","Name to be saved").commit();
   myprefs.edit().putString("password","Password to be saved").commit();

SharedPreferences用于在不同的应用执行之间保存数据(当您关闭并打开应用时,您将能够访问数据,前提是您先保存数据)。