不能去适配器碎片

时间:2017-06-13 20:03:14

标签: java android android-fragments

我有活动,这个活动是可绘制的菜单。 在drawable中,我包含一些片段,在我的片段中我用适配器显示一些数据。我尝试在单击数据时执行操作,然后它将转到其他片段。哦是的,我尝试从我的适配器发送一些值到片段。 请帮帮我

这是我的适配器..

package com.skripsi.anna.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.skripsi.anna.MenuEdit;
import com.skripsi.anna.R;
import com.skripsi.anna.model.ModelMenu;

import java.util.List;

/**
 * Created by gandhi on 6/11/17.
 */

public class BarangEditor extends RecyclerView.Adapter<BarangEditor.ViewHolder> {

private List<ModelMenu> modelMenus;
private Context context;

public BarangEditor(List<ModelMenu> modelMenus){
    this.modelMenus = modelMenus;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView namaBarang,hargaBarang;
    public ViewHolder(View v) {
        super(v);
        namaBarang = (TextView) v.findViewById(R.id.nama_barang);
        hargaBarang = (TextView) v.findViewById(R.id.harga);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                ModelMenu modelMenu = modelMenus.get(position);
                MenuEdit menuEdit = new MenuEdit();
                menuEdit.newInstance(modelMenu.getIdMenu(),modelMenu.getNamaMenu(),modelMenu.getHargaMenu());
// i want to send this value to fragment

            }
        });
    }
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_barang_editor, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ModelMenu modelMenu = modelMenus.get(position);
    holder.hargaBarang.setText(modelMenu.getHargaMenu());
    holder.namaBarang.setText(modelMenu.getNamaMenu());

}

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



}

这是我的片段

package com.skripsi.anna;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MenuEdit extends Fragment {


private String idMenu,namaMenu,hargaMenu;

private OnFragmentInteractionListener mListener;

public MenuEdit() {
}
public static MenuEdit newInstance(String idMenu, String namaMenu, String hargaMenu) {
    MenuEdit fragment = new MenuEdit();
    Bundle args = new Bundle();
    args.putString("idMenu", idMenu);
    args.putString("namaMenu", namaMenu);
    args.putString("hargaMenu",hargaMenu);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        idMenu = getArguments().getString("idMenu");
        namaMenu = getArguments().getString("namaMenu");
        hargaMenu = getArguments().getString("hargaMenu");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu_edit, container, false);


    return view;
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}
public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}
}

我试过这个

FragmentTransaction ft = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();

但它使我的应用程序FC 它不起作用

2 个答案:

答案 0 :(得分:0)

我使用此代码,我希望为你工作

public void addFragment(Fragment fragment, boolean addToBackStack, String tag)
{
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();

    if (addToBackStack)
    {
        ft.addToBackStack(tag);
    }
    ft.replace(R.id.content_frame, fragment, tag);
    try{
        ft.commitAllowingStateLoss();
    }catch (Exception e){

    }
}

答案 1 :(得分:0)

[解决]

我尝试将此代码放在我的适配器上

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, menuEdit).commit();

但它说不能解决getSupportFragment 所以它是真正的问题,对于解决方案,我写了一些这样的代码

AppCompatActivity activity = (AppCompatActivity) v.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.flContent, menuEdit).addToBackStack(null).commit();

及其工作 它现在是我的完整适配器

package com.skripsi.anna.adapter;

import android.os.Bundle;
import android.support.v4.app.FragmentController;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.skripsi.anna.MenuEdit;
import com.skripsi.anna.R;
import com.skripsi.anna.model.ModelMenu;

import java.util.List;

/**
* Created by gandhi on 6/11/17.
*/

public class BarangEditor extends RecyclerView.Adapter<BarangEditor.ViewHolder> {

private List<ModelMenu> modelMenus;

public BarangEditor(List<ModelMenu> modelMenus){
    this.modelMenus = modelMenus;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView namaBarang,hargaBarang;
    public ViewHolder(View v) {
        super(v);
        namaBarang = (TextView) v.findViewById(R.id.nama_barang);
        hargaBarang = (TextView) v.findViewById(R.id.harga);
        v.setOnClickListener(new View.OnClickListener() {
            public FragmentController appCompatActivity;

            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                ModelMenu modelMenu = modelMenus.get(position);
                MenuEdit menuEdit = new MenuEdit();
                Bundle bundle = new Bundle();
                bundle.putString("idMenu",modelMenu.getIdMenu());
                menuEdit.setArguments(bundle);
                AppCompatActivity activity = (AppCompatActivity) v.getContext();
                activity.getSupportFragmentManager().beginTransaction().replace(R.id.flContent, menuEdit).addToBackStack(null).commit();
            }
        });
    }
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_barang_editor, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ModelMenu modelMenu = modelMenus.get(position);
    holder.hargaBarang.setText(modelMenu.getHargaMenu());
    holder.namaBarang.setText(modelMenu.getNamaMenu());

}

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



}