Gallery on image change textview update

我与影子孤独终老i 提交于 2019-12-25 05:16:31

问题


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?


回答1:


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) {}
    });

}


来源:https://stackoverflow.com/questions/10299713/gallery-on-image-change-textview-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!