Android 片段中EditText(来自GridView)的用户输入未持久化
我在GridView中有一个默认的文本和图像,当用户单击EditText时,他们应该能够更改该值并通过SharedReferences将其保存,因此即使应用程序被终止,EditText也会保留更改 然而,这是行不通的。当我关闭应用程序并返回时,EditText会再次显示默认值。我猜这是因为我是如何使用LayoutInflater的,虽然我不确定,因为LayoutInflater Android文档不是很明确。我在GridView适配器中使用了一次充气器,在片段布局中使用了一次充气器,然后在片段中再次使用充气器来获取EditText,以便用户可以对其进行更改。我应该只使用两个充气器,就像使用适配器充气器两次一样,但如何使用?我认为我的SharedReferences信息是正确的,但我可能错了。有什么建议吗?多谢各位 CuteCollectionFragment.java fragment_cute_collection.xml grid_row.xmlAndroid 片段中EditText(来自GridView)的用户输入未持久化,android,android-layout,gridview,android-fragments,sharedpreferences,Android,Android Layout,Gridview,Android Fragments,Sharedpreferences,我在GridView中有一个默认的文本和图像,当用户单击EditText时,他们应该能够更改该值并通过SharedReferences将其保存,因此即使应用程序被终止,EditText也会保留更改 然而,这是行不通的。当我关闭应用程序并返回时,EditText会再次显示默认值。我猜这是因为我是如何使用LayoutInflater的,虽然我不确定,因为LayoutInflater Android文档不是很明确。我在GridView适配器中使用了一次充气器,在片段布局中使用了一次充气器,然后在片段中
请在适配器类中使用加载和保存首选项 并使用首选项值而不是常量值更新mediaTitle
mediaTitle.setText("Add title");
首选项代码使用此
SharedPreferences mSharedPreferences =getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE);
/*
* TO WRITE VALUES INTO THE PREFERENCE FILE
*/
//get Editor object using sharedPreference edit() Method
Editor mEditor= mSharedPreferences.edit();
//set boolean Values into preference files
mEditor.putString("stringValue", "hello");
//set integer value into the preference file
mEditor.commit();
/*
* TO READ THE VALUES FROM THE PREFERENCE FILE
*/
//get the String value from preference
String stringValue=mSharedPreferences.getString("stringValue", "default value");
谢谢,但那不行。我不能将方法放在一个类中,而只能放在一个活动或片段中,我认为这是因为充气器代码由于某种原因没有到达方法中的EditText,因为我一直在mediaTitle变量上获取空指针,因为当我试图打开片段时它崩溃了。我不想将整个代码放在getView中,这样可以访问mediaTitle,因为它只是第一次设置空视图,没有动态用户交互。此外,getApplicationContext在片段或类中都不起作用。getActivity适用于片段,getContext适用于类。我确实将常量更改为字符串,但不幸的是并没有解决我的问题。将getActivity作为上下文传递给适配器并使用它们
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2198bb" >
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right"
android:background="@drawable/button_border" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/media_item"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" >
</ImageView>
<EditText
android:id="@+id/media_title"
android:layout_width="105dp"
android:layout_height="40dp"
android:textSize="15sp"
android:gravity="center"
android:maxLines="1"
android:maxLength="10"
android:inputType="text"
android:imeOptions="actionDone"
android:layout_gravity="center">
</EditText>
</LinearLayout>
mediaTitle.setText("Add title");
SharedPreferences mSharedPreferences =getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE);
/*
* TO WRITE VALUES INTO THE PREFERENCE FILE
*/
//get Editor object using sharedPreference edit() Method
Editor mEditor= mSharedPreferences.edit();
//set boolean Values into preference files
mEditor.putString("stringValue", "hello");
//set integer value into the preference file
mEditor.commit();
/*
* TO READ THE VALUES FROM THE PREFERENCE FILE
*/
//get the String value from preference
String stringValue=mSharedPreferences.getString("stringValue", "default value");