2017-09-26 80 views
0

我創建回收視圖來顯示數據,之後我想通過 位置的數據到另一個活動,但是當我使用loge檢查位置是否合格時在logcat中找不到位置。通過改造活動傳遞數據

在MainActivity我添加靜態列表Recipe模型和創建方法返回此列表:

public class MainActivity extends AppCompatActivity { 

    private RecyclerView recyclerView; 
    private ProgressBar progressBar; 
    private LinearLayoutManager mLayoutManager; 
    private ArrayList<Model> list; 
    private RecyclerViewAdapter adapter; 
    private static List<Recipe>mListrecipe; 

    String url = "https://d17h27t6h515a5.cloudfront.net/"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
     progressBar = (ProgressBar) findViewById(R.id.progressbar); 

     mLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false); 
     recyclerView.setLayoutManager(mLayoutManager); 

     /// create a list-- 
     list = new ArrayList<>(); 
     /// get the list from json file 
       getRetrofitArray(); 

     adapter = new RecyclerViewAdapter(list, MainActivity.this); 
      // set adapter to recyclerview 
     recyclerView.setAdapter(adapter); 

    } 

     public void getRetrofitArray(){ 

      Retrofit retrofit = new Retrofit.Builder() 
           .baseUrl(url) 
           .addConverterFactory(GsonConverterFactory.create()) 
           .build(); 

      RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class); 
      Call<List<Recipe>> call = service.getRecipeDetails(); 
      call.enqueue(new Callback<List<Recipe>>() { 
       @Override 
       public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) { 
        Log.e("main ", " retrofit response "+ response.body().toString()); 
        mListrecipe=response.body(); 
        for (int i=0; i<response.body().size();i++){ 
         Log.e("main ", " name "+ response.body().get(i).getName() + " serving "+ 
          response.body().get(i).getServings()); 
         list.add(new Model(Model.IMAGE_TYPE,response.body(), response.body().get(i).getName() , 
           " Serving " +response.body().get(i).getServings()) ); 

        } 
        adapter.notifyDataSetChanged(); 

       } 

       @Override 
       public void onFailure(Call<List<Recipe>> call, Throwable t) { 
        Log.e("main ", " retrofit error "+ t.toString()); 
       } 
      }); 



     } 

public static List<Recipe>getList(){ 
    return mListrecipe; 
} 
} 

我添加putExtra與關鍵值傳遞位置

public class RecyclerViewAdapter extends RecyclerView.Adapter { 

    private ArrayList<Model> dataset; 
    private Context mContext; 
    public RecyclerViewAdapter(ArrayList<Model> mlist, Context context) { 
     this.dataset = mlist; 
     this.mContext = context; 
    } 

    public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{ 

      ImageView imageView; 
     TextView title, subtitle; 
     public ImageTypeViewHolder(View itemView) { 
      super(itemView); 

      this.title = (TextView) itemView.findViewById(R.id.title); 
      this.subtitle = (TextView) itemView.findViewById(R.id.subtitle); 
      this.imageView=(ImageView)itemView.findViewById(R.id.imageview); 


     } 
    } 
    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlist, parent, false); 
     return new ImageTypeViewHolder(view) ; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { 
     Model object = dataset.get(position); 

     ((ImageTypeViewHolder) holder).title.setText(object.title); 
     ((ImageTypeViewHolder) holder).subtitle.setText(object.subtitle); 
     /// dataset.get(position) 
     ((ImageTypeViewHolder) holder).title.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent=new Intent(mContext,SelectReceipe.class); 
       intent.putExtra("itempostion",position); 
       mContext.startActivity(intent); 
      } 
     }); 
     ((ImageTypeViewHolder) holder).subtitle.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent=new Intent(mContext,SelectReceipe.class); 
       intent.putExtra("itempostion",position); 

       mContext.startActivity(intent); 
      } 
     }); 
     ((ImageTypeViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent=new Intent(mContext,SelectReceipe.class); 
       intent.putExtra("itempostion",position); 

       mContext.startActivity(intent); 
      } 
     }); 

    } 

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

這是2活動我通過了位置但我找不到價值logcat

public class SelectReceipe extends AppCompatActivity {   
    List<Recipe>sListRecipe; 
    public int itempostion; 
    private String TAG; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.selectreceipe); 

     sListRecipe=MainActivity.getList(); 
     Intent i =getIntent(); 
     itempostion= i.getExtras().getInt("itempostion"); 
     Log.e(TAG,"itempostion"+String.valueOf(itempostion)+"valou"+sListRecipe.get(itempostion).getName().toString()); 

    } 

的logcat:

