安卓/ Java的 - 更新listArray调用适配器查看(Android / Java - Up

2019-10-29 12:52发布

我试图实现一个viewPager这将让我改变Youtube的播放列表( String PLAYLIST取决于ViewPager的位置)。 有人告诉我,在另一个StackOverflow的文章中,我需要: "update the listArray prior to calling 'notifyDataSetChanged()'. The call to notify is the signal to update the View in the adapter you are using. In your code, the getYouTube... needs to update and array object that the adapter has a reference to. You should be able to get it by implementing connections in your code."

^ -资料来源: 使用ViewPager / PagerAdapter改变串

根据响应我以前SO交联上面 - 我相信我会需要使用这样的事情:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = LayoutInflater.from(context)
          .inflate(R.layout.home, parent, false);
    }

    ImageView imageView = new ImageView(context);
    ImageView imageView = getItem(position);
    imageView.setImageResource(imageView.getImage());

    return convertView;
}

^ -资料来源: http://www.piwai.info/android-adapter-good-practices/

怎么我的电流源工作原理:

我有一个包含字符串播放列表获得来自YouTube的响应JSON请求:

@Override
   protected Void doInBackground(Void... arg0) {
       try {

         HttpClient client = new DefaultHttpClient();

         HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?author="+PLAYLIST+"&v=2&alt=jsonc");

我想用一个滑动式的页脚图像(pagerAdapter)来改变字符串PLAYLIST的价值。 我最关注的是试图弄清楚如何使用如下所示的PagerAdapter设置如下:

String PLAYLIST = "vevo";
String PLAYLIST = "TheMozARTGROUP‎";
String PLAYLIST = "TimMcGrawVEVO‎";
String PLAYLIST = "TiestoVEVO‎";
String PLAYLIST = "EminemVEVO‎";

PLAYLIST的值由PagerAdapter设置,那么之后立即使用PLAYLIST的新值创建播放列表:

新GetYouTubeUserVideosTask(ResponseHandler所,PLAYLIST).execute();

我已经创建了一个包含我想使用的值一个新的字符串数组。 我的问题现在被理解为所有的是:我怎么能修改下面的源,设置使用数组值中的一个播放列表的价值,并执行新的播放列表? (目前我的源代码编译,但是当我刷卡PagerAdapter - 什么也没有发生)

截图:

电流源:

private class ImagePagerAdapter extends PagerAdapter {
    public ImagePagerAdapter(Activity act, int[] mImages,
            String[] stringArra) {
        imageArray = mImages;
        activity = act;
        stringArray = stringArra;
    }

    // this is your constructor
    public ImagePagerAdapter() {
        super();
        // setOnPageChangeListener(mPageChangeListener);
    }

    private int[] mImages = new int[] { R.drawable.selstation_up_btn,
            R.drawable.classical_up_btn, R.drawable.country_up_btn,
            R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
            R.drawable.island_up_btn, R.drawable.latin_up_btn,
            R.drawable.pop_up_btn, R.drawable.samba_up_btn };

    private String[] stringArray = new String[] { "vevo",
            "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
    "EminemVEVO‎" };

    @Override
    public int getCount() {
        return mImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Context context = Home.this;
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ScaleType.FIT_XY);
        imageView.setImageResource(mImages[position]);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }

    private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(final int position) {
            onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
            mCurrentTabPosition = position;

        }
    };

    protected void onTabChanged(final PagerAdapter adapter,
            final int oldPosition, final int newPosition) {
        // Calc if swipe was left to right, or right to left
        if (oldPosition > newPosition) {
            // left to right
        } else {
            // right to left

            View vg = findViewById(R.layout.home);
            vg.invalidate();
        }
        final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            int oldPos = viewPager.getCurrentItem();

            @Override
            public void onPageScrolled(int position, float arg1, int arg2) {

                if (position > oldPos) {
                    // Moving to the right

                } else if (position < oldPos) {
                    // Moving to the Left

                    View vg = findViewById(R.layout.home);
                    vg.invalidate();
                }
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub

            }

        });

    }
}
}

Answer 1:

看到“getView()”的方法底部在这里 ,比较它与你的代码,你孤儿ImageView的...请注意,在样品中,填充了位图的ImageView这就是属于被包含在膨胀图的图...

R.layout.item拥有R.id.text R.layout.item拥有R.id.image



文章来源: Android / Java - Updating listArray to call View in adapter