自定义适配器不填充GridView

时间:2016-02-05 17:27:37

标签: android gridview android-adapter custom-adapter recycler-adapter

我已经制作了一个自定义适配器来填充我的gridView。我正在做的是我试图用两个不同的数据(也是自定义)填充gridview,它们是:Movies和FavouriteMovies。当使用电影更新dridview时它会更新得很好,但是当它的FavouriteMovies它没有。我将FavouriteMovies存储在一个sqlite数据库中,然后查询它以返回一个游标。数据返回完美,没有错误。主要问题是我的MoviesAdapter。在这里当我添加电影到适配器它添加到MoviesAdapter内的列表,它工作得很好但是当我将FavouriteMovies添加到适配器时,它不会将它添加到MoviesAdapter内的favouriteList内,它的大小始终为零。 请帮忙!! 这是我的MoviesAdapter:     包com.akshitjain.popularmovies;

template elastic {
    subject = `Time: {{.State.Touched.Format "15:04:05UTC"}}`
}
//Changed on 2016 Feb 01 to
template elastic {
    subject = `Time: {{.Last.Time.Format "15:04:05UTC"}}`
}

这是我要添加到适配器的地方:

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


public class MoviesAdapter extends BaseAdapter {


private Context mContext;
List<Movies> list = new ArrayList<>();
List<FavouriteMovies> favouriteList = new ArrayList<>();

public MoviesAdapter(Context c) {
    this.mContext = c;
}

@Override
public int getCount() {
    if (list != null)
        return list.size();
    else
        return favouriteList.size();
}

@Override
public Object getItem(int position) {
    if (list != null)
        return list.get(position);
    else
        return favouriteList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView =     LayoutInflater.from(mContext).inflate(R.layout.grid_item_movies, parent, false);
    }

    ImageView moviePoster = (ImageView) convertView.findViewById(R.id.grid_item_movie_image);
    if (list != null) {
        Movies movies = list.get(position);
        String posterPath = movies.posterPath;
        final String POSTER_FINAL_URL = Constants.IMAGE_BASE_URL +     Constants.POSTER_SIZE_LARGE + posterPath;

            Picasso.with(mContext).load(POSTER_FINAL_URL.trim()).into(moviePoster);
    } else {
        FavouriteMovies favouriteMovies = favouriteList.get(position);
        byte[] bb = favouriteMovies.posterImage;
        moviePoster.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0,       bb.length));
    }

    return convertView;
}

public void add(Movies movies) {
    list.add(movies);
}

public void offlineAdd(FavouriteMovies favouriteMovies) {
    favouriteList.add(favouriteMovies);
}

public void clear() {
    list.clear();
}

}

对不起我的粗俗语言。

2 个答案:

答案 0 :(得分:0)

如果您设置自己喜欢的电影,则总是会有count == 0

List<Movies> list = new ArrayList<>();

@Override
public int getCount() {
    if (list != null)
        return list.size();
    else
        return favouriteList.size();
}

这将始终返回0的计数,因为列表永远不会是null。你最喜欢的电影会被忽略。

解决方案

  • 由于您只显示电影收藏夹,因此您应使用2个适配器。如果您不想这样做,
  • 使用boolean。只需将showFavorites设置为true,然后显示它们即可。或者如果你保留空检查,
  • 在显示收藏时设置List<Movie> = null;,然后它也会有效。

答案 1 :(得分:0)

使用以下适配器: -

boolean[] CheckedItem_Adap;
ImageLoader imgload;
int headercount = 0, childCount;
LinkedList<HashMap<String, String>> allData;
LinkedList<HashMap<String, String>> temp_Header;
LinkedList<HashMap<String, String>> temp_child = new LinkedList<HashMap<String, String>>();
ArrayList<String> header;
ArrayList<String> childCount1;
private int[] mSectionIndices;
private String[] mSectionLetters;
private LayoutInflater inflater;
private Activity context;

