如何使用变量<objects>模仿CustomListAdapter

时间:2017-01-06 04:26:27

标签: java android user-interface

我正在尝试为我的一个小项目实现customlistadapter。我基本上想要让java使用适当的类来膨胀视图。我先来这里:

public class slide {

    public class video {
        VideoView videoOfTheDay;

        //Purpose of this constructor
        public video(VideoView videoOfTheDay) {
            this.videoOfTheDay = videoOfTheDay;
        }

        public VideoView getVideoOfTheDay() {
            return videoOfTheDay;
        }

    }

    public class blog {
        ImageView imageOfTheDay;
        TextView messageOfTheDay;

        public blog(ImageView imageOfTheDay, TextView messageOfTheDay) {
            this.imageOfTheDay = imageOfTheDay;
            this.messageOfTheDay = messageOfTheDay;
        }

        public ImageView getImageOfTheDay() {
            return imageOfTheDay;
        }

        public TextView getMessageOfTheDay() {
            return messageOfTheDay;
        }
    }

    public class advertisement {
        ImageView ImageViewAd1;
        ImageView ImageViewAd2;

        public advertisement(ImageView imageViewAd1, ImageView imageViewAd2) {
            this.ImageViewAd1 = imageViewAd1;
            this.ImageViewAd2 = imageViewAd2;
        }

        public ImageView getImageViewAd1() {
            return ImageViewAd1;
        }

        public ImageView getImageViewAd2() {
            return ImageViewAd2;
        }
    }
}`

我已经列出了超类幻灯片中的所有类,因为如果不将它们分组,我就无法完成任何错误。从那里我开始要求Java查看自己并确定用于填充元素的适当类:

class CustomListAdapter extends BaseAdapter {
        private ArrayList<slide> customVariableDisplay;
        private LayoutInflater layoutInflater;

        public CustomListAdapter(Context context, ArrayList<slide>customVariableDisplay) {
            this.customVariableDisplay = customVariableDisplay;
            layoutInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return customVariableDisplay.size();
        }

        public Object getItem(int position) {
            return customVariableDisplay.get(position);
        }

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

        // If the element of the slide is a video -- then the getView will return...
        if(slide==slide.video){
            public View getView ( int position, View convertView, ViewGroup parent){
            ViewHolder holder;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.act_layout, null);
                holder = new ViewHolder();
                holder.slide.video = (VideoView) convertView.findViewById(R.id.videolayout);
                }
            else{
                holder = (ViewHolder)convertView.getTag();
            }
                holder.video.setVideoResource(customVariableDisplay.get(position).getVideoOfTheDay());
            }
            return convertView;
        }
        // If the element is a 'blog' then --- then the getView will return...
        else if(slide==slide.blog){
            public View getView ( int position, View convertView, ViewGroup parent){
            ViewHolder holder;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.act_layout, null);
                holder = new ViewHolder();
                holder.slide.blog.message = (TextView) convertView.findViewById(R.id.messageInLayout);
                holder.slide.blog.image = (ImageView) convertView.findViewById(R.id.imageInLayout);
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
                //Can write to getClass() for either?
                //Ex: holder.(setImageResource(cVD) || setText(cVD)).getClass ??
                holder.image.setImageResource(customVariableDisplay.get(position).getImageofTheDay());
                holder.message.setText(customVariableDisplay.get(position).getMessageOfTheDay());
            }
            return convertView;
        }
        //Else if the element of the slide is an 'advertisement' then the getView will return...
        else if (slide==slide.advertisement){
            public View getView ( int position, View convertView, ViewGroup parent){
                ViewHolder holder;
                if (convertView == null) {
                    convertView = layoutInflater.inflate(R.layout.act_layout, null);
                    holder = new ViewHolder();
                    holder.slide.advertisement.imagead1 = (ImageView)convertView.findViewById(R.id.imageAdOneInAdvertisementLayout);
                    holder.slide.blog.image = (ImageView)convertView.findViewById(R.id.imageAdTwoInAdvertismentLayout);
                    convertView.setTag(holder);
                }
                else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.imagead1.setImageResource(customVariableDisplay.get(position).getImageViewAd1());
                holder.imagead2.setImageResource(customVariableDisplay.get(position).getImageViewAd2());
            }
            return convertView;
        }
        else{
            //Throw a final View exception for unprecedented errors!!
        }
    }
}`

我不知道如何在if语句中询问Java是什么类。 //如果此幻灯片包含课程博客......等任何帮助都会受到赞赏!谢谢!

2 个答案:

答案 0 :(得分:0)

您可以在适配器中声明一个变量slideType并将其传递给适配器构造函数,并根据该值在onBindViewHolder()onCreateViewHolder()

中夸大您的布局
int slideType;

public CustomListAdapter(Context context, ArrayList<slide>customVariableDisplay, int slideType) {
  this.slideType = slideType;
}

并在您的适配器中定义三个单独的方法来传递您的列表,以便您可以绑定数据。

答案 1 :(得分:0)

$(div).on("click", ".download", function(){ 是否会显示混合的视频,博客等。如果是,那么您需要使用以下检查。

div

此外,您需要为幻灯片对象中的博客和广告设置null,或者如果您要显示的项目是视频,则不要初始化它们