【发布时间】: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