public Adapter_UserTimeLine_StickyGrid(Activity context1, LinkedList<HashMap<String, String>> list, boolean[] checkedItems, ArrayList<String> childCountArray, ArrayList<String> headerarray, LinkedList<HashMap<String, String>> temp_Headers_LinkList) {

    this.CheckedItem_Adap = checkedItems;
    allData = list;
    inflater = LayoutInflater.from(context1);
    imgload = new ImageLoader(context1);
    childCount1 = childCountArray;
    header = headerarray;
    temp_Header = temp_Headers_LinkList;
    context = context1;
}

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

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder = new ViewHolder();
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.sticky_listitems, parent, false);
    }


    holder.ivPostPic_Sticky = (ImageView) convertView.findViewById(R.id.ivPostPic_Sticky);
    holder.ivSelectedToDelete_Sticky = (ImageView) convertView.findViewById(R.id.ivSelectedToDelete_Sticky);
    holder.rlPostSticky_ManageSticky = (RelativeLayout) convertView.findViewById(R.id.rlPostSticky_ManageSticky);

    if (CheckedItem_Adap[position] == true) {
        holder.ivSelectedToDelete_Sticky.setVisibility(View.VISIBLE);
    } else if (CheckedItem_Adap[position] == false) {
        holder.ivSelectedToDelete_Sticky.setVisibility(View.GONE);
    }

    if (allData.get(position).get("media_type").equals("text")) {
        holder.ivPostPic_Sticky.setImageResource(R.drawable.line_small);
    } else {
        imgload.DisplayImage(Constants.Imge_baseUrl + allData.get(position).get("video_thumb"),
        holder.ivPostPic_Sticky);
    }


    holder.rlPostSticky_ManageSticky.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (UserTimeLine_Activity.isDeleteSelected) {
                if (CheckedItem_Adap[position] == false) {
                    UserTimeLine_Activity.Selected_PostIds.add(allData.get(position).get("post_id"));
                    holder.ivSelectedToDelete_Sticky.setVisibility(View.VISIBLE);
                    CheckedItem_Adap[position] = true;
                } else {
                    UserTimeLine_Activity.Selected_PostIds.remove(allData.get(position).get("post_id"));
                    holder.ivSelectedToDelete_Sticky.setVisibility(View.GONE);
                    CheckedItem_Adap[position] = false;
                }

                for (int i = 0; i < UserTimeLine_Activity.Selected_PostIds.size(); i++) {
                    Log.e("postid", i + "-" + UserTimeLine_Activity.Selected_PostIds.get(i) + "");

                }
                if (UserTimeLine_Activity.Selected_PostIds.get(0).equals(UserTimeLine_Activity.Selected_PostIds.get(0))) { }
            } else {
                String media_type = allData.get(position).get("media_type");
                String post_id = allData.get(position).get("post_id");
                Intent in = new Intent(context, PostDetails_UserOwn_TimeLIne_Activity.class);
                in.putExtra("media_type", media_type);
                in.putExtra("post_id", post_id);
                in.putExtra("position", String.valueOf(position));
                in.putExtra("post_User_Name", allData.get(position).get("post_User_Name"));
                context.startActivity(in);
            }
        }
    });

    return convertView;
}

@Override
public int getCountForHeader(int i) {
    int c = Integer.valueOf(childCount1.get(i));
    return c;
}

@Override
public int getNumHeaders() {
    return temp_Header.size();
}

@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
    HeaderViewHolder holder = new HeaderViewHolder();
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.headersticky_managepost, parent, false);
    }

    temp_Header.get(position).get("post_datetime");

    TextView tvDate_Sticky = (TextView) convertView.findViewById(R.id.tvDate_Sticky);
    for (int i = 0; i < temp_Header.size(); i++) {
    }

    String Date = temp_Header.get(position).get("post_datetime");
    String FormatedDate = parseDateToddMMyyyy(Date);
    tvDate_Sticky.setText(FormatedDate);
    return convertView;
}

public String parseDateToddMMyyyy(String time) {
    String inputPattern = "yyyy-MM-dd";
    String outputPattern = "MMM-dd-yyyy";
    SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

    Date date = null;
    String str = null;

    try {
        date = inputFormat.parse(time);
        str = outputFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}

class HeaderViewHolder {
    TextView tvDate_Sticky;
}

class ViewHolder {
    ImageView ivPostPic_Sticky, ivSelectedToDelete_Sticky;
    RelativeLayout rlPostSticky_ManageSticky;
}
}