M
madgnoll
Neues Mitglied
- 0
Hallo zusammen,
ich bin Android/Java Beginner und tüftle gerade an einer Bildergalerie. Bei dieser sollen sich einzelne Bilder der Galarie ausblenden lassen. Also wie eine Filterfunktion.
Eclipse zeigt mir keinen Fehler an. Leider stürzt aber die App bei Betätigung der Checkbox ab.
Vor dem Einbau der "Filterfunktion"/Checkbox hat die Galerie fehlerfrei funktioniert.
Woran könnte dies liegen bzw. wo liegt mein Fehler?
Vielen Dank im Voraus für Eure Antworten,
Beste Grüße,
madgnoll
ich bin Android/Java Beginner und tüftle gerade an einer Bildergalerie. Bei dieser sollen sich einzelne Bilder der Galarie ausblenden lassen. Also wie eine Filterfunktion.
Eclipse zeigt mir keinen Fehler an. Leider stürzt aber die App bei Betätigung der Checkbox ab.
Vor dem Einbau der "Filterfunktion"/Checkbox hat die Galerie fehlerfrei funktioniert.
Woran könnte dies liegen bzw. wo liegt mein Fehler?
Vielen Dank im Voraus für Eure Antworten,
Beste Grüße,
madgnoll
Code:
package com.example.bildergalarie;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
//the images to display
Integer[] imageIDs = {
R.drawable.eins,
R.drawable.zwei,
R.drawable.drei,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Note that Gallery view is deprecated in Android 4.1---
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,long id)
{
Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
// display the images selected
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
// sets a grey background; wraps around the images
TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
a.recycle();
}
// returns the number of images
public int getCount() {
return imageIDs.length;
}
// returns the ID of an item
public Object getItem(int position) {
return position;
}
// returns the ID of an item
public long getItemId(int position) {
return position;
}
// returns an ImageView view
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
public void itemClicked(View v) {
CheckBox checkBox1 = (CheckBox)v;
if (checkBox1.isChecked()){
ImageView imageView1 = (ImageView) findViewById(R.drawable.eins);
imageView1.setVisibility(View.VISIBLE);
} else {
ImageView imageView1 = (ImageView) findViewById(R.drawable.eins);
imageView1.setVisibility(ImageView.GONE);
}
}
}
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="{relativePackage}.${activityClass}"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image1"
android:layout_width="215dp"
android:layout_height="315dp"
android:layout_gravity="center_horizontal"
android:background="#cfcfcf"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="@drawable/eins" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="22dp"
android:onClick="itemClicked"
android:text="CheckBox" />
</LinearLayout>