2017-05-20 13 views
1

キャストをプログラムのURLから設定できません。 MainActivity to MoviesActivityさらに私のプログラムは以下のようになります。 イメージの設定方法も教えてください。私はまだ学んでいます。Webservice、プログラムでキャストを設定できません

スクリーンenter image description here

public class Movies extends AppCompatActivity { 

    private ListView listView; 
    private ProgressDialog dialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_movies); 
     dialog = new ProgressDialog(this); 
     dialog.setIndeterminate(true); 
     dialog.setCancelable(false); 
     dialog.setMessage("Loading. Please wait..."); 
     listView = (ListView)findViewById(R.id.lvMovies); 
     new JSONTask().execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesData.txt"); 
    } 

    public class JSONTask extends AsyncTask<String,String,List<MovieModel>> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      dialog.show(); 
     } 

     @Override 
     protected List<MovieModel> doInBackground(String... params) { 
      HttpURLConnection connection = null; 
      BufferedReader reader = null; 
      try { 
       URL url = new URL(params[0]); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.connect(); 
       InputStream stream = connection.getInputStream(); 
       reader = new BufferedReader(new InputStreamReader(stream)); 
       StringBuffer buffer = new StringBuffer(); 
       String line =""; 
       while ((line = reader.readLine()) != null){ 
        buffer.append(line); 
       } 
       String finalJson = buffer.toString(); 
       JSONObject parentObject = new JSONObject(finalJson); 
       JSONArray parentArray = parentObject.getJSONArray("movies"); 
       List<MovieModel> movieModelList = new ArrayList<>(); 
       Gson gson = new Gson(); 
       for(int i=0; i<parentArray.length(); i++) { 
        JSONObject finalObject = parentArray.getJSONObject(i); 
        MovieModel movieModel = gson.fromJson(finalObject.toString(), MovieModel.class); 
        movieModelList.add(movieModel); 
       } 
       return movieModelList; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } finally { 
       if (connection != null) 
        connection.disconnect(); 
       try { 
        if (reader != null) 
         reader.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(List<MovieModel> result) { 
      super.onPostExecute(result); 
      dialog.dismiss(); 
      MovieAdapter movieAdapter = new MovieAdapter(getApplicationContext(), R.layout.row, result); 
      listView.setAdapter(movieAdapter); 
     } 
    } 

public class MovieAdapter extends ArrayAdapter{ 

     private List<MovieModel> movieModelList; 
     private int resource; 
     private LayoutInflater inflater; 

     public MovieAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<MovieModel> objects) { 
      super(context, resource, objects); 
      movieModelList = objects; 
      this.resource = resource; 
      inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     } 
     @NonNull 
     @Override 
     public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 
      ViewHolder holder = null; 
      if (convertView == null){ 
       holder = new ViewHolder(); 
       convertView = inflater.inflate(resource,null); 
       holder.ivMovieIcon = (ImageView)convertView.findViewById(R.id.ivIcon); 
       holder.tvMovie = (TextView)convertView.findViewById(R.id.tvMovie); 
       holder.tvTagline = (TextView)convertView.findViewById(R.id.tvTagline); 
       holder.tvYear = (TextView)convertView.findViewById(R.id.tvYear); 
       holder.tvDuration = (TextView)convertView.findViewById(R.id.tvDuration); 
       holder.tvDirector = (TextView)convertView.findViewById(R.id.tvDirector); 
       holder.rbMovieRating = (RatingBar)convertView.findViewById(R.id.rbMovie); 
       holder.tvCast = (TextView)convertView.findViewById(R.id.tvCast); 
       holder.tvStory = (TextView)convertView.findViewById(R.id.tvStory); 
       convertView.setTag(holder); 
      }else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      holder.tvMovie.setText(movieModelList.get(position).getMovie()); 
      holder.tvTagline.setText(movieModelList.get(position).getTagline()); 
      holder.tvYear.setText("Year : " + movieModelList.get(position).getYear()); 
      holder.tvDuration.setText("Duration : "+ movieModelList.get(position).getDirector()); 
      holder.tvDirector.setText("Director : " +movieModelList.get(position).getDirector()); 
      holder.rbMovieRating.setRating(movieModelList.get(position).getRating()/2); 

      StringBuffer stringBuffer = new StringBuffer(); 
      for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){ 
       stringBuffer.append(cast.getName() + ", "); 
      } 

      holder.tvCast.setText("Cast : " + stringBuffer); 
      holder.tvStory.setText(movieModelList.get(position).getStory()); 
      return convertView; 
     } 
     class ViewHolder{ 
      private ImageView ivMovieIcon; 
      private TextView tvMovie; 
      private TextView tvTagline; 
      private TextView tvYear; 
      private TextView tvDuration; 
      private TextView tvDirector; 
      private RatingBar rbMovieRating; 
      private TextView tvCast; 
      private TextView tvStory; 
     } 
    } 
} 

public class MovieModel { 

    private String movie; 
    private int year; 
    private float rating; 
    private String duration; 
    private String director; 
    private String tagline; 
    @SerializedName("cast") 
    private List<Cast> castList; 
    private String image; 
    private String story; 

    public String getMovie() { 
     return movie; 
    } 

    public void setMovie(String movie) { 
     this.movie = movie; 
    } 

    public int getYear() { 
     return year; 
    } 

    public void setYear(int year) { 
     this.year = year; 
    } 

    public float getRating() { 
     return rating; 
    } 

    public void setRating(float rating) { 
     this.rating = rating; 
    } 

    public String getDuration() { 
     return duration; 
    } 

    public void setDuration(String duration) { 
     this.duration = duration; 
    } 

    public String getDirector() { 
     return director; 
    } 

    public void setDirector(String director) { 
     this.director = director; 
    } 

    public String getTagline() { 
     return tagline; 
    } 

    public void setTagline(String tagline) { 
     this.tagline = tagline; 
    } 

    public List<Cast> getCastList() { 
     return castList; 
    } 

    public void setCastList(List<Cast> castList) { 
     this.castList = castList; 
    } 

    public String getImage() { 
     return image; 
    } 

    public void setImage(String image) { 
     this.image = image; 
    } 

    public String getStory() { 
     return story; 
    } 

    public void setStory(String story) { 
     this.story = story; 
    } 

    public static class Cast { 
     private String name; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 
    } 
} 

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="5dp"> 

     <ImageView 
      android:id="@+id/ivIcon" 
      android:layout_width="120dp" 
      android:layout_height="160dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_alignParentTop="true" 
      app:srcCompat="@drawable/avengers" /> 

     <TextView 
      android:id="@+id/tvMovie" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginStart="10dp" 
      android:layout_marginTop="8dp" 
      android:layout_toEndOf="@+id/ivIcon" 
      android:layout_toRightOf="@+id/ivIcon" 
      android:text="MovieName" 
      android:textSize="20sp"/> 

     <TextView 
      android:id="@+id/tvTagline" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/tvMovie" 
      android:layout_alignStart="@+id/tvMovie" 
      android:layout_below="@+id/tvMovie" 
      android:layout_marginTop="7dp" 
      android:text="Tag Line" /> 

     <TextView 
      android:id="@+id/tvYear" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/tvTagline" 
      android:layout_alignStart="@+id/tvTagline" 
      android:layout_below="@+id/tvTagline" 
      android:layout_marginTop="8dp" 
      android:text="Year" /> 

     <TextView 
      android:id="@+id/tvDuration" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/tvYear" 
      android:layout_alignStart="@+id/tvYear" 
      android:layout_below="@+id/tvYear" 
      android:layout_marginTop="10dp" 
      android:text="Duration" /> 

     <TextView 
      android:id="@+id/tvDirector" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/tvDuration" 
      android:layout_alignStart="@+id/tvDuration" 
      android:layout_below="@+id/tvDuration" 
      android:layout_marginTop="7dp" 
      android:text="Director" /> 

     <RatingBar 
      android:id="@+id/rbMovie" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/ivIcon" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="12dp" 
      android:stepSize="0.5" /> 

     <TextView 
      android:id="@+id/tvCast" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Cast" 
      android:textSize="15sp" 
      android:layout_centerVertical="true" 
      android:layout_toLeftOf="@+id/rbMovie" 
      android:layout_toStartOf="@+id/rbMovie" 
      android:layout_marginRight="11dp" 
      android:layout_marginEnd="11dp" /> 

     <TextView 
      android:id="@+id/tvStory" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:text="Story" 
      android:layout_below="@+id/tvCast" 
      android:layout_alignLeft="@+id/tvCast" 
      android:layout_alignStart="@+id/tvCast" /> 

    </RelativeLayout> 
+0

を変更してみてください私の更新ANS –

答えて

1

が、これはあなたのレイアウトファイル

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 


    <ImageView 
     android:id="@+id/ivIcon" 
     android:layout_width="200dp" 
     android:layout_height="100dp" 
     android:layout_gravity="center_horizontal|center" 
     android:layout_weight="1" 
     android:scaleType="fitXY" 
     android:src="@drawable/redfox" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/tvMovie" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="MovieName" 
      android:textSize="20sp" /> 

     <TextView 
      android:id="@+id/tvTagline" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Tag Line" /> 

     <TextView 
      android:id="@+id/tvYear" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Year" /> 

     <TextView 
      android:id="@+id/tvDuration" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Duration" /> 

     <TextView 
      android:id="@+id/tvDirector" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Director" /> 


     <TextView 
      android:id="@+id/tvCast" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Cast" 
      android:textSize="15sp" /> 

     <TextView 
      android:id="@+id/tvStory" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:text="Story" /> 

    </LinearLayout> 
</LinearLayout> 

<RatingBar 
    android:id="@+id/rbMovie" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:max="5" 
    android:stepSize="0.5" /> 

+0

はい、その作業を確認してください。しかし、レイアウト上の問題は何ですか?答えてくれてありがとう。 –