I'm trying to pass List from one Activity to another. As I checked by Logs, in FindAllPairsEasyActivity it puts player_points to the list but I can't read it in ProgressActivity.
What I want is to display a set of points as ListView because there are few games.
FindAllPairsEasyActivity.java (only two methods):
public class FindAllPairsEasyActivity extends AppCompatActivity {
ArrayList<Integer> findAllPairsList = new ArrayList<>();
...
public void saveScore() {
//Log.i("info","saveScore w find all pairs easy");
SharedPreferences preferences = this.getSharedPreferences(usernameView.getText().toString(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
int totalScore = preferences.getInt("totalScore",0);
totalScore = totalScore + player_points;
editor.putInt("rts_score",player_points);
findAllPairsList.add(player_points);
//Log.i("d","FindAllPairsEasy findAllPairsList: " + findAllPairsList);
editor.putInt("total_score",totalScore);
editor.commit();
}
public ArrayList<Integer> getList() {
return findAllPairsList;
}
ProgressActivity.java:
public class ProgressActivity extends AppCompatActivity {
public ListView list;
TextView usernameView, myProgressView, totalScoreView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
totalScoreView = findViewById(R.id.totalScoreView);
myProgressView = findViewById(R.id.myProgressView);
//List <Integer> findAllPairsList = findAllPairsEasyActivity.getList();
AssetManager am = getApplicationContext().getAssets();
Typeface logoFont = Typeface.createFromAsset(am, String.format(Locale.ENGLISH, "fonts/%s","FjallaOne-Regular.ttf"));
Typeface myProgressFont = Typeface.createFromAsset(am, String.format(Locale.ENGLISH, "fonts/%s","Montserrat-Regular.ttf"));
myProgressView.setTypeface(logoFont);
myProgressView.setText("My Progress");
list = findViewById(R.id.listView1);
String user = getIntent().getStringExtra("username");
usernameView = findViewById(R.id.username_progress);
usernameView.setVisibility(View.INVISIBLE);
usernameView.setText("" + user);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_progress,R.id.listView1,cars);
SharedPreferences preferences = this.getSharedPreferences(usernameView.getText().toString(), Context.MODE_PRIVATE);
int rts_score = preferences.getInt("rts_score",0);
int mc_score = preferences.getInt("mc_score",0);
int g_score = preferences.getInt("g_score",0);
int ftt_score = preferences.getInt("ftt_score",0);
int total_score = rts_score + mc_score + g_score + ftt_score;
FindAllPairsEasyActivity findAllPairsEasyActivity = new FindAllPairsEasyActivity();
list.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,findAllPairsEasyActivity.getList()));
Log.i("list","findAllPairsList content: " + findAllPairsEasyActivity.getList().size());
totalScoreView.setTypeface(logoFont);
totalScoreView.setText("Total score: " + total_score);
}
}
Thank you in advance!
Is it really necessary to save data in SharedPreference
, if the motive is just sending data?
Why not just use putExtra
to send data and use getSerializableExtra
to get data?
Activity FindAllPairsEasyActivity:
ArrayList<Integer> findAllPairsList = new ArrayList<Integer>();
intent.putExtra("my_list", findAllPairsList);
startActivity(intent);
Activity ProgressActivity:
ArrayList<Integer> findAllPairsList = getIntent().getSerializableExtra("my_list");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.