【问题标题】:why am i unable to use pass data through my inflated layout by using share preferences?为什么我无法使用共享首选项通过我的膨胀布局传递数据?
【发布时间】:2015-07-23 11:18:40
【问题描述】:

我有一个关于使用意图/共享首选项将数据从一个类传递到另一个类的问题。

我增加了一个计数器,使用共享首选项将总数存储在应用程序内存中。现在总计数存储得非常好,但是当我将这些数据传递给我的主要活动的膨胀布局时,数据会丢失吗?

public class GetStartedMain{…….
public int videoCount(int totalVid){
    Toast.makeText(GetStartedMain.this, "Video was added     "+totalVid, Toast.LENGTH_SHORT).show();

    SharedPreferences preferences = this.getSharedPreferences("MyPreferences",0);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt("VAR1", totalVid);
    editor.commit();
    int j = preferences.getInt("VAR1", 0);
    Log.e(null, "counter message @ @ @ : " + j);

    return totalVid;
} }
//then pass to ProfileVideo class

public class ProfileVideo{
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_vid_activity);
SharedPreferences preferences = ctx.getSharedPreferences("MyPreferences", 0);

int counter = preferences.getInt("VAR1", 0); //no id: default value

Log.e(null, "counter message @ @ @ @ @ ! !: " + counter);
}

我还使用了 android 意图方法,我试图将信息传递给膨胀的布局类。这是一个失败,因为意图需要启动一个新活动,我希望将数据传递给我的主要活动的膨胀布局。

public class ProfileMain{……….
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_activity);
SharedPreferences preferences = this.getSharedPreferences("MyPreferences", 0);

 counter = preferences.getInt("VAR1", 0); //no id: default value

Log.e(null, "counter message @ @ @ @ @: " + counter);
}
  private void buttonClickVid() {

        Intent intent = new Intent(this, ProfileVideo.class);
        intent.putExtra("vcount", counter);
startActivity(intent);
            mainLayer.removeAllViews();

        //showVidButton.setOnClickListener(null);
        mainLayer.addView(vidDisplay);


          //  showNoteButton.setOnClickListener(flagButton);
        }


    private void buttonClickNote() {
        mainLayer.removeAllViews();

        //showNoteButton.setOnClickListener(null);
        mainLayer.addView(noteDisplay);
        }

         // mainLayer.removeAllViews();


    @Override

    public void onClick(View v) {
        if (v == showVidButton) {

           // buttonClickVid();
            buttonClickVid();

        }
        if (v == showNoteButton) {

            buttonClickNote();

        }

    }

—> pass to the ProfileVideo class
public class ProfileVideo extends Activity {
    TextView vidCountMain;
    int vc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_vid_activity);

        Intent intent = getIntent();
      vc = intent.getIntExtra("vcount", 0);
        Log.e(null, "counter message @ @ @ @ @ ? vc : " + vc);

 vidCountMain=(TextView) findViewById(R.id.vid_total);
vidCountMain.setText(String.valueOf(counter));
Toast.makeText(ProfileVideo.this, "WOOOOOW   " + counter, Toast.LENGTH_LONG).show();

}

我正在研究并发现一篇文章说这是因为来自主要活动的签名冲突以及在主要活动之上分层的膨胀布局。这是真的?如果是这样,我将如何解决这个问题?

目前这是应用程序所做的 http://imgur.com/a/89id0 --用户点击视频 --视频活动显示在新活动中

但这就是我想要它做的 http://i.imgur.com/XW6lewp.png --用户点击视频 --视频活动通过视频计数结果膨胀到主要活动中

请帮忙!

【问题讨论】:

    标签: android android-layout android-intent android-activity sharedpreferences


    【解决方案1】:

    只需创建一个具有 SharedPreferences 实例的新类,并将应用程序上下文传递给它的构造函数:

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    
    
    public class ExamplePreferences {
    
        private SharedPreferences instancePreferences;
        private Context mContext;
    
        public TemperaturePreferences(Context context) {
            mContext = context;
            instancePreferences= PreferenceManager.getDefaultSharedPreferences(mContext);
    
        }
    //Use methods to do what you need for example:
       public void putNewPreference(String key, String value) {
    
    
          SharedPreferences.Editor editor = degreesPreferences.edit();
          editor.putString(key, value);
          editor.apply();
       }
    
    }
    

    然后在您可能需要的任何活动中实例化该类并使用它。请告诉我这是否有效?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 2020-10-04
      • 2023-03-20
      • 2019-03-08
      相关资源
      最近更新 更多