gpt4 book ai didi

java - 在android中使用Picasso填充gridview

转载 作者:行者123 更新时间:2023-12-01 11:17:23 25 4
gpt4 key购买 nike

我正在尝试使用从电影数据库返回的海报图像填充我的 GridView 。我单步执行,没有遇到错误,但没有显示任何内容。这是我的代码,任何帮助将不胜感激。我认为问题出在适配器上,但我已经对此进行了调试,并且 GridView 最终得到了正确数量的子项。

公共(public)类 MainActivity 扩展 AppCompatActivity {

private GridView mMoviesGrid;
private ListAdapter mMoviesAdapter;
public ArrayList<String> mPosterMoviePaths;
String mMovieJsonStr = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mMoviesGrid = (GridView) findViewById(R.id.movie_list_grid);

new FetchMovieData().execute();

if(mMovieJsonStr != null){
mPosterMoviePaths = MovieDataParser.getMoviePosterPaths(mMovieJsonStr);
}

mMoviesAdapter = new MovieAdapter(this, mPosterMoviePaths);
mMoviesGrid.setAdapter(mMoviesAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

private class MovieAdapter extends ArrayAdapter {
private Context mContext;
private ArrayList<String> mItems;

public MovieAdapter(Context context, ArrayList<String> objects) {
super(context, R.layout.movie_grid_item,objects);
this.mContext = context;
this.mItems = objects;
}

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

//if the view is null than inflate it otherwise just fill the list with
if(convertView == null){
//inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.movie_grid_item, parent, false);
}
ImageView image =(ImageView) convertView.findViewById(R.id.movie_image);
Picasso.with(mContext).load(mItems.get(position)).into(image);
return convertView;
}
}

public class FetchMovieData extends AsyncTask <String, Void, Void> {

@Override
protected Void doInBackground(String... params) {

// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;

try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are available at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
URL url = new URL(getString(R.string.picasso_url_popular_movies));

// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
mMovieJsonStr = null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}

if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
mMovieJsonStr = null;
}
mMovieJsonStr = buffer.toString();
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attempting
// to parse it.
mMovieJsonStr = null;
} finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}

return null;
}

}

}

最佳答案

问题是你在onCreate方法中调用AsyncTask,并且在下面填充你的gridView,所以,AsyncTask需要时间通过互联网从服务器获取json,但你没有给它时间。所以当你填充你的gridview 并且当时您没有图像 url,这就是 picasso 无法加载图像的原因。

从 onCreate 中删除以下行

if(mMovieJsonStr != null){
mPosterMoviePaths = MovieDataParser.getMoviePosterPaths(mMovieJsonStr);
}
mMoviesAdapter = new MovieAdapter(this, mPosterMoviePaths);
mMoviesGrid.setAdapter(mMoviesAdapter);

将它们写入AsyncTask中的onPostExecute

@Override
protected void onPostExecute(String result) {
if(mMovieJsonStr != null){
mPosterMoviePaths = MovieDataParser.getMoviePosterPaths(mMovieJsonStr);
}
mMoviesAdapter = new MovieAdapter(Your_Activity_Name.this, mPosterMoviePaths);
mMoviesGrid.setAdapter(mMoviesAdapter);
}

作为引用,请查看下面的链接

http://developer.android.com/reference/android/os/AsyncTask.html

public class FetchMovieData extends AsyncTask <String, Void, Void> {

@Override
protected Void doInBackground(String... params) {

// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;

try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are available at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
URL url = new URL(getString(R.string.picasso_url_popular_movies));

// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
mMovieJsonStr = null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}

if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
mMovieJsonStr = null;
}
mMovieJsonStr = buffer.toString();
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attempting
// to parse it.
mMovieJsonStr = null;
} finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}

return null;
}
****Below is the onPostExecute method****

@Override
protected void onPostExecute(String result) {
if(mMovieJsonStr != null){
mPosterMoviePaths = MovieDataParser.getMoviePosterPaths(mMovieJsonStr);
}
mMoviesAdapter = new MovieAdapter(Your_Activity_Name.this, mPosterMoviePaths);
mMoviesGrid.setAdapter(mMoviesAdapter);
}

}

并通过 Activity 扩展您的 Activity

public class MainActivity extends Activity {
}

关于java - 在android中使用Picasso填充gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31650891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com