简体   繁体   中英

AsyncTask to update adapter UI

im pretty new to Android and AsyncTask and was hoping you could help...Im trying to refresh an ImageView adapter so it updates on the UI... i was told to use notifyDataSetChanged() but i just can get it to work...i have setup an asynctask but the only result i get is a NullPointerException ...i need the update to happen when a new element is added to my nextColorArray ....Please look at my code, i seriously dont even know if im using my asynctask in the right manner or if im even close?!..cheers

public class GameScreen extends Activity{
int yellow = 0xffffff66;
int green = 0xff00EE76;
int red = 0xffff4342;
int blue = 0xff42c3ff;
int purple = 0xff9932CC;
int white = 0xFFFFFFFF;

int total_Count = 0;
int colorPosition = 0;//nextColor position
int colorPickerStart = 0;
ArrayList<Integer>nextColorArray;
ImageView imageView;
ImageAdapter ia;  
private static final String TAG = GameScreen.class.getSimpleName();//log

ArrayList<Integer>colorPicker = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_screen);
    new upDateNextColor().execute();
}

class upDateNextColor extends AsyncTask<Void, Void, Void> {

    GridView gridview2;

    @Override
    protected Void doInBackground(Void... voids) {
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        nextColorArray.add(blue);
        nextColorArray.add(green);
        nextColorArray.add(red);
        nextColorArray.add(yellow);
        nextColorArray.add(purple);
        Collections.shuffle(nextColorArray);
    }

    @Override
    protected void onPreExecute() {
        nextColorArray = new ArrayList<Integer>(10);
        gridview2 = (GridView) findViewById(R.id.colorNext);
        ia = new ImageAdapter(nextColorArray);
        gridview2.setAdapter(ia);
        if(total_count > 10){
        nextColorArray.add(0, white);
        ia.notifyDataSetChanged();
        }           
    }
 }

 public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList aL;

    public ImageAdapter(Context c, ArrayList<Integer>aL) {
        mContext = c;
        this.aL = aL;
    }

    public ImageAdapter(ArrayList<Integer>aL) {
        this.aL = aL;
    }

    public ImageAdapter(Context c){
        mContext = c;
    }

    public int getCount() {

        return 10;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
            imageView.setBackgroundColor(nextColorArray.get(colorPosition));
            if(colorPosition < 9) colorPosition++;
        } else {
            imageView = (ImageView) convertView;
        }
        return imageView;
    }
}

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException
at android.view.ViewConfiguration.get(ViewConfiguration.java:332)
at android.view.View.<init>(View.java:3254)
at android.widget.ImageView.<init>(ImageView.java:105)

you are getting Null Pointer Exception because :

1. mContext is null in ImageAdapter becuase you are using Single parameter Constructor of ImageAdapter for creating object. so pass Activity Context also as:

ia = new ImageAdapter(GameScreen.this,nextColorArray);

2. use

gridview2 = (GridView)GameScreen.this. findViewById(R.id.colorNext);

for initializing GridView in onPreExecute

Edit: I made these changes in the code.

     return aL.size();   // change this in getCount()
     imageView.setBackgroundColor(nextColorArray.get(position));

FullCode

public class MainActivity extends Activity{
int yellow = 0xffffff66;
int green = 0xff00EE76;
int red = 0xffff4342;
int blue = 0xff42c3ff;
int purple = 0xff9932CC;
int white = 0xFFFFFFFF;

int total_Count = 0;
int colorPosition = 0;//nextColor position
int colorPickerStart = 0;
ArrayList<Integer>nextColorArray;
ImageView imageView;
ImageAdapter ia;  
private static final String TAG = MainActivity.class.getSimpleName();//log

ArrayList<Integer>colorPicker = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new upDateNextColor().execute();
}

class upDateNextColor extends AsyncTask<Void, Void, Void> {

GridView gridview2;

@Override
protected Void doInBackground(Void... voids) {
    return null;
}
@Override
protected void onPostExecute(Void result) {
    nextColorArray.add(blue);
    nextColorArray.add(green);
    nextColorArray.add(red);
    nextColorArray.add(yellow);
    nextColorArray.add(purple);
    Collections.shuffle(nextColorArray);
}

@Override
protected void onPreExecute() {
    nextColorArray = new ArrayList<Integer>(10);
    gridview2 = (GridView) findViewById(R.id.gridview);
    ia = new ImageAdapter(nextColorArray);
    gridview2.setAdapter(ia);
    nextColorArray.add(0, white);
    ia.notifyDataSetChanged();          
  }
}

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList aL;

public ImageAdapter(Context c, ArrayList<Integer>aL) {
    mContext = c;
    this.aL = aL;
}

public ImageAdapter(ArrayList<Integer>aL) {
    this.aL = aL;
}

public ImageAdapter(Context c){
    mContext = c;
}

public int getCount() {
   //int a = 10;
    return aL.size();
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        imageView = new ImageView(MainActivity.this);
        imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
        imageView.setBackgroundColor(nextColorArray.get(position));
        if(colorPosition < 9) colorPosition++;
    } else {
        imageView = (ImageView) convertView;
    }
    return imageView;
 }
}
}

Snap shot

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM