如何在返回上一个活动时保存edittext

时间:2014-03-19 06:27:33

标签: android listview

可能是我的问题错了。但是当我点击列表视图中的项目时,我打开其他活动来编辑项目名称文本,之后我回来按它保存但没有重命名为listview.so请帮助我。

listview类:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_view);

    listView=(ListView)findViewById(R.id.listView);
    listView.invalidateViews();
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

    final List<String> s = new ArrayList<String>();
    for(BluetoothDevice bt : pairedDevices)
       s.add(bt.getName());
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, s));
    //listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    //listView = getListView();
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt,
                long paramLong) {
       // TextView select_text=(TextView)viewClicked.findViewById(R.id.select_text);

            String name=s.get(paramInt);
            Intent i = new Intent(SettingOptionActivity.this, EditPreview.class);     
            i.putExtra("name", name);
            startActivityForResult(i,0);

            //Log.v("____string name________", "__________"+name);
            //loadSavedPreferences(s.get(paramInt));
    }              

    }); 

}

以下类是编辑项目名称

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_layout);
    edittext=(EditText)findViewById(R.id.device_text);

  //  edittext.setText(val);    
    loadSavedPreferences();

}
private void loadSavedPreferences() {
    /*SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
*/   Intent i = getIntent();
       String val = "";

       if(i.hasExtra("name"))
             val = i.getStringExtra("name");

    edittext.setText(val);
    Log.v("___________edittext", "_______________"+edittext.getText());
    }
    private void savePreferences(String key, String value) {
    //SharedPreferences sharedPreferences = PreferenceManager
      //      .getDefaultSharedPreferences(this);
    SharedPreferences   sharedPreferences=this.getSharedPreferences(
                  "com.example.app", Context.MODE_PRIVATE);
    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
     Log.v("__________key", "_______________"+key);
     Log.v("___________value", "_______________"+value);
    //edittext.setText(editor.toString());
     Intent intent=new Intent();
        intent.putExtra("name", edittext.getText().toString());
        setResult(RESULT_OK, intent);
        finish();
    editor.commit();
    }
    public void saveData(){
    savePreferences("name", edittext.getText().toString());
     Log.v("___________edittext", "_______________"+edittext.getText().toString());
    }
    @Override
    public void onBackPressed(){
        saveData();

    super.onBackPressed();
    }   

所以请检查并帮助我。

3 个答案:

答案 0 :(得分:0)

您可以尝试其他方式,而不是SharedPreference。

//首先保存文字

@Override 
public void onSaveInstanceState( Bundle outState )
{
      super.onSaveInstanceState( outState );
      outState.putInt( "textValue", editText.getText().toString() );
}

//加载文字

@Override 
public void onCreate ( Bundle savedInstanceState )
{
      ....

      if ( savedInstanceState != null )
      {
           editText.setText ( savedInstanceState.getString ( "textValue" ) );
      }

      ....
}

答案 1 :(得分:0)

我不太确定但是你可以做你的

final List<String> s = new ArrayList<String>();

=> public static List<String> s; (Before the constructor)

和你的ListView活动onCreate()

s = new ArrayList<String>();

在EditName活动中,您可以修改ListView.s。 -listitems name属性,然后在你调用的ListView的onResume方法上:

编辑2:

在onCreate之前:

private ArrayAdapter<String> aa;

on onCreate:

this.aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, s));
listView.setAdapter(this.aa);

在onResume:

this.aa.notifyDataSetChanged();

答案 2 :(得分:0)

在listview类中尝试:

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int   paramInt,
            long paramLong) {
   // TextView select_text=(TextView)viewClicked.findViewById(R.id.select_text);

        String name=s.get(paramInt);
        Intent i = new Intent(SettingOptionActivity.this, EditPreview.class);     
        i.putExtra("name", name);
        i.putExtra("position", paramInt);
        startActivityForResult(i,0);

        //Log.v("____string name________", "__________"+name);
        //loadSavedPreferences(s.get(paramInt));
}              

}); 

并实现回调方法:

onActivityResult(int requestCode, int resultCode, Intent data) {
     if(requestCode == 0 && resultCode == RESULT_OK) {
        int position = data.getIntExtra("position", 0);
        String newName = data.getIntExtra("name");
        s.get(0).replace(oldname, newName );
        yourAdapterObject.notifyDatasetChanged();
     }

}

并以你的名义编辑类传递位置,就像你从意图中获得的那样。