我可以使用自定义适配器在gridview中正确显示图像(默认设置)。 但是,当我尝试更改ForecastFragment(下面的代码段)中的字符串[]时,不会调用updateList函数。我希望重新创建网格中的所有视图。
ImageAdapter.java:
public class ImageAdapter extends BaseAdapter {
public Context mContext; ImageAdapter(Context c) {
mContext = c;
String LOG_TAG = ImageAdapter.class.getSimpleName();
Log.e(LOG_TAG,"Constructor Context : "+mContext);
}
public int getCount() {
return this.eatFoodyImages.length;
}
public int size1;
public int size2;
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public void updateList(String[] string)
{
this.eatFoodyImages=string;
this.notifyDataSetChanged();
String LOG_TAG = ImageAdapter.class.getSimpleName();
Log.e(LOG_TAG, "\n\nupdatelist : " + this.eatFoodyImages);
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
size1 = (int) this.mContext.getResources().getDimension(R.dimen.image_size1);
size2 = (int) this.mContext.getResources().getDimension(R.dimen.image_size2);
imageView.setLayoutParams(new GridView.LayoutParams(size1,size2));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
String LOG_TAG = ImageAdapter.class.getSimpleName();
Log.e(LOG_TAG, "\n\n\nPosition : " + position + " Strings Array : \n\n" + eatFoodyImages[position].toString());
//System.out.println();
Picasso.with(mContext)
.
load(eatFoodyImages[position])
.
into(imageView);
return imageView;
}
// references to our images
public String[] eatFoodyImages = {
"http://i.imgur.com/rFLNqWI.jpg", //Default Random Data!!!
"http://i.imgur.com/C9pBVt7.jpg",
"http://i.imgur.com/rT5vXE1.jpg",
"http://i.imgur.com/aIy5R2k.jpg",
"http://i.imgur.com/MoJs9pT.jpg",
"http://i.imgur.com/S963yEM.jpg",
"http://i.imgur.com/rLR2cyc.jpg",
"http://i.imgur.com/SEPdUIx.jpg",
"http://i.imgur.com/aC9OjaM.jpg",
"http://i.imgur.com/76Jfv9b.jpg",
"http://i.imgur.com/fUX7EIB.jpg",
"http://i.imgur.com/syELajx.jpg",
"http://i.imgur.com/COzBnru.jpg",
"http://i.imgur.com/Z3QjilA.jpg",
};
}
ForecastFragment.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
movieadapter = new ImageAdapter(this.getActivity());
gridview.setAdapter(movieadapter);
});
return rootView;
}
调用updateList:
public class FetchWeatherTask extends AsyncTask<String, Void, String[]>
{//Showing only relevant code
IA.updateList(eatFoodyImages); //Here eatFoodyImages contains the new String[] to be used for image loading.
答案 0 :(得分:0)
在这里,我有一个像下面的适配器
public View getView(int position, View convertView, ViewGroup parent) {
Movie movie = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.movie_tile, parent, false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.movie_image);
Picasso.with(this.getContext()).load(Constants.MOVIE_POSTER_PATH_SMALL + movie.getPosterPath()).placeholder(ContextCompat.getDrawable(this.getContext(), R.drawable.movie)).error(ContextCompat.getDrawable(this.getContext(), R.drawable.movie)).into(imageView);
return convertView;
}
在网格视图中设置图块。
我触发asynctask并从API获取电影然后通知加载电影的数据集并刷新视图。
基本上你的notifyDataSet()应该在AsyncTask的onPostExecute()中。
这是我的异步任务代码,以便更好地理解。
public class CallMovieDBAPI extends AsyncTask<String, Void, List<Movie>> {
ProgressDialog dialog;
private Activity activity;
private GridView gridView;
public CallMovieDBAPI(Activity activity, GridView gridView) {
this.activity = activity;
this.gridView = gridView;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(getActivity());
dialog.setMessage(activity.getString(R.string.progressDialogText));
dialog.show();
}
@Override
protected List<Movie> doInBackground(String... params) {
InputStream in = null;
String jsonResponse = null;
String apiURL = params[0];
try {
URL url = new URL(apiURL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
jsonResponse = getJsonObject(in);
movieList = JsonParser.parse(jsonResponse);
} catch (Exception e) {
Log.e("CallMovieDBAPI", "Exception occurred in AsyncTask - CallMovieDBAPI", e);
}
return movieList;
}
@Override
protected void onPostExecute(List<Movie> movies) {
Log.d("Popular Movies", "Got data successfully");
if (dialog.isShowing()) {
dialog.dismiss();
}
movieAdapter = new MovieAdapter(activity, movieList);
gridView.setAdapter(movieAdapter);
movieAdapter.notifyDataSetChanged();
if (isTablet) {
((MovieClickedCallback) getActivity()).onMovieClicked(0, movieAdapter);
}
}
如果这解决了,请告诉我。