安卓 如何 保存 edittext 的值

在 listview的item中有edittext的值 但是edittext定义在继承BaseAdapter外部类,我想设置edittext后保存他的值程序关闭后下次启动还能读出来,这样就必须把edittext的值存在fragment的onPause方法中,但是直接adapter里的edittext会报错空指针,求大神指点一下如何解决?

3个回答

代码 贴出 ,说清楚点~

sld22525482
「已注销」 代码贴在下面了 大神
大约 3 年之前 回复

用shareperference

chen364567628
微信搜-毕设在线 回复sld22525482: 要指定写入时的操作模式
大约 3 年之前 回复
chen364567628
微信搜-毕设在线 你的写入时getSharedPreferences的第二个参数没穿值,虽然是int但是可选参数只有四个。指定操作模式
大约 3 年之前 回复
sld22525482
「已注销」 回复人生如戏练好演技: 我貌似是存的时候没有存成功 能帮我看一下我的代码吗。。已经贴出来了
大约 3 年之前 回复
chen364567628
微信搜-毕设在线 回复sld22525482:sharedperferences写成全局的,初始化view你就调一下初始化数据啊。
大约 3 年之前 回复
sld22525482
「已注销」 但是我edittext是在getview中获取的呀,要怎么传到fragment里面呢?
大约 3 年之前 回复

/**

  • Created by bmvod on 2016/9/14.
    */
    public class HotSpotsListAdapter extends BaseAdapter {
    Context context;
    List list;
    public EditText edt_input_1, edt_input_2, edt_input_3, edt_input_4;
    public EditText hotspots_name_edit, hotspots_password_edit;

    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);
    hotspots_name_edit = (EditText) convertView.findViewById(R.id.name_edit);
    hotspots_password_edit = (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);
    WriteUserInfo();
    SharedPreferences read = context.getSharedPreferences("user_hotspots_detect", 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);
    hotspots_name_edit.setText(name);
    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(".");
    break;
    case 2:
    textView.setText("固定");
    edt_input_1.setFocusable(false);
    edt_input_2.setFocusable(false);
    edt_input_3.setFocusable(false);
    edt_input_4.setFocusable(false);
    break;
    case 3:

            hotspots_password_edit.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);
            break;
    }
    
    return convertView;
    

    }

    public void WriteUserInfo() {
    SharedPreferences userdetect = context.getSharedPreferences("user_hotspots_detect", 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", hotspots_name_edit.getText().toString());
    editor.putString("edt_password", hotspots_password_edit.getText().toString());
    editor.commit();
    

    }
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!