2 sld22525482 sld22525482 于 2016.09.17 09:48 提问

保存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 = context.getSharedPreferences("user_hotspots", Context.MODE_PRIVATE);
    String ip_input1 = read.getString("edt_ip_input1", "");
    String ip_input2 = read.getString("edt_ip_input2", "");
    String ip_input3 = read.getString("edt_ip_input3", "");
    String ip_input4 = read.getString("edt_ip_input4", "");
    String name = read.getString("edt_name", "");
    String password = read.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.setText(password);
            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;
    }
    WriteUserInfo();
    return convertView;
}

public static void WriteUserInfo() {
    SharedPreferences userdetect = context.getSharedPreferences("user_hotspots", 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() {
    adapter_hotSpotsList.WriteUserInfo();
    super.onPause();
    Log.e("HotSpots", "onPause");
}

1个回答

Marksinoberg
Marksinoberg   Ds   Rxr 2016.09.17 10:11
 editor.putString("edt_ip_input1", edt_input_1.getText().toString());

先debug一下你的这个getText能否正确的获的数据。

Marksinoberg
Marksinoberg 看来真的是没有捕获呵。应该是你底层的有些代码写的不规范引起的。
大约一年之前 回复
sld22525482
sld22525482 是什么原因导致的没有获取到呢?
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片