【问题标题】:Android How to pass data from class to activity? [duplicate]Android 如何将数据从类传递到活动? [复制]
【发布时间】:2016-11-15 16:08:50
【问题描述】:

我在将数据(值)从 java 类传递到活动并在 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。此活动 (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);
    }

非常感谢您的帮助。

【问题讨论】:

  • 是错字吗,usernamepassword 的键相同?
  • 有人问过类似的问题here
  • 但是我想要一个结果。它是来自mysql的json。不是密码和用户名。在 json 中有姓名、姓氏、年龄等信息。我想获取这个 json 并解析,然后用这些信息填充 textViews。

标签: android


【解决方案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 中,因此您的用户名和密码都为 null 并且您什么也看不到。

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

 //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 用于在不同应用执行之间保存数据(当您关闭和打开应用时,您将能够访问数据,前提是您先保存数据)。

【讨论】:

  • 但是我想要一个结果。它是来自mysql的json。不是密码和用户名。在 json 中有姓名、姓氏、年龄等信息。我想获取这个 json 并解析,然后用这些信息填充 textViews。
  • 在这种情况下,您根本不需要 sharedpreferences...因为您有一个后端数据库来保留信息。你需要从你的数据库中读取
猜你喜欢
  • 2017-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
相关资源
最近更新 更多