2 sld22525482 sld22525482 于 2016.09.19 15:00 提问

用SharedPreferences方法 保存edittext的值 使用时遇到一个麻烦求解决

public class HotSpotsListAdapter extends BaseAdapter {
public static Context context;
List list;
public static EditText edt_input_1, edt_input_2, edt_input_3, edt_input_4;
public static EditText edt_hotspots_name, edt_hotspots_password;
public HotSpotsListAdapter(Context context, List list) {
this.context = context;
this.list = list;

}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.network_hotspots_list_item, null);
TextView text_left = (TextView) convertView.findViewById(R.id.hotspots_list_left_text);
RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.hotspots_item_content);
relativeLayout.setBackgroundColor(Color.parseColor("#a7ffffff"));
text_left.setText(list.get(position).toString());
if (HotSpotsFragment.list_selected_position == position) {
relativeLayout.setBackgroundColor(Color.WHITE);
}
TextView textView = (TextView) convertView.findViewById(R.id.hotspots_list_text);
edt_hotspots_name = (EditText) convertView.findViewById(R.id.name_edit);
edt_hotspots_password = (EditText) convertView.findViewById(R.id.password_edit);
TextView point1 = (TextView) convertView.findViewById(R.id.point_1);
TextView point2 = (TextView) convertView.findViewById(R.id.point_2);
TextView point3 = (TextView) convertView.findViewById(R.id.point_3);
edt_input_1 = (EditText) convertView.findViewById(R.id.ip_input_1);
edt_input_2 = (EditText) convertView.findViewById(R.id.ip_input_2);
edt_input_3 = (EditText) convertView.findViewById(R.id.ip_input_3);
edt_input_4 = (EditText) convertView.findViewById(R.id.ip_input_4);

SharedPreferences read_name = context.getSharedPreferences("hotspots_user_name", Context.MODE_PRIVATE);
SharedPreferences read_edit = context.getSharedPreferences("hotspots_user_edit", Context.MODE_PRIVATE);
SharedPreferences read_password = context.getSharedPreferences("hotspots_user_password", Context.MODE_PRIVATE);
String ip_input1 = read_edit.getString("edt_ip_input1", "");
String ip_input2 = read_edit.getString("edt_ip_input2", "");
String ip_input3 = read_edit.getString("edt_ip_input3", "");
String ip_input4 = read_edit.getString("edt_ip_input4", "");
String name = read_name.getString("edt_name", "");
String password = read_password.getString("edt_password", "");
switch (position) {
    case 0:
        edt_input_1.setVisibility(View.GONE);
        edt_input_2.setVisibility(View.GONE);
        edt_input_3.setVisibility(View.GONE);
        edt_input_4.setVisibility(View.GONE);
        edt_hotspots_name.setText(name);
        edt_hotspots_password.setVisibility(View.GONE);
        break;
    case 1:
        edt_input_1.setText(ip_input1);
        edt_input_2.setText(ip_input2);
        edt_input_3.setText(ip_input3);
        edt_input_4.setText(ip_input4);
        point1.setText(".");
        point2.setText(".");
        point3.setText(".");
        edt_hotspots_name.setVisibility(View.GONE);
        edt_hotspots_password.setVisibility(View.GONE);
        break;
    case 2:
        textView.setText("固定");
        edt_input_1.setVisibility(View.GONE);
        edt_input_2.setVisibility(View.GONE);
        edt_input_3.setVisibility(View.GONE);
        edt_input_4.setVisibility(View.GONE);
        edt_hotspots_password.setVisibility(View.GONE);
        edt_hotspots_name.setVisibility(View.GONE);
        break;
    case 3:
        edt_hotspots_name.setVisibility(View.GONE);
        edt_hotspots_password.setText(password);
        edt_input_1.setVisibility(View.GONE);
        edt_input_2.setVisibility(View.GONE);
        edt_input_3.setVisibility(View.GONE);
        edt_input_4.setVisibility(View.GONE);
        break;
    case 4:
        textView.setText("启用");
        edt_input_1.setVisibility(View.GONE);
        edt_input_2.setVisibility(View.GONE);
        edt_input_3.setVisibility(View.GONE);
        edt_input_4.setVisibility(View.GONE);
        edt_hotspots_password.setVisibility(View.GONE);
        edt_hotspots_name.setVisibility(View.GONE);
        break;
}

return convertView;

}

public static void WriteUserEditInfo() {
SharedPreferences userdetect = context.getSharedPreferences("hotspots_user_edit", 0);
SharedPreferences.Editor editor = userdetect.edit();

editor.putString("edt_ip_input1", edt_input_1.getText().toString());
editor.putString("edt_ip_input2", edt_input_2.getText().toString());
editor.putString("edt_ip_input3", edt_input_2.getText().toString());
editor.putString("edt_ip_input4", edt_input_4.getText().toString());
editor.commit();

}

public static void WriteUserNameInfo() {
SharedPreferences userdetect = context.getSharedPreferences("hotspots_user_name", 0);
SharedPreferences.Editor editor = userdetect.edit();
editor.putString("edt_name", edt_hotspots_name.getText().toString());
/* editor.putString("edt_password", edt_hotspots_password.getText().toString());*/
editor.commit();
}

public static void WriteUserPasswordInfo() {
SharedPreferences userdetect = context.getSharedPreferences("hotspots_user_password", 0);
SharedPreferences.Editor editor = userdetect.edit();

editor.putString("edt_password", edt_hotspots_password.getText().toString());
editor.commit();

}

}

这是adapter里面的代码,然后再fragment中
@Override
public void onPause() {
System.out.println(HotSpotsListAdapter.edt_input_1.getText().toString());
adapter_hotSpotsList.WriteUserEditInfo();
adapter_hotSpotsList.WriteUserNameInfo();
adapter_hotSpotsList.WriteUserPasswordInfo();
super.onPause();
Log.e("HotSpots", "onPause");
}

但是切换fragment执行onPause后再回来数据并没有读取到 应该是没有存进去 想请教一下这里该如何调用?才能正确的存到edittext的值呢?

应该是没有存进去,函数的调用有问题 。。

1个回答

itworkermk
itworkermk   2016.09.28 17:42

你的需求是现存密码 后去密码 对不?
SharedPreferences preferences=getSharedPreferences("user",Context.MODE_PRIVATE);
,Context.MODE_PRIVATE别用0代替
super.onPause();放到onPause() {下面

Csdn user default icon
上传中...
上传图片
插入图片