变量在android活动中没有变化

时间:2013-12-08 02:49:05

标签: android

我在android项目中有我的活动代码(我简化了它):

public class Favorites扩展了Activity实现     AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory {

public static int[] b;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);


    favorites_total_number = 0;
    // some code for change the favorites_total_number value

    b = new int[favorites_total_number];
    // some code for change values of b

 }

Globals global = new Globals(this);
public Integer[] mImageIds = global.build_favorite_list(b,favorites_total_number); 
// in previous line, b is not changed (but in onCreate method, I changed it)

}

我在onCreate方法中更改了b,但是当我使用b(在最后一行)时,它没有改变。 我无法在其他地方为onCreate方法编写代码(必须编写,因为部分代码只在onCreate方法中运行)。 我该怎么做才能改变?

完整代码:

public class Favorites扩展了Activity实现     AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory {

public static int[] b;
public static int favorites_total_number;

// final SharedPreferences shared2 = this.getSharedPreferences("Prefs", Context.MODE_PRIVATE);

@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);

    // show on gallery
    final SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
    favorites_total_number = 0;
    for(int x = 1; x < 401; x = x+1) {  // change for develope
        String each_subject = "subject_" + String.valueOf(x);
        Boolean channel = shared.getBoolean(each_subject, false);
        if(channel){
            favorites_total_number = favorites_total_number + 1;
        }
    }
    b = new int[favorites_total_number];
    int p=-1;
    for(int x = 1; x < 401; x = x+1) {  // change for develope
        String each_subject = "subject_" + String.valueOf(x);
        Boolean channel = shared.getBoolean(each_subject, false);
        if(channel){
            p=p+1;
            b[p] = x;
        }
    }
    // String str = String.valueOf(favorites_total_number);
    String str = String.valueOf(b[1]);
    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
    // --

    setContentView(R.layout.gallery);


    mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
     mSwitcher.setFactory(this);
     mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
             android.R.anim.fade_in));
     mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
             android.R.anim.fade_out));

    Gallery g = (Gallery) findViewById(R.id.gallery);
     g.setAdapter(new ImageAdapter(this));
     g.setOnItemSelectedListener(this);

 }

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
     mSwitcher.setImageResource(mImageIds[position]);
 }

public void onNothingSelected(AdapterView<?> parent) {
 }

public View makeView() {
     ImageView i = new ImageView(this);
     i.setBackgroundColor(0xFF000000);
     i.setScaleType(ImageView.ScaleType.FIT_CENTER);
     i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
     LayoutParams.MATCH_PARENT));
     return i;
 }

private ImageSwitcher mSwitcher;

public class ImageAdapter extends BaseAdapter {
     public ImageAdapter(Context c) {
         mContext = c;
     }

    public int getCount() {
         return mThumbIds.length;
     }

    public Object getItem(int position) {
         return position;
     }

    public long getItemId(int position) {
         return position;
     }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mThumbIds[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        i.setBackgroundResource(R.drawable.cartoon_1);
        return i;
     }

    private Context mContext;

}

// int[] b_0={1,2,3,5,6};
// int favorites_total_number_0=5;
Globals global = new Globals(this);
private Integer[] all_images = global.favorite_images_0;  
public Integer[] mThumbIds = global.build_favorite_list(all_images,b,favorites_total_number);    
public Integer[] mImageIds = global.build_favorite_list(all_images,b,favorites_total_number);  

}

1 个答案:

答案 0 :(得分:0)

从您发布的代码中,它看起来像这一行

public Integer[] mImageIds = global.build_favorite_list(b,favorites_total_number);

超出onCreate()或任何其他方法,这意味着它将在onCreate()之前运行(您更改b的值。尝试将其移至onCreate()内并看看你是否得到了你想要的东西。

public static int[] b;
public Integer[] mImageIds;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);


    favorites_total_number = 0;
    // some code for change the favorites_total_number value

    b = new int[favorites_total_number];
    // some code for change values of b

   // moved the below lines inside onCreate()
   Globals global = new Globals(this);
   mImageIds = global.build_favorite_list(b,favorites_total_number);
 }