Android 如何从数组中调用旧对象?
我将我的动态数据存储在一个动态字符串数组(HoleScore)中,该数组位于一个活动中,该活动是一个记分板,每次都可以最小化并打开。但是,当我从另一个活动返回到此活动时,我无法访问我以前存储的第一个对象 e、 g 如果我来自孔_计数器=1,那么我可以看到孔孔[1] 如果我来自孔_计数器=2,那么我看不到孔孔[1],我只能看到孔孔[2] 这意味着第一个Android 如何从数组中调用旧对象?,android,arrays,Android,Arrays,我将我的动态数据存储在一个动态字符串数组(HoleScore)中,该数组位于一个活动中,该活动是一个记分板,每次都可以最小化并打开。但是,当我从另一个活动返回到此活动时,我无法访问我以前存储的第一个对象 e、 g 如果我来自孔_计数器=1,那么我可以看到孔孔[1] 如果我来自孔_计数器=2,那么我看不到孔孔[1],我只能看到孔孔[2] 这意味着第一个//打印每个孔的数据--不打印任何内容 这是我的代码: package com.example.plusgolf; import android.
//打印每个孔的数据--如果我来自孔_计数器=2,则代码>不打印任何内容
这是我的代码:
package com.example.plusgolf;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class ScoreBoard extends Activity
{
TextView hole_score, hole_score2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.scoreboard);
super.onCreate(savedInstanceState);
// Picking Data from OnCourse
Bundle extras = getIntent().getExtras();
// Get Stroke & Par Number from OnCourse
int stroke = extras.getInt("stroke");
int par_number = extras.getInt("par_number");
// Calling Core
final Core calling_core = (Core) getApplicationContext();
// Get hole_counter from Core
final int hole_counter = calling_core.getHoleCounter();
// Get Holes from Core (Total Holes of the Game)
final int holes = calling_core.getHoles();
// Creating Array of String to Store Data of Each Hole
String[] HoleScore = new String[holes];
// Storing Hole Data in Array
HoleScore[hole_counter] = "Hole "+ hole_counter + " = " + stroke + " / " + par_number;
// Printing Data of each Hole -- !!!!!
hole_score = (TextView) findViewById(R.id.hole_number1);
hole_score.setText(HoleScore[1]);
// Printing Data of each Hole
hole_score2 = (TextView) findViewById(R.id.hole_number2);
hole_score2.setText(HoleScore[2]);
}
}
我刚刚找到了一种更好的方法来存储这些通过活动传递的数据:共享偏好。
比每次打开第二个活动时发送捆绑包要好得多
因此,我真的建议您使用共享首选项来获取pass数据,因为您将使用一次以上的活动
第一项活动:
// Starting Shared Preferences
public static String filename = "MySharedData";
SharedPreferences mySharedData = getSharedPreferences(filename, 0);
// Editor
SharedPreferences.Editor editor = mySharedData.edit();
// Values to Send
editor.putInt("hole_" + hole_counter + "_stroke", stroke_counter);
editor.putInt("hole_" + hole_counter + "_par" , par_number);
// Done Signal
editor.commit();
// Starting Shared Preferences
public static String filename = "MySharedData";
SharedPreferences mySharedData = getSharedPreferences(filename, 0);
// Getting All Data Stored from First Activity
for (int i = 0; i <= length.that.you.need; i++)
{
strokeArray[i] = mySharedData.getInt("hole_" + i + "_stroke", 0);
parArray[i] = mySharedData.getInt("hole_" + i + "_par", 0);
}
第二项活动:
// Starting Shared Preferences
public static String filename = "MySharedData";
SharedPreferences mySharedData = getSharedPreferences(filename, 0);
// Getting All Data Stored from First Activity
for (int i = 0; i <= length.that.you.need; i++)
{
strokeArray[i] = mySharedData.getInt("hole_" + i + "_stroke", 0);
parArray[i] = mySharedData.getInt("hole_" + i + "_par", 0);
}
//启动共享首选项
公共静态字符串filename=“MySharedData”;
SharedReferences mySharedData=GetSharedReferences(文件名,0);
//获取从第一个活动存储的所有数据
对于(int i=0;i),看起来您只是为单个数据点设置HoleScore[hole_counter],而不是所有数据点……HoleScore[hole_counter]=“hole”+hole_counter+“=”+stroke+“/”+par_number;@AdiInbar我应该为每个变量创建一个数组,如:hole_counter、stroke和par_number吗?我想你的意思是将其发送给@Guardanis。我从第一篇博文评论队列编辑了博文;Android/Java不是我的专业领域。@Guardanis我应该为每个变量创建一个数组,如:hole_counter、stroke和par_number吗?不,您只需要一个数组,但不是用所有的值填充它。您只是用选定的值填充它,而不是像您尝试的那样用所有值填充它。