正在异步任务类中获取共享首选项



我正在尝试构建一个应用程序,该应用程序将患者id作为共享首选项,并在另一个活动中使用该id来获取该id的记录。在"主活动"中,我设置了"共享首选项",它正确地设置了值。然而,在FetchSinglePatientData中,我无法获得相同的共享偏好值。

在那个错误发生之前,我什么都没得到。我的代码在下面:

public void getSinglePatient(View v)
{
etID = findViewById(R.id.editTextID);
SharedPreferences sharedPref = getSharedPreferences("patientId", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("patientId",etID.getText().toString());
editor.apply();
String xx = sharedPref.getString("patientId","hayamk");
Log.d("XX","DEGER" + xx);
//instantiate intent class
Intent intent=new Intent(MainActivity.this, GetSinglePatient.class);
//start the activity
startActivity(intent);
}

GetSinglePatient活动,此活动使用后台中的fetchSinglePatientData。etchSinglePatinData如下所示:

package project.android.mapd713.college.centennial.com.mapd713application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class fetchSinglePatientData extends AsyncTask<Void,Void,Void> {
String data = "";
String dataParsed = "";
String singleParsed = "";
JSONObject myObject;
private Context ctx;
public fetchSinglePatientData(Context ctx) {
this.ctx = ctx;
}

@Override
protected Void doInBackground(Void... voids) {

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx);
//String patientId = prefs.getString("patientId", "");
String xx = sharedPref.getString("patientId","fafafa");
Log.d("XX2","DEGE2R" + xx);


Log.i("fonksiyon","ICINE GIRDI");
try {
URL url = new URL("https://mapd713prjct.herokuapp.com/patients/5bf63c770fc33ea59c9c3a97");
Log.i("URL","URL ICINE GIRDI");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null) {
line = bufferedReader.readLine();
data = data + line;
}
myObject = new JSONObject(data);
myObject.getString("doctor");
Log.d("DOKTOR BU NE","hmm" + myObject.getString("doctor"));

} catch (MalformedURLException e) {
e.printStackTrace();
System.out.print("HATA 1: " + e);
} catch (IOException e) {
e.printStackTrace();
System.out.print("HATA 2: " + e);
} catch (JSONException e) {
e.printStackTrace();
System.out.print("HATA 3: " + e);
}

return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
System.out.println("girdi");
Log.i("onPostExecute","GIRDI");
GetSinglePatient.mTextViewResult.setText(myObject.toString());
}
}

日志如下,有两个不同的输入,1(jajaja和2(呵呵

2018-12-01 22:38:12.360 7470-7470/project.android.mapd713.college.centennial.com.mapd713application D/XX: DEGERjajaja
2018-12-01 22:38:12.816 7470-
7497/project.android.mapd713.college.centennial.com.mapd713application D/XX2: DEGE2Rfafafa
2018-12-01 22:43:05.644 7470-7470/project.android.mapd713.college.centennial.com.mapd713application D/XX: DEGERhehehe
2018-12-01 22:43:05.815 7470-7547/project.android.mapd713.college.centennial.com.mapd713application D/XX2: DEGE2Rfafafa

非常感谢!

您使用两种不同的方法来获得SharedPreferences对象。首先使用Context.getSharedPreferences((方法,该方法采用首选项文件名。然后使用静态方法PreferenceManager.getDefaultSharedPreferences((。这将导致使用两个不同的SharedPreference文件。只要选择一种或另一种方式,保持一致,就会更好地发挥作用。

要解决此问题,请使用带有define name和mode的共享首选项。对于示例:

SharedPreferences SharedPreference = context.getSharedPreferences("defined 
name" , Context.MODE_PRIVATE);
  1. 要在共享首选项中插入数据而不延迟,请使用commit((而不是apply((
editor.commit();
  1. 并将ApplicationContext发送到异步任务类

我通过更改以下代码解决了问题:

SharedPreferences sharedPref = getSharedPreferences("patientId", Context.MODE_PRIVATE);

带有

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

的确,我使用了两种不同的SharedPreference方法,因此,我无法获得患者id。然而,更改

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx);

带有

SharedPreferences sharedPref = getSharedPreferences("patientId", Context.MODE_PRIVATE);

不起作用,因为getSharedPreferences((需要访问上下文。

在我看来,这对安卓系统来说有点棘手。我建议这些帖子:

post1 post2

相关内容

  • 没有找到相关文章

最新更新