2016-11-22 14 views
-1

私は2つのアクティビティ:MainActivityとDetailActivityを持っています。オプションメニューでアクティビティを開始した後に戻るボタン

最初に、AppはMainActivityに起動されます。

パブリッククラスMainActivityがAppCompatActivity {

private ArrayList<Movie> stringArrayList ; 
private RecyclerView mRecyclerView; 
private MovieData mAdapter; 
private String name = "popular"; 
private String string ; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    mRecyclerView = (RecyclerView)findViewById(R.id.recycler_view); 
    stringArrayList = new ArrayList<>(); 

} 

@Override 
protected void onStart() { 
    super.onStart(); 
    Log.v("LOG_V","onStart() is called"); 
    string = name ; 
    updateMovie(string); 
} 


public void updateMovie(String s) 
{ 
    new MovieAsyncTask().execute(s); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_list,menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    switch (id) 
    { 
     case R.id.top_rated : 
      name = "top_rated"; 
      string = name; 
      updateMovie(name); 
      break; 
     case R.id.popular_movie : 
      name = "popular"; 
      string = name; 
      updateMovie(name); 
      break; 
     case R.id.about : 
      Toast.makeText(this,"About",Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.upComing_movie : 
      name = "upcoming"; 
      string = name; 
      updateMovie(name); 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public class MovieAsyncTask extends AsyncTask<String,Void,ArrayList<Movie>> 
{ 
    @Override 
    protected ArrayList<Movie> doInBackground(String... params) { 
     HttpURLConnection httpURLConnection = null; 
     InputStream inputStream ; 
     String jsonString ; 

     BufferedReader bufferedReader = null; 


     final String MOVIE_BASE_URL = "http://api.themoviedb.org/3/movie/"+params[0]+"?"; 

     final String APPID_PARAM = "api_key"; 

     try { 

      Uri uri = Uri.parse(MOVIE_BASE_URL).buildUpon() 
        .appendQueryParameter(APPID_PARAM,BuildConfig.OPEN_MOVIE_API_KEY).build(); 


      URL url = new URL(uri.toString()); 
      Log.v("URL : ",uri.toString()); 

      httpURLConnection = (HttpURLConnection)url.openConnection(); 
      httpURLConnection.setRequestMethod("GET"); 
      httpURLConnection.connect(); 

      inputStream = httpURLConnection.getInputStream(); 
      bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      StringBuffer stringBuffer = new StringBuffer(); 

      String line ; 
      while ((line=bufferedReader.readLine())!=null) 
      { 
       stringBuffer.append(line+"\n"); 
      } 
      if(stringBuffer.length()==0) 
      { 
       return null; 
      } 

      jsonString = stringBuffer.toString(); 

     }catch (IOException I) 
     { 
      Log.e("LOG_TAG","Error"); 
      return null; 
     }finally { 
      if(httpURLConnection!=null) 
      { 
       httpURLConnection.disconnect(); 
      } 
      if(bufferedReader!=null) 
      { 
       try { 
        bufferedReader.close(); 
       }catch (IOException i) 
       { 
        i.printStackTrace(); 
       } 

      } 

     } 
     try { 
      return getMovieDataFromJson(jsonString); 
     }catch (JSONException e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public ArrayList<Movie> getMovieDataFromJson(String json) throws JSONException 
    { 
     ArrayList<Movie> arrayList = new ArrayList<>(); 
     final String RESULTS = "results"; 
     final String POSTER_BASE_URL = "http://image.tmdb.org/t/p/w185"; 
     final String POSTER_PATH = "poster_path"; 
     final String VOTE_AVERAGE = "vote_average"; 
     final String BACKDROP_URL = "backdrop_path"; 
     final String RELEASE_DATE = "release_date"; 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray jsonArray = jsonObject.getJSONArray(RESULTS); 
     Log.v("jsonArray Size :",jsonArray.length()+""); 
     for(int i=0;i<jsonArray.length();i++) 
     { 
      JSONObject jsonObject1 = jsonArray.getJSONObject(i); 
      String posterUrl = POSTER_BASE_URL+jsonObject1.getString(POSTER_PATH); 
      String vote = jsonObject1.getString(VOTE_AVERAGE); 
      String date = jsonObject1.getString(RELEASE_DATE); 
      String backImageUrl = jsonObject1.getString(BACKDROP_URL); 
      Log.v("Poster Url" , posterUrl); 
      arrayList.add(new Movie(vote,date,posterUrl,backImageUrl)); 
     } 

     return arrayList; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<Movie> arrayList) { 
     stringArrayList = arrayList ; 
     if(arrayList!=null) 
     { 
      mAdapter = new MovieData(getApplicationContext(),stringArrayList); 
      mRecyclerView.setAdapter(mAdapter); 
      mAdapter.notifyDataSetChanged(); 
      mRecyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2)); 
     } 
     super.onPostExecute(arrayList); 
    } 
} 

}

Recycler View Adapter 

パブリッククラスMovieDataがRecyclerView.Adapter {

public ArrayList<Movie> movieArrayList ; 
public Context mContext ; 

public class viewHolder extends RecyclerView.ViewHolder 
{ 

    ImageView imageView ; 
    TextView ratingView,releaseView ; 
    public viewHolder(View itemView) { 
     super(itemView); 
     imageView = (ImageView)itemView.findViewById(R.id.image_view); 
     ratingView = (TextView)itemView.findViewById(R.id.rating_view); 
     releaseView = (TextView)itemView.findViewById(R.id.release_date); 

    } 

} 

public MovieData(Context context , ArrayList<Movie> arrayList) { 
    movieArrayList = arrayList; 
    mContext = context ; 
} 

@Override 
public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(mContext).inflate(R.layout.content_main,parent,false); 

    return new viewHolder(view); 
} 

@Override 
public void onBindViewHolder(viewHolder holder, final int position) { 
    final Movie movie = movieArrayList.get(position); 
    final String u = "https://image.tmdb.org/t/p/w500"+movie.getBackdropUrl(); 
    Log.v("LOG_TAG",u); 
    Picasso.with(mContext).load(movie.getPosterUrl()).into(holder.imageView); 
    holder.releaseView.setText("Release : "+movie.getmReleaseDate()); 
    holder.ratingView.setText("Rating : "+movie.getmRating()+"/10"); 
    holder.imageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(v.getContext(),DetailActivity.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.putExtra(Intent.EXTRA_TEXT,u); 
      v.getContext().startActivity(intent); 
     } 
    }); 
} 

@Override 
public int getItemCount() { 
    return movieArrayList.size(); 
} 

}

Detail Activity 
0を拡張を拡張の

パブリッククラスDetailActivityはAppCompatActivity {

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

    Toolbar toolbar = (Toolbar)findViewById(R.id.detail_toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    Intent intent = getIntent(); 
    url = intent.getStringExtra(Intent.EXTRA_TEXT); 
    ImageView imageView = (ImageView)findViewById(R.id.backdrop_image); 
    Picasso.with(this).load(url).into(imageView); 

    FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.float_button); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show(); 
     } 
    }); 

} 

}

を拡張する方法ができます私の詳細アクティビティからメニューで選択された映画のリストにカムバック。

+0

あなたの質問を理解できませんでした。 DetailActivityからMainActivityに移動しようとしていますか? –

答えて

0

MainActivityに戻るボタンを押して戻りたい場合は、onBackPressed()メソッドを実装する必要があります。詳細here。エルス

あなたはちょうどあなたが現在の活動を終了し、スタックに戻っ活動に取得したいDetailActivityクラス内のどこthis.finish()を呼び出すことができます。

関連する問題