Question

Hello I have a gallery with 2 images in it. I want each time i change the image a textview in my layout to change its name. In my code it happens only the first time and remains that way.

public class BrowseActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browse_activity_layout);

    Gallery gallery = (Gallery) findViewById(R.id.browseActivityGallery);
    gallery.setAdapter(new ImageAdapter(this)); 
    gallery.setSpacing(0); 
}
public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {

            R.drawable.building_a,
            R.drawable.building_b,

    };

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.browseActivityGalleryStylable);
        attr.recycle();
    }

    public int getCount() {

        return mImageIds.length;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView browseActivityTitleTextView = (TextView)findViewById(R.id.browseActivityTitleTextView);

            if(position == 0){
                browseActivityTitleTextView.setText(" Α'");
            }
            if(position == 1){
                browseActivityTitleTextView.setText("  Β'");
            }           

        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mImageIds[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
        return imageView;
    }
}
}

When i swipe the gallery it changes to B' as it should but when i swipe back it stays B' Anyone knows how to fix this?

Was it helpful?

Solution

Rather than changing the text from the BaseAdapter you should add a listener to the gallery and change it that way. You should also find the reference to the textview in your onCreate method.

private TextView browseActivityTitleTextView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse_activity_layout);

browseActivityTitleTextView = (TextView)findViewById(R.id.browseActivityTitleTextView);

Gallery gallery = (Gallery) findViewById(R.id.browseActivityGallery);
gallery.setAdapter(new ImageAdapter(this)); 
gallery.setSpacing(0); 

gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {              

            switch(position){
            case 0:
                browseActivityTitleTextView.setText(" Α'");
                break;
            case 1:
                browseActivityTitleTextView.setText(" B'");
                break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {}
    });

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top