【问题标题】:variable didnt change in android acticityandroid活动中的变量没有变化
【发布时间】:2013-12-08 02:49:05
【问题描述】:

我在 android 项目中的活动有此代码(我已对其进行了简化):

公共类收藏夹扩展了 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 方法中运行)。 我该怎么做才能改变?

完整代码:

公共类收藏夹扩展了 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);  

}

【问题讨论】:

    标签: android


    【解决方案1】:

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

    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);
     }
    

    【讨论】:

    • 代码很复杂,我无法更改该行的位置(其他一些代码会损坏)。
    • 这条线是不是像我想的那样超出了任何方法?如果是这样,它onCreate() 中的任何内容之前运行。如果您需要有关如何修复它的帮助,您需要解释为什么其他代码“将被损坏”。
    • 感谢您的帮助。我写了完整的代码(post的更新),你可以看到它的结构。
    • 如果你像我上面所说的那样声明和初始化mImageIds,会发生什么?
    • 您的建议是正确的,它有效并且没有问题(我测试过)。谢谢codeMagic。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多