我想让GridLayout
每个街区成为电影海报。我使用themoviedb.org来做到这一点。我认为我做的一切都正确,但我的应用程序中仍然没有任何显示,有人可以帮助我使用我的代码。
我有3个班级和1个xml文件
1)第一堂课是MainActivity:
package com.example.admin.movies;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2)第二个类是MainFragment,它是我想要使用的片段,内部AsyncTask类显示take" poster_path"来自JSON查询: 包com.example.admin.movies;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by Admin on 11/7/2016.
*/
public class MainFragment extends Fragment {
private ArrayList<String> imagesEndPointsList;
public MainFragment(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetImagesAsyncTask sync = new GetImagesAsyncTask();
sync.execute();
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main,container,false);
GridView gridView = (GridView)rootView.findViewById(R.id.grid_movies);
gridView.setAdapter(new ImageAdapter(getContext(),imagesEndPointsList));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(),""+position,Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public class GetImagesAsyncTask extends AsyncTask<Void,Void,String[]> {
private String [] imagesEndPoints;
final private String LOG_TAG = GetImagesAsyncTask.class.getSimpleName();
//My API Key to https://www.themoviedb.org/
String myKey;
@Override
protected void onPreExecute() {
super.onPreExecute();
myKey = "29a99f9dc266ab3a2108e9f9511a05ed";
}
@Override
protected String[] doInBackground(Void... 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;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
String API_KEY = "api_key";
try{
final String BASE_URL =
"http://api.themoviedb.org/3/movie/popular?";
Uri buildUri = Uri.parse(BASE_URL).buildUpon()
.appendQueryParameter(API_KEY,myKey).build();
URL url = new URL(buildUri.toString());
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.
return 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.
return null;
}
forecastJsonStr = buffer.toString();
Log.v(LOG_TAG,forecastJsonStr);
return getMovieDataFromJson(forecastJsonStr);
}catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attempting
// to parse it.
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
return null;
}
@Override
protected void onPostExecute(String[] string) {
super.onPostExecute(string);
if(string != null) {
imagesEndPointsList.clear();
for (String dayForecastStr : string) {
imagesEndPointsList.add(dayForecastStr);
}
}
}
private String[] getMovieDataFromJson(String JsonString) throws JSONException{
// Weather information. Each day's forecast info is an element of the "list" array.
final String OWM_RESULT = "results";
final String OWM_PATH = "poster_path";
try{
JSONObject forecastJson = new JSONObject(JsonString);
JSONArray imagesArray = forecastJson.getJSONArray(OWM_RESULT);
imagesEndPoints = new String[imagesArray.length()];
for (int i = 0 ; i<imagesArray.length(); i++){
JSONObject dayForecast = imagesArray.getJSONObject(i);
imagesEndPoints[i] = dayForecast.getString(OWM_PATH);
return imagesEndPoints;
}
}catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
}
}
3)我建立的适配器:
package com.example.admin.movies;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by Admin on 11/7/2016.
*/
public class ImageAdapter extends BaseAdapter {
final private String BASE_URL =
"http://image.tmdb.org/t/p/w185/";
Context mContext;
private ArrayList<String> endPoints;
public ImageAdapter(Context context, ArrayList<String> arrayList){
mContext = context;
endPoints = arrayList;
}
@Override
public int getCount() {
return endPoints.size();
}
@Override
public Object getItem(int position)
{
return endPoints.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
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);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}else{
imageView = (ImageView)convertView;
}
Log.v("BASEURL",BASE_URL+endPoints.get(position));
Picasso.with(mContext).load(BASE_URL+endPoints.get(position)).into(imageView);
return imageView;
}
}
4)和xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.movies.MainFragment">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/grid_movies"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</FrameLayout>
答案 0 :(得分:0)
我认为您的问题依赖于适配器的getView(),只需使用imageView创建一个xml,并将此资源用作每行的项目,而不是以编程方式创建imageView。
@Override
public View getView(int position,View convertView,ViewGroup parent){
ViewHolderItem viewHolder;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.imageViewItem= (ImageView) convertView.findViewById(R.id.imageView);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
// object item based on the position
String url = BASE_URL+endPoints.get(position);
// assign values if the object is not null
if(url!= null) {
Piccaso. ---.into(imageViewItem);
}
return convertView;
}
答案 1 :(得分:0)
tools:context="com.example.admin.movies.MainFragment"
没有告诉Android替换FrameLayout
。它只是让Android Studio用片段渲染预览。
如果要通过XML添加片段,则需要执行以下操作:
<fragment class="com.example.admin.movies.MainFragment"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
答案 2 :(得分:0)
在设置Adapter之前,您确定AsyncTask已完成吗?
你需要: