Hallo ich möchte in meiner App ein Bilder Gallerie einbauen wie die Grid Lists hier in der Android Doc: GridLists
Da ist wohl leider nichts zu erklärt und das Grid View beispiel (ist bei den Grid Lists verlinkt) funktioniert bei mir nicht (heißt es ist einfach nur das leere Fragment zu sehen).
Meine Fragment Klasse für die Menüpunkte im Navigation Drawer:
Und die ImageAdapter Klasse:
Da ist wohl leider nichts zu erklärt und das Grid View beispiel (ist bei den Grid Lists verlinkt) funktioniert bei mir nicht (heißt es ist einfach nur das leere Fragment zu sehen).
Meine Fragment Klasse für die Menüpunkte im Navigation Drawer:
Java:
public class ImageViewFragment extends Fragment {
private int orderBy;
private Vector<Bitmap> thumbs = new Vector<Bitmap>();
// Constructor
public ImageViewFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
orderBy = (Integer) getArguments().get("wall_order");
return inflater.inflate(R.layout.image_layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
GetPreviewTask task = new GetPreviewTask();
task.execute("http://alpha.wallhaven.cc/latest?page=1");
GridView gridview = (GridView) getActivity().findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(getActivity(), thumbs));
switch (orderBy) {
case 0:
view.setBackgroundColor(Color.YELLOW);
break;
case 1:
view.setBackgroundColor(Color.RED);
break;
case 2:
view.setBackgroundColor(Color.GRAY);
default:
break;
}
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
// Do stuff
}
public class GetPreviewTask extends AsyncTask<String, Void, Void> {
private String sourceCode = "";
private Vector<String> IDs = new Vector<String>();
private Vector<Bitmap> thumbNails = new Vector<Bitmap>();
@Override
protected Void doInBackground(String... urls) {
try {
getSourceCode(urls[0]);
getIDsFromSource();
getThumbNails();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Get the source code from Wallhaven
private void getSourceCode(String url) throws IOException {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
InputStream in = null;
try {
in = entity.getContent();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
String line = "";
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null ) {
builder.append(line);
}
sourceCode = builder.toString();
}
// Get all IDs from the source code
private void getIDsFromSource() {
int startIndex = 0;
// find all wallpaper ID's
if(sourceCode.indexOf("http://alpha.wallhaven.cc/wallpapers/thumb/small/", startIndex) == -1)
Log.d("DEBUG", "sourceCode is wrong");
while(sourceCode.indexOf("http://alpha.wallhaven.cc/wallpapers/thumb/small/", startIndex) != -1) {
int currentIndex = sourceCode.indexOf("http://alpha.wallhaven.cc/wallpapers/thumb/small/", startIndex);
int indexID = sourceCode.indexOf("-", currentIndex) + 1;
int endIndexID = sourceCode.indexOf(".", indexID);
// Get the ID
String ID = sourceCode.substring(indexID, endIndexID);
IDs.add(ID);
// Display the ID for debugging
Log.d("ID CHECK", ID);
// Set new start Index
startIndex = endIndexID;
}
}
// Download the preview thumb nails from Wallhaven
private void getThumbNails() {
for(String ID : IDs) {
thumbs.add(getImage(ID));
Log.d("DEBUG", "Thumnail " + ID + " downloaded!");
}
}
private Bitmap getImage(String ID) {
Bitmap thumb = null;
InputStream in = null;
try {
in = (InputStream) new URL("http://alpha.wallhaven.cc/wallpapers/thumb/small/th-" + ID + ".jpg").getContent();
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e("URL ERROR", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
Log.e("IO ERROR", e.getMessage());
}
thumb = BitmapFactory.decodeStream(in);
try {
in.close();
} catch (IOException e) {
Log.e("IO ERROR", e.getMessage());
e.printStackTrace();
}
return thumb;
}
}
}
Und die ImageAdapter Klasse:
Java:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Vector<Bitmap> thumbs;
public ImageAdapter(Context c, Vector<Bitmap> thumbs) {
this.thumbs = thumbs;
mContext = c;
}
@Override
public int getCount() {
return thumbs.size();
}
@Override
public Object getItem(int i) {
return thumbs.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView;
if(view == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) view;
}
imageView.setImageBitmap(thumbs.get(i));
return null;
}
}
Zuletzt bearbeitet: