如何將此活動轉換爲片段?我已經轉換某些部分,但我得到的錯誤,這是我的實際代碼,任何人都可以提供幫助指正,我可以將活動轉換爲片段嗎?
public class GalleryActivity extends Fragment {
public Cursor cursor;
/*
* Column index for the Thumbnails Image IDs.
*/
public int columnIndex;
String imagePath;
MenuItem mSelectButton;
int mSelectSwitch;
private static final int MIN_DISTANCE = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOCITY = 100;
private GestureDetector mGestureDetector;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_gallery, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] projection = {MediaStore.Images.Thumbnails._ID};
//managedQuery causes an error
cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,projection,null,null,
MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
//findViewById causes an error
GridView GalleryImages = (GridView) findViewById(R.id.galleryGrid1);
GalleryImages.setAdapter(new GalleryAdapter(this));
// new GalleryAdapter(this) causes an error
GalleryImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
mSelectButton.setVisible(true);
String[] imgData = {MediaStore.Images.Media.DATA};
cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,imgData, null,
null,
null);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
imagePath = cursor.getString(columnIndex);
}
});
}
private class GalleryAdapter extends BaseAdapter {
private Context context;
public GalleryAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setScaleType(ImageView.ScaleType.CENTER_CROP);
picturesView.setLayoutParams(new GridView.LayoutParams(470,470));
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
我們需要看到你所得到的錯誤。請發佈Logcat –
編譯器上的錯誤不在logcat上, 這是在managedQuery上: 未定義類型new AdapterView.OnItemClickListener()的方法managedQuery(Uri,String [],null,null,null){ } –