Android : AlertDialog didn't show up

on Sunday, October 26, 2014


I create a class that extend base adapter, this adapter will be shown in a fragment. The program run smoothly, but when i clicked an item, it have to show an alertdialog. but the alertdialog didn't show up until i swap to other fragment. This is the class that contain my alertdialog code.



public class GridAdapter extends BaseAdapter {
private Activity _activity;
public static ArrayList<String> _filePaths = new ArrayList<String>();
private ArrayList<String> _imgPaths = new ArrayList<String>();
private int imageWidth, imageHeight;
public static ImageButton imageView;
private PelitaViewer myPage;

public GridAdapter(Activity activity, ArrayList<String> filePaths, ArrayList<String> imgPaths, int imageWidth, int imageHeight) {
this._activity = activity;
this._filePaths = filePaths;
this._imgPaths = imgPaths;
this.imageWidth = imageWidth;
this.imageHeight = imageHeight;

}

@Override
public int getCount(){
return this._filePaths.size();
}

@Override
public Object getItem(int position){
return this._filePaths.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null){
imageView = new ImageButton(_activity);
} else {
imageView = (ImageButton) convertView;
}

Bitmap image = decodeFile(_imgPaths.get(position), imageWidth, imageHeight);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageHeight));
imageView.setImageBitmap(image);
imageView.setOnClickListener(new OnImageClickListener(position));
return imageView;
}
public static Bitmap decodeFile(String filePath, int WIDTH, int HEIGHT){
try{
File f = new File(filePath);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_WIDTH = WIDTH;
final int REQUIRED_HEIGHT = HEIGHT;
int scale = 1;
while(((o.outWidth/scale/2) > REQUIRED_WIDTH) && ((o.outHeight/scale/2) > REQUIRED_HEIGHT)){
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
}catch (FileNotFoundException e){
e.printStackTrace();
}
return null;
}

class OnImageClickListener implements OnClickListener{

int _postion;

public OnImageClickListener(int position) {
this._postion = position;
}

@Override
public void onClick(View v) {

AlertDialog.Builder alert = new AlertDialog.Builder(_activity);
alert.setTitle("Action!");
alert.setMessage("What do you want to do?");
alert.setPositiveButton("OK", null);

alert.setNegativeButton("Cancel", null);

alert.show();
}
}

}

0 comments:

Post a Comment