使用自定义ArrayAdapter填充GridView第一次不会加载(asynktask)

时间:2016-05-19 03:09:37

标签: android android-studio gridview android-arrayadapter custom-adapter

问题是我无法弄清楚为什么网格不会填充图像,这是我第一次运行应用程序。如果我刷新或更改为横向模式,图像加载,应用程序工作正常。我假设它必须是我给适配器的List,不知何故,我第一次丢失了他的引用,当onCreateView再次调用(刷新/横向)时,它得到修复。我试图改变我定义适配器,列表等的地方和一些像setGridData一样的方法,但它不起作用。也许你可以帮助我

这是片段和asynkTask的代码(顺便说一下它工作正常)。

public class GridViewAdapter extends ArrayAdapter {


private static final String LOG_TAG = GridViewAdapter.class.getSimpleName();
private Context mContext;
private int mLayoutResourceId;
private ArrayList<Movie> mGridData;
/**
 * This is our own custom constructor (it doesn't mirror a superclass constructor).
 * The context is used to inflate the layout file, and the List is the data we want
 * to populate into the lists
 *
 * @param context        The current context. Used to inflate the layout file.
 * @param movieList A List of AndroidFlavor objects to display in a list
 */
public GridViewAdapter(Activity context, int layoutResourceId, ArrayList<Movie> movieList) {
    // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
    // the second argument is used when the ArrayAdapter is populating a single TextView.
    // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
    // going to use this second argument, so it can be any value. Here, we used 0.
    super(context, layoutResourceId, movieList);
    mContext=context;
    mLayoutResourceId=layoutResourceId;
    mGridData=movieList;

}

/**
 * Updates grid data and refresh grid items.

 */
public void setGridData(List<Movie> movies) {
    mGridData.clear();
    mGridData.addAll(movies);
    notifyDataSetChanged();
}

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

    View row = convertView;
    ImageView imageView;
    if (row == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(mLayoutResourceId, parent, false);
        imageView = (ImageView) row.findViewById(R.id.list_item_movie_imageview);
        row.setTag(imageView);
    } else {
        imageView = (ImageView) row.getTag();
    }
    Movie movie =  mGridData.get(position);
    Picasso.with(mContext).load(movie.getPoster_path()).into(imageView);
    Log.d(LOG_TAG,movie.getOriginalTitle());
    return row;
}

}

这是适配器的代码:

CREATE PROCEDURE `get_bindings_chart`(IN in_layout_id TINYINT(3), IN in_game_id SMALLINT(5))
BEGIN
    SELECT  b.normal_group, b.normal_action, b.shift_group, b.shift_action, b.ctrl_group, b.ctrl_action, b.alt_group, b.alt_action, b.altgr_group, b.altgr_action, b.extra_group, b.extra_action, b.image_file, b.key_number
    FROM    bindings as b
    WHERE   b.layout_id = in_layout_id
    AND     b.game_id = in_game_id;
END

电影类是一个包含数据的简单类。 getView()方法中的日志在第一次运行时只弹出一次,但必须是20次。

1 个答案:

答案 0 :(得分:0)

我也有类似的问题。请尝试以下方法:

mGridViewAdapter.notifyDataSetChanged()

目前。你没有在任何对象上调用它。如果这不起作用,请尝试使用此方法:

mGridViewAdapter.notifyDataSetInvalidated()

进一步阅读:How to refresh Android listview?