-1

我正在開發一個應用程序來顯示圖像和文本。當點擊該項目時,它會轉到另一個片段。列表顯示是正確的,但是當我點擊該項目時,它不會進入片段。我正在使用回收站適配器來列出項目。代碼如下所示。android值通過適配器片段

public class MyRecyclerAdapter extends RecyclerView.Adapter <MyRecyclerAdapter.MyViewHolder> { 

String categoryId; 

private List <NewsFeeds> feedsList; 
private Context context; 
private LayoutInflater inflater; 

public MyRecyclerAdapter(Context context, List <NewsFeeds> feedsList) { 

    this.context = context; 
    this.feedsList = feedsList; 
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View rootView = inflater.inflate(R.layout.singleitem_recyclerview_categories, parent, false); 
    return new MyViewHolder(rootView); 
} 

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 
    NewsFeeds feeds = feedsList.get(position); 
    //Pass the values of feeds object to Views 
    holder.title.setText(feeds.getName()); 
    //holder.categoryId.setText(feeds.getCategory_id()); 
    categoryId = feeds.getCategory_id(); 
    Log.d("LOGTAG", "id : " + categoryId); 
    holder.imageview.setImageUrl(feeds.getImgURL(), NetworkController.getInstance(context).getImageLoader()); 

    Log.d("LOGTAG", "feeds.getFeedName():" + feeds.getName()); 
} 

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

public class MyViewHolder extends RecyclerView.ViewHolder { 

    private TextView title; 
    private NetworkImageView imageview; 
    private CardView cardView; 


    public MyViewHolder(View itemView) { 
    super(itemView); 
    title = (TextView) itemView.findViewById(R.id.title_view); 
    //categoryId = (TextView) itemView.findViewById(R.id.content_view); 
    // Volley's NetworkImageView which will load Image from URL 
    imageview = (NetworkImageView) itemView.findViewById(R.id.thumbnail); 
    cardView = (CardView) itemView.findViewById(R.id.card_view); 

    cardView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    //Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show(); 



    // I want to send values to SubCategoryFragment and start SubCategoryFragment 
    Bundle args = new Bundle(); 
    args.putString("category_id", categoryId); 
    //set Fragmentclass Arguments 
    SubCategoryFragment fragobj = new SubCategoryFragment(); 
    fragobj.setArguments(args); 

    Log.d("LOGTAG", categoryId); 
    Log.d("LOGTAG", "clicked"); 



    //newInstance(categoryId); 



    } 
    }); 




    } 


} 


} 

我想發送值到SubCategoryFragment並啓動SubCategoryFragment。

我SubCategoryFragment代碼

public class SubCategoryFragment extends Fragment { 

public SubCategoryFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_sub_category, container, false); 
    //Bundle bundle = this.getArguments(); 


    Bundle args = getArguments(); 
    //String categoryId = args.getString("index"); 

    String categoryId = getArguments().getString("category_id"); 
    //String categoryId = getArguments().getString("category_id"); 
    TextView textView = (TextView) rootView.findViewById(R.id.label); 
    textView.setText(categoryId); 



    // Inflate the layout for this fragment 
    return rootView; 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
} 
} 

請幫我

回答

2

在你onClickListener();

Fragment fragment = new tasks(); 
FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.replace(R.id.content_frame, fragment); 
fragmentTransaction.addToBackStack(null); 
fragmentTransaction.commit(); 

變化R.id.content_frame與片段

+0

這顯示了兩個錯誤不能化解,不能化解getactivity –

+0

@faziltm你嘗試使用的getContext()或getApplicationContext()而不是getActivity()? –

+0

我嘗試過使用getContext()或getApplicationContext()而不是getActivity()。但它顯示無法解決任務()並且無法解析getApplicationContext() –

1

從適配器你意圖爲發送數據:

Fragment fragment = new tasks(); 
FragmentManager fragmentManager = context.getSupportFragmentManager(); // this is basically context of the class 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
Bundle bundle=new Bundle(); 
bundle.putString("name", "From Adapter"); //key and value 
//set Fragmentclass Arguments 
fragment.setArguments(bundle); 
fragmentTransaction.replace(R.id.content_frame, fragment); 
fragmentTransaction.addToBackStack(null); 
fragmentTransaction.commit(); 

和片段onCreateView方法:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    String strtext=getArguments().getString("name"); //fetching value by key 
    return inflater.inflate(R.layout.fragment, container, false); 
} 
+0

它顯示兩個錯誤無法解析並且無法解析getactivity –

+0

它顯示無法解析任務和getSupportFragmentManager。請幫助我。 –

0

您已經在onClick法犯了一個錯誤。

當你想要去一個片段到其他片段,你必須使用FragmentTransaction.class交易片段 請查看下面的代碼。

編輯

SecondFragment fragment = new SecondFragment(); 
    FragmentManager fragmentManager = currentfragment.getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      fragmentTransaction.add(R.id.content_frame, fragment); 
     fragmentTransaction.hide(currentfragment) fragmentTransaction.addToBackStack(currentfragment.getclass().getsimplename()); 
      fragmentTransaction.commit(); 

編輯

只要把下面的代碼在你的RecyclerViewAdapter方法onBindViewHolder。

holder.cardView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show(); 
      // I want to send values to SubCategoryFragment and start SubCategoryFragment 
      Bundle args = new Bundle(); 
      args.putString("category_id", categoryId); 
      //set Fragmentclass Arguments 
      SubCategoryFragment fragobj = new SubCategoryFragment(); 
      fragobj.setArguments(args); 
      FragmentManager fragmentManager = currentfragment.getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      fragmentTransaction.add(R.id.content_frame, fragobj); 
      fragmentTransaction.hide(currentfragment); 
     fragmentTransaction.addToBackStack(currentfragment.getclass().getsimplename()); 
      fragmentTransaction.commit(); 


      //newInstance(categoryId); 



     } 
    }); 

編輯

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> { 

    String categoryId; 

    private List<NewsFeeds> feedsList; 
    private Context context; 
    private LayoutInflater inflater; 
    private Fragment currentFragment; 

    public MyRecyclerAdapter(Context context, List<NewsFeeds> feedsList, final Fragment currentFragment) { 

     this.context = context; 
     this.feedsList = feedsList; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     this.currentFragment = currentFragment; 
    } 

    @Override 
    public MyRecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View rootView = inflater.inflate(R.layout.singleitem_recyclerview_categories, parent, false); 
     return new MyViewHolder(rootView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     NewsFeeds feeds = feedsList.get(position); 
     //Pass the values of feeds object to Views 
     holder.title.setText(feeds.getName()); 
     //holder.categoryId.setText(feeds.getCategory_id()); 


     holder.title.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show(); 
       // I want to send values to SubCategoryFragment and start SubCategoryFragment 
       Bundle args = new Bundle(); 
       args.putString("category_id", categoryId); 
       //set Fragmentclass Arguments 
       SubCategoryFragment fragobj = new SubCategoryFragment(); 
       fragobj.setArguments(args); 
       FragmentManager fragmentManager = currentfragment.getFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.add(R.id.content_frame, fragobj); 
       fragmentTransaction.hide(currentfragment); 
       fragmentTransaction.addToBackStack(currentfragment.getclass().getsimplename()); 
       fragmentTransaction.commit(); 


       //newInstance(categoryId); 


      } 
     }); 

    } 

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

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     private TextView title; 

     public MyViewHolder(View itemView) { 
      super(itemView); 
      title = (TextView) itemView.findViewById(R.id.title_view); 
     } 


    } 


} 
+0

它顯示無法解析獲取活動。 –

+0

是的,因爲你已經在適配器中編碼,所以你已經在適配器中傳遞了片段對象。 我已更新我的文章檢查 –

+0

它是否在RecyclerAdapter的onclick視圖中使用?當我在RecyclerAdapter onClick中使用時,它顯示了三個錯誤。 1.非靜態方法getFragmentManager不能從靜態上下文中引用。 2。表達式在fragmentTransaction.hide(CategoriesFragment)中除外。 3.無法解析方法getclass() –

0

這個替換您的onclick聽衆。

cardView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    //Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show(); 



    // I want to send values to SubCategoryFragment and start SubCategoryFragment 
    Bundle args = new Bundle(); 
    args.putString("category_id", categoryId); 
    //set Fragmentclass Arguments 
    SubCategoryFragment fragobj = new SubCategoryFragment(); 
    fragobj.setArguments(args); 

    Log.d("LOGTAG", categoryId); 
    Log.d("LOGTAG", "clicked"); 
    //put this in your code 
FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
      FragmentTransaction fragmentTransaction =   fragmentManager.beginTransaction(); 
      fragmentTransaction.replace(R.id.SubCategoryFragment, fragobj); 
      fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 


    //newInstance(categoryId); 



    } 
    });  
+0

只有一個錯誤無法解析方法getActivity() –

+0

只有一個錯誤::無法解析方法'getActivity()' –