我只是从原来的帖子中编辑了这篇帖子,因为我可以缩小问题的范围,但不想为此开篇额外的帖子。
我想要的 我有一个活动,其中我将房屋中的数据生成到数组中。然后在打开另一个活动时,应该使用数据来显示它。
我的问题 数据已正确生成,我可以在日志中看到要访问它之前生成的数据。在访问时,它仍然无法显示我的数据。
我的代码
Houses.java(我在其中生成房屋并使用下面已经给出的答案来获取我的数据)
$container
->getDefinition('scheb_two_factor.security.google_authenticator')
->setPublic(true);
BuyHousesActivity.java(此处应访问并显示数据)
public class Houses{
int[][] NewHouseProps = new int[20][10];
int[][] HouseProps;
public Houses(){
}
public ArrayList<Integer> getClasses()
{
ArrayList<Integer> classes = new ArrayList<>();
Log.d("Test","ID: "+3+" CLASS: "+NewHouseProps[3][0]);
Log.d("Test","ID: "+4+" CLASS: "+NewHouseProps[4][0]);
for(int id=0 ; id<=8; id++) {
classes.add(NewHouseProps[id][0]);
Log.d("getClasses","Classes: "+NewHouseProps[id][0]);
Log.d("getClasses!!!!!!!!!", "List: "+classes.get(id));
}
return classes;
}
public void GenerateHouses(){
Log.d("Generate Houses","!!");
for(int id=0; id<=8; id++){
Random r = new Random();
int h_class, garage = 0, rooms = 0, furn = 0, balkon = 0, pool = 0;
h_class = r.nextInt(4); //Klasse
if(h_class == 0){
rooms = r.nextInt(4)+1;
balkon = r.nextInt(1);
}else if(h_class == 1){
rooms = r.nextInt(3)+3;
garage = r.nextInt(1)+1;
pool = r.nextInt(1);
balkon = r.nextInt(2);
}else if(h_class == 2){
rooms = r.nextInt(3)+2;
garage = r.nextInt(2)+2;
pool = r.nextInt(1);
}else if(h_class == 3){
rooms = r.nextInt(8)+6;
garage = r.nextInt(6)+4;
pool = r.nextInt(1);
balkon = r.nextInt(3);
}
furn = r.nextInt(1);
int price = GenerateHousePrice(id, h_class, garage, rooms, furn, balkon, pool);
NewHouseProps[id][0] = h_class;
NewHouseProps[id][1] = price;
NewHouseProps[id][2] = rooms;
NewHouseProps[id][3] = furn;
NewHouseProps[id][4] = balkon;
NewHouseProps[id][5] = pool;
NewHouseProps[id][6] = garage;
Log.d("ID: ", Integer.toString(id));
Log.d("CLASS: ",Integer.toString(NewHouseProps[id][0]));
Log.d("PRICE: ", Integer.toString(NewHouseProps[id][1]));
}
}
public int GenerateHousePrice(int id, int h_class, int garage, int rooms, int furn, int balkon, int pool){
int price = 0, pricep = 0;
Random r = new Random();
if(h_class == 0){
price = r.nextInt(150000)+50000; // 50 - 200 tausend
price += rooms * 7500;
}
else if(h_class == 1){
price = r.nextInt(165000)+85000; //85 - 250 tausend
price += rooms * 12500;
}
else if(h_class == 2){
price = r.nextInt(300000)+100000; //100 - 400 tausnd
price += rooms * 20000;
}
else if(h_class == 3){
price = r.nextInt(800000)+500000; // 500 - 1.3 Mill.
price += rooms * 15000;
}
if(garage > 0){
pricep += price*0.20;
pricep += garage * 500;
}
if(furn == 1){
pricep += price*0.25;
}
if(balkon > 0){
pricep += price*0.1;
pricep += balkon*500;
}
if(pool == 1){
pricep += 20000;
}
price += pricep;
return price;
}
我的日志 这些是我的日志的结果,它们与您在上面看到的位置相同: Generating Accessing
希望您能帮助我,告诉我我做错了什么和/或忘记了什么。
答案 0 :(得分:0)
发生这种情况是因为您使用的是空列表。看。
在您的public static char[][] hashTable = {
"".toCharArray(), "".toCharArray(),
"abc".toCharArray(), "def".toCharArray(),
"ghi".toCharArray(), "jkl".toCharArray(),
"mno".toCharArray(), "pqrs".toCharArray(),
"tuv".toCharArray(), "wxyz".toCharArray()
};
中,您致电:
FragmentBuyHouse
但是,当您实例化BuyHousesActivity bha = new BuyHousesActivity();
List<Integer> classes = bha.classes;
时,它将初始化不包含任何数据的数组。
看你的班级new BuyHouseActivity()
BuyHouseActivity
在实现中,您在方法List<Integer> classes = new ArrayList<>();
中填充数组classes
,但在安装该类时未调用该方法。这是因为在第46行出现错误。
您需要像使用Bundle一样通过参数传递数据。
解决方案2
您可以创建一个静态方法来返回类:
更改您的onCreate()
:
BuyHousesActivity
您想获得像public class BuyHousesActivity extends FragmentActivity {
List<Integer> classes = new ArrayList<>();
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Houses h = new Houses(); //Activity where my Houses get generated
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buy_houses);
Log.d("1. Step", "!!!!!!!!!!!!!!!!!!!!!!");
// for(int id=0; id<=8;id++){
// classes.add(h.NewHouseProps[id][0]);
// Log.d("Klasse: ", Integer.toString(h.NewHouseProps[id][0]));
// }
// Log.d("2. Step", "!!!!!!!!!!!!!!!!!!!!!!");
/*
classes.add(R.drawable.apartment); //Class 0
classes.add(R.drawable.townhouse); //Class 1
classes.add(R.drawable.bungalow); //Class 2
classes.add(R.drawable.villa); //Class 3
*/
mPager = findViewById(R.id.vpPager);
mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), classes);
mPager.setAdapter(mPagerAdapter);
}
public static ArrayList<Integer> getClasses()
{
Houses h = new Houses();
classes = new ArrayList<>();
for(int id=0 ; id<=8; id++) {
classes.add(h.NewHouseProps[id][0]);
}
return classes;
}
@Override
public void onBackPressed() {
//ViewPager Last Page or Back
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
FragmentBuyHouse