图像& string数组无法解析为变量

时间:2013-12-06 20:20:28

标签: java android arrays android-viewpager android-arrayadapter

我收到错误声明“mImages& stringArray无法解析为变量”但是我不确定为什么。我使用了以下示例,我非常确定我已经非常密切地关注它了。

Different text for each image in image viewpager

错误位置:

mImages cannot be resolved to a variable:

ImagePagerAdapter adapter = new ImagePagerAdapter(this, mImages, stringArray);

stringArray cannot be resolved to a variable:

uploader.setText(stringArray[position]);

消息来源:Home.java

   public class Home extends YouTubeBaseActivity implements

VideoClickListener {
    // A reference to our list that will hold the video details
    private VideosListView listView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    private int mCurrentTabPosition = NO_CURRENT_POSITION;
    private static final int NO_CURRENT_POSITION = -1;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private String[] drawerListViewItems;
    private ViewPager mPager;
    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    String TAG = "DEBUG THIS";
    String PLAYLIST = "idconex";
    Activity activity;
    int imageArray[];
    String[] stringArray;
    private OnPageChangeListener mPageChangeListener;
    ImagePagerAdapter adapter = new ImagePagerAdapter(this, mImages, stringArray);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        final ActionBar actionBar = getActionBar();
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

        viewPager.setAdapter(adapter);

        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);

        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
                drawerLayout, /* DrawerLayout object */
                R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open, /* "open drawer" description */
                R.string.drawer_close /* "close drawer" description */
                );
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);
        listView = (VideosListView) findViewById(R.id.videosListView);
        // Here we are adding this activity as a listener for when any row in
        // the List is 'clicked'
        // The activity will be sent back the video that has been pressed to do
        // whatever it wants with
        // in this case we will retrieve the URL of the video and fire off an
        // intent to view it
        listView.setOnVideoClickListener(this);
        new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();
    }

    Handler responseHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            populateListWithVideos(msg);
        };
    };

    private void populateListWithVideos(Message msg) {
        Library lib = (Library) msg.getData().get(
                GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());
    }

    @Override
    protected void onStop() {
        responseHandler = null;
        super.onStop();
    }

    // This is the interface method that is called when a video in the listview
    // is clicked!
    // The interface is a contract between this activity and the listview

    @Override
    public void onVideoClicked(Video video) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(video.getUrl()));
        startActivity(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
        // true
        // then it has handled the app icon touch event
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

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


        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.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
                PLAYLIST.replace("TimMcGrawVEVO‎", PLAYLIST);

                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

                         PLAYLIST.replace("TimMcGrawVEVO‎", PLAYLIST);
                        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

                }

            });

        }
    }
}

消息来源:VideosAdapter

public class VideosAdapter extends BaseAdapter {
    // The list of videos to display
    List<Video> videos;
    // An inflator to use when creating rows
    private LayoutInflater mInflater;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    Context my_context;
    /**
     * @param context this is the context that the list will be shown in - used to create new list rows
     * @param videos this is a list of videos to display
     */
    public VideosAdapter(Context context, List<Video> videos) {
        this.videos = videos;
        this.mInflater = LayoutInflater.from(context);
        my_context = context;
    }

    @Override
    public int getCount() {
        return videos.size();
    }

    @Override
    public Object getItem(int position) {
        return videos.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // If convertView wasn't null it means we have already set it to our list_item_user_video so no need to do it again
        if(convertView == null){
            // This is the layout we are using for each row in our list
            // anything you declare in this layout can then be referenced below
            convertView = mInflater.inflate(R.layout.list_item_user_video, parent, false);
        }
        // We are using a custom imageview so that we can load images using urls
        // For further explanation see: http://blog.blundell-apps.com/imageview-with-loading-spinner/
        UrlImageView thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);

        TextView title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
        TextView uploader = (TextView) convertView.findViewById(R.id.userVideouploaderTextView); 
    //  TextView uploader = (TextView) layout.findViewById(R.id.userVideouploaderTextView);
        uploader.setText(stringArray[position]);

            fav_up_btn1 = (Button) convertView.findViewById(R.id.fav_up_btn1);
        fav_up_btn1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                boolean favIsUp = fav_up_btn1
                        .getBackground()
                        .getConstantState()
                        .equals(my_context.getResources().getDrawable(
                                R.drawable.fav_up_btn1).getConstantState());

                // set the background
                fav_up_btn1
                .setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
                        : R.drawable.fav_up_btn1);
            }
        });

        // Get a single video from our list
        final Video video = videos.get(position);
        // Set the image for the list item
        thumb.setImageDrawable(video.getThumbUrl());
        // Set the title for the list item
        title.setText(video.getTitle());
        uploader.setText("by " + video.getUploader() + " |  ");

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

您没有stringarray,VideoAdapter.java中有videos。所以你应该做一些像uploader.setText(videos.get(position).getTitle())

这样的事情