如何在Android中添加数据并从模型类获取数据?

时间:2018-07-19 12:16:06

标签: android

我有一个名为 PhotoviewHolder 的模型类,我想添加数据并从模型类中获取数据;

class PhotoViewModel implement ListViewModel {
String imagePath;
String date_created;
String description;

public PhotoViewModel(String imagePath, String date_created, String description) {
    this.imagePath = imagePath;
    this.date_created = date_created;
    this.description = description;
}


public PhotoViewModel() {

}
public String getImagePath() {
    return imagePath;
}

public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
}

public String getDate_created() {
    return date_created;
}

public void setDate_created(String date_created) {
    this.date_created = date_created;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@Override
public String toString() {
    return "PhotoViewModel{" +
            "imagePath='" + imagePath + '\'' +
            ", date_created='" + date_created + '\'' +
            ", description='" + description + '\'' +
            '}';
} 
}

这是我尝试过的内容::

  

例如:我添加了此代码mainactivity >>我只得到图像   该代码未获取数据的mainactivity中的路径   另一项活动...

       List<Photo> photos = mPhotos;
                List<ListViewModel> listViewModels = new ArrayList<>();
                for (Photo photo : photos) {
                    if(photo.getPopularlist().contains("true")){
                        if(photo.getType_post().equals("Photo")){
                            List<PhotoViewModel> pho = new ArrayList<>();
                            PhotoViewModel p = new PhotoViewModel();
                            p.setImagePath(photo.getImage_path());
                            pho.add(p);

                            listViewModels.add(new PhotoViewModel(photo.getImage_path(),photo.getDescription(),photo.getDate_created()));
                           // Log.d(TAG, "onDataChange: alphs"+photoViewModel1.getImagePath());

                        }
                        if(photo.getType_post().equals("Video")){
                            listViewModels.add(new VideoViewModel(photo.getImage_path()));
                        }
                    }
                }

此代码是我获取的数据,但没有获取显示 null 的数据,任何人都可以帮助我...我是新手

PhotoViewModel photoViewModel= new PhotoViewModel();
Log.d(TAG, "onDataChange: photoviewmodeldata"+photoViewModel.getImagePath());

//这是我的适配器代码

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup 

父级,int viewType){

    //findViewById...
    mContext = parent.getContext();
    switch (viewType) {
        case ListViewModel.VIEW_TYPE_PHOTO:
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_list, parent, false);
            mContext = parent.getContext();
            return new PhotoHolder(v);


        case ListViewModel.VIEW_TYPE_VIDEO:
            View i = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_type, parent, false);
            mContext = parent.getContext();
            return new VideoHolder(i);


    }
    return null;

}
@Override
public int getItemViewType(int position) {
    return mListViewModels.get(position).getViewType();
}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

    int viewType = getItemViewType(position);
    switch (viewType) {
        case ListViewModel.VIEW_TYPE_PHOTO:
            final PhotoHolder photoview = (PhotoHolder) holder;
            mPhotoHolder = photoview;
            mPhoto = new Photo();
            PhotoViewModel p =  new PhotoViewModel();
            photoview.usersPhoto = new StringBuilder();
            final ImageLoader imageLoader = ImageLoader.getInstance();
           PhotoViewModel photoViewModel= new PhotoViewModel();
            Log.d(TAG, "onBindViewHolder: photoviewmodeldata "+photoViewModel.getImagePath());
            break;

        case ListViewModel.VIEW_TYPE_VIDEO:
            final VideoHolder viewHolder2 = (VideoHolder) holder;
            mVideoHolder = viewHolder2;
            viewHolder2.usersVideo = new StringBuilder()
        //get the profile image and username

            break;
    }
}
public ListViewModel getItem(int position) {
    return mListViewModels.get(position);
}
public void setListViewModels(List<ListViewModel> listViewModels) {
    mListViewModels = listViewModels;
    notifyDataSetChanged();
}
@Override
public int getItemCount() {
     return mListViewModels == null? 0 : mListViewModels.size();
}
public Trending_adapter(){
}

1 个答案:

答案 0 :(得分:0)

对于您遇到的问题,此行可以自行解释。

PhotoViewModel photoViewModel= new PhotoViewModel();
Log.d(TAG, "onBindViewHolder: photoviewmodeldata"+photoViewModel.getImagePath());

您关心创建新对象并尝试从新对象获取数据,而无需将数据初始化到该对象。

此外,在您的代码中,我没有看到这些行的适当需要。

List<PhotoViewModel> pho = new ArrayList<>();
PhotoViewModel p = new PhotoViewModel();
p.setImagePath(photo.getImage_path());
pho.add(p);

请清除所有这些混淆,并使其与OOP概念相适应。

  

更新:

好的。作为您的适配器代码,您正在使用以下代码从Array获取ListItem的数据。您不是在从ArrayList获得价值,而是在创建PhotoViewModel的新实例。

case ListViewModel.VIEW_TYPE_PHOTO:
        final PhotoHolder photoview = (PhotoHolder) holder;
        mPhotoHolder = photoview;
        mPhoto = new Photo();
        PhotoViewModel p =  new PhotoViewModel();
        photoview.usersPhoto = new StringBuilder();
        final ImageLoader imageLoader = ImageLoader.getInstance();
       PhotoViewModel photoViewModel= new PhotoViewModel();
        Log.d(TAG, "onBindViewHolder: photoviewmodeldata "+photoViewModel.getImagePath());
        break;

尝试使用getItem(position)方法获取项目。您将从数组中获得相对位置的对象。

        final PhotoHolder photoview = (PhotoHolder) holder;

        ListViewModel listItem = getItem(position); // getting item from listview. Please check of positioning by yourself.

        /** Omiting extra code */ 

        if(listItem instance of PhotoViewModel){ //because PhotoViewModel extends ListViewModel 
        //and you used parent class in ArrayList instead of Two Different Child Class. 
            PhotoViewModel photoViewModel =  (PhotoViewModel)listItem; // downcasting to child class
            Log.d(TAG, "onBindViewHolder: photoviewmodeldata "+photoViewModel.getImagePath());
        }
        break;