2 sld22525482 sld22525482 于 2016.09.18 13:18 提问

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

保存listview 的 item 中的edittext的值 SharedPreferences方法的使用问题 遇到一个麻烦求解决

在adapter里面定义了静态的函数保存edittext然后再listview所在的Fragment的onPause中调用,切换fragment发现并没有读取到保存的数据,求大神帮忙解决,看一下是哪里除了问题啊。。

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<String> 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.putString("edt_name", edt_hotspots_name.getText().toString());
    editor.putString("edt_password", edt_hotspots_password.getText().toString());*/
    editor.commit();
}

public static void WriteUserNameInfo() {
    SharedPreferences userdetect = context.getSharedPreferences("hotspots_user_name", 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.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_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.putString("edt_name", edt_hotspots_name.getText().toString());
    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的值呢?

2个回答

woshiwangbiao
woshiwangbiao   2016.09.18 14:05

你的写入方法都写在onPause()方法里,都还没存怎么可能先读取到呢?

sld22525482
sld22525482 执行onPause的时候存啊,第二次打开程序就取出来了不是这样吗。。
大约一年之前 回复
sld22525482
sld22525482   2016.09.18 14:25

执行onPause的时候存啊,第二次打开程序就取出来了不是这样吗。。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!