Android notifyDataSetChanged仅在刷新活动后才有效

时间:2014-08-04 07:07:22

标签: android sharedpreferences notifydatasetchanged

我的应用程序中的

notifyDataSetChanged仅在刷新活动后工作(从中退出并再次打开)。

我不知道为什么,我尝试了几种方法,但我没有取得成功。

我的代码是:

protected void onActivityResult(int requestCode, int resultCode, Intent i) {
    SharedPreferences pref = getSharedPreferences(Questions, MODE_PRIVATE);

    int id = i.getExtras().getInt("id"); // get id
    if(pref.getBoolean("answered"+id, true)){ // if item answered
        imgAdapter.notifyDataSetChanged(); // change if answered - works only after refreshing
        completed = (TextView) findViewById(R.id.Correct);
        Correct= pref.getInt("Correct", 0); 
        completed.setText("Finished: " + Correct + "/5"); // Works fine

        }

}

添加" onActivityResult(位置,位置,意图)后发生这种情况;" (在添加它之前,即使活动已被刷新,它也不起作用)

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent intent = new Intent(Question.this, Answer.class);
                intent.putExtra("id", position);
                onActivityResult(position, position, intent); // to pass id
                startActivityForResult(intent, position);
}

适配器

public class ImgAdapter extends BaseAdapter {
    private Context mContext;
    private ColorMatrixColorFilter cf;


    public ImgAdapter(Context c) {
        mContext = c;
        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(0);
        cf = new ColorMatrixColorFilter(matrix);
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        imageView = (ImageView) convertView;
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setAdjustViewBounds(true);
        imageView.setImageResource(mThumbIds[position]);

        if(Question.items.get(position).answered){
            imageView.setColorFilter(cf);
            imageView.setAlpha(175);
        }

        return imageView;
    }

    public static Integer[] mThumbIds = 
    {
        R.drawable.question1,
        R.drawable.question2,
        R.drawable.question3,
        R.drawable.question4,
        R.drawable.question5,
    };

}

2 个答案:

答案 0 :(得分:0)

您应该将onActivityResult()方法中的代码移动到onItemClick()方法中。此外,您需要确保在调用Question.items之前更新您的数据集(例如notifyDataSetChanged()所代表的任何内容)。

答案 1 :(得分:0)

试试这个:

public class ImgAdapter extends BaseAdapter {
    private Context mContext;
    private ColorMatrixColorFilter cf;
    private int answered=-1;

    public ImgAdapter(Context c) {
        mContext = c;
        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(0);
        cf = new ColorMatrixColorFilter(matrix);
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        imageView = (ImageView) convertView;
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setAdjustViewBounds(true);
        imageView.setImageResource(mThumbIds[position]);

        if(position==answered){
            imageView.setColorFilter(cf);
            imageView.setAlpha(175);
        }

        return imageView;
    }

    public static Integer[] mThumbIds = 
    {
        R.drawable.question1,
        R.drawable.question2,
        R.drawable.question3,
        R.drawable.question4,
        R.drawable.question5,
    };
    public void setAnswered(int answered){
    this.answered=answered; 
   }
}

protected void onActivityResult(int requestCode, int resultCode, Intent i) {
    SharedPreferences pref = getSharedPreferences(Questions, MODE_PRIVATE);

    int id = i.getExtras().getInt("id"); // get id
    if(pref.getBoolean("answered"+id, true)){ // if item answered

        completed = (TextView) findViewById(R.id.Correct);
        Correct= pref.getInt("Correct", 0); 
        imgAdapter.setAnswered(Correct);
        imgAdapter.notifyDataSetChanged(); // change if answered - works only after refreshing
        completed.setText("Finished: " + Correct + "/5"); // Works fine

        }

}