09-26 13:34:41.355 31852-31852/? D/dalvikvm: Late-enabling CheckJNI 
    09-26 13:34:42.459 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts 
    09-26 13:34:42.471 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve interface method 20343: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V 
    09-26 13:34:42.471 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
    09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
    09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
    09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve interface method 20345: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
    09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
    09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
    09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve interface method 20349: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
    09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
    09-26 13:34:42.483 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
    09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 478: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
    09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
    09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
    09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 500: Landroid/content/res/TypedArray;.getType (I)I 
    09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008 
    09-26 13:34:42.695 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild 
    09-26 13:34:42.695 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 20822: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
    09-26 13:34:42.699 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002 
    09-26 13:34:42.735 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged 
    09-26 13:34:42.735 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 20233: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V 
    09-26 13:34:42.735 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007 
    09-26 13:34:42.739 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList 
    09-26 13:34:42.743 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 292: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList; 
    09-26 13:34:42.743 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 
    09-26 13:34:42.763 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable 
    09-26 13:34:42.763 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 441: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
    09-26 13:34:42.763 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
    09-26 13:34:42.763 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 
    09-26 13:34:42.767 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 443: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
    09-26 13:34:42.767 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
    09-26 13:34:42.771 31852-31852/blueappsoftware.mybakingtips E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 
    09-26 13:34:42.771 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve instanceof 142 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper; 
    09-26 13:34:42.771 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c 
    09-26 13:34:42.815 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 256K, 3% free 10939K/11271K, paused 18ms+1ms, total 27ms 
    09-26 13:34:42.815 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 4ms 
    09-26 13:34:42.911 31852-31852/blueappsoftware.mybakingtips D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so 

                      [ 09-26 13:34:42.931 31852:31852 D/   ] 
                      HostConnection::get() New Host Connection established 0xb7f81e90, tid 31852 
    09-26 13:34:43.019 31852-31852/blueappsoftware.mybakingtips D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so 
    09-26 13:34:43.035 31852-31852/blueappsoftware.mybakingtips D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so 
    09-26 13:34:43.111 31852-31852/blueappsoftware.mybakingtips W/EGL_genymotion: eglSurfaceAttrib not implemented 
    09-26 13:34:43.127 31852-31852/blueappsoftware.mybakingtips D/OpenGLRenderer: Enabling debug mode 0 
    09-26 13:34:43.183 31852-31852/blueappsoftware.mybakingtips D/OpenGLRenderer: TextureCache::get: create texture(0xb7fa1438): name, size, mSize = 1, 4096, 4096 
    09-26 13:34:43.339 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature (Ljava/nio/file/Path;) 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;) 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips I/dalvikvm: Could not find method java.nio.file.Files.newOutputStream, referenced from method okio.Okio.sink 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve static method 22787: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream; 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x71 at 0x000a 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature (Ljava/nio/file/Path;) 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;) 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips I/dalvikvm: Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve static method 22786: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream; 
    09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x71 at 0x000a 
    09-26 13:34:43.567 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 279K, 4% free 11121K/11527K, paused 24ms+10ms, total 38ms 
    09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: retrofit response [[email protected], [email protected], [email protected], [email protected]] 
    09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: name Nutella Pie serving 8 
    09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: name Brownies serving 8 
    09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: name Yellow Cake serving 8 
    09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: name Cheesecake serving 8 
    09-26 13:34:44.119 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 217K, 4% free 11298K/11655K, paused 15ms+0ms, total 20ms 
    09-26 13:34:44.179 31852-31852/blueappsoftware.mybakingtips D/OpenGLRenderer: TextureCache::get: create texture(0xb7f36950): name, size, mSize = 9, 33856, 37952 
    09-26 13:34:48.955 31852-31852/blueappsoftware.mybakingtips W/EGL_genymotion: eglSurfaceAttrib not implemented 
    09-26 13:34:48.955 31852-31852/blueappsoftware.mybakingtips E/RecyclerView: No adapter attached; skipping layout 
    09-26 13:34:48.967 31852-31852/blueappsoftware.mybakingtips E/RecyclerView: No adapter attached; skipping layout 
    09-26 13:52:03.367 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 332K, 4% free 11375K/11847K, paused 6ms+1ms, total 16ms 
    09-26 14:16:49.627 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 426K, 5% free 11345K/11911K, paused 9ms+0ms, total 9ms 

回答

0

你可能沒有得到,因爲你正在使用來自不同Activity.Instead吸氣列表中,您可以通過意向通過你的清單,但對於這一點,你應該實現你的類作爲Parcelable或Serializable.Here是一個很好的職位,How can I make my custom objects Parcelable?有一件事,如果你必須加載並暫時保存一個列表,以便您可以在App中的任何地方使用,那麼你可以在Android應用程序類中使用setter和getter.Here是對於那個不錯的帖子Using the Android Application class to persist data