如何从recyclerView getAdapter()获取正确的项目?

时间:2016-05-27 10:02:15

标签: android android-fragments android-recyclerview

循环视图的XML文件:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"

  <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerAircraftAdd"
    android:background="#FFF" />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SALVA AEREO"
    android:id="@+id/salvaereo"

    android:textColor="#fff"
    android:background="#29B6F6"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

这是我的recyclerView适配器:

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


    private static final int LOADING_IMAGE = 0;
    private static final int SELECT_PICTURE = 111;
    private static final int DETAIL_AIRCRAFT = 1;
    private static final int DESCRIPTION = 2;
    public static Bitmap bmp;
    private Context context;
    private String imm,mod,man,cat,cl,vel,auto,cons,pos,cost;

    private ArrayList<String> arrayList;

    public AircraftAdapter(Context context,ArrayList<String> aryLstMyCarModel)
    {
        this.context = context;
        this.arrayList = aryLstMyCarModel;
    }



    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view;
      //  context = parent.getContext();

        if(viewType == LOADING_IMAGE)
        {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_aircraft_photo, parent, false);
            ViewSimple simpleView = new ViewSimple(view);
            return simpleView;
        }
        else if(viewType == DETAIL_AIRCRAFT)
        {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_aircraft_details, parent, false);
            DetailAircraftViewHolder detailAircraftViewHolder = new DetailAircraftViewHolder(view);
            return detailAircraftViewHolder;
        }
        else
        {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_description, parent, false);
            DescriptionViewHolder descriptionViewHolder = new DescriptionViewHolder(view);
            return descriptionViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {

        if(getItemViewType(position)==LOADING_IMAGE)
        {
            ViewSimple mHolder = (ViewSimple)holder;
            if (arrayList.size() != 0) {


                File f = new File(arrayList.get(position));
                Log.d("ssasf",arrayList.get(position));



                Picasso.with(context).load("file:///"+arrayList.get(position)).resize(600,600).into(mHolder.image, new Callback() {
                    @Override
                    public void onSuccess()
                    {
                        Log.d("sd","done");
                    }

                    @Override
                    public void onError()
                    {
                       Log.d("er","error");
                    }
                });

            }

        }

        else if(getItemViewType(position)==DETAIL_AIRCRAFT)
        {
            DetailAircraftViewHolder mHolder = (DetailAircraftViewHolder) holder;


        }
        else if(getItemViewType(position)==DESCRIPTION)
        {
            DescriptionViewHolder mHolder = (DescriptionViewHolder)holder;


        }

    }

    @Override
    public int getItemCount()
    {
        return 3;
    }

    @Override
    public int getItemViewType(int position)
    {
        if(position == 0)
            return LOADING_IMAGE;

        else if(position ==1)
            return DETAIL_AIRCRAFT;
        else
            return DESCRIPTION;

    }

    private class ViewSimple extends RecyclerView.ViewHolder
    {
        ImageView button,image;
        public ViewSimple(View itemView)
        {
            super(itemView);
            button = (ImageView)itemView.findViewById(R.id.imgAdding);
            image = (ImageView)itemView.findViewById(R.id.aircfraftImg);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    FragmentActivity activity = (FragmentActivity)context;
                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    activity.startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_PICTURE);

                }
            });

        }
    }


    public class DetailAircraftViewHolder extends RecyclerView.ViewHolder
    {
        EditText immatricolazione, modello, manifacturer,categoria,classe;
        EditText velocita,autonomia,consumo,posti,costorario;

        public DetailAircraftViewHolder(View itemView)
        {
            super(itemView);
            immatricolazione=(EditText)itemView.findViewById(R.id.immatricolazione);
            modello = (EditText)itemView.findViewById(R.id.modello);
            manifacturer = (EditText)itemView.findViewById(R.id.manifacturer);
            categoria = (EditText)itemView.findViewById(R.id.categoria);
            classe = (EditText)itemView.findViewById(R.id.classe);
            velocita = (EditText)itemView.findViewById(R.id.velocita);
            autonomia = (EditText)itemView.findViewById(R.id.autonomia);
            consumo = (EditText)itemView.findViewById(R.id.consumo);
            posti = (EditText)itemView.findViewById(R.id.postitotali);
            costorario = (EditText)itemView.findViewById(R.id.costorario);

            if(pos != null)
             Log.d("Posto",pos);

        }
    }

    public class DescriptionViewHolder extends RecyclerView.ViewHolder
    {
        public DescriptionViewHolder(View itemView)
        {
            super(itemView);

        }

    }

    }

这就是我想要做的事情(在片段类中):

  public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{

    View rootView =  inflater.inflate(R.layout.fragment_add_aircraft, container, false);

    recyclerView=(RecyclerView)rootView.findViewById(R.id.recyclerAircraftAdd);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    adapter = new AircraftAdapter(getActivity(),path);
    recyclerView.setAdapter(adapter);
    Button button = (Button)rootView.findViewById(R.id.salvaereo);

    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
          adapter = (AircraftAdapter)recyclerView.getAdapter();

           for(int i = 0;i<adapter.getItemCount(); i++)
           {
             if (i== 0)  //first card
             {


             }
             else if (i ==1) //second card
             {

             }
             else if(i==2) //third card
             {

             }


           }

        }
    });

    return rootView;

我的问题是:我怎样才能将editText(在recyclerView中)的值返回到片段(在这个if -else if语句中)? 谢谢

2 个答案:

答案 0 :(得分:3)

您可以使用以下代码来获取它。

   if(i == 0)
   {
         View selectedView = recyclerView.getChildAt(i);
         EditText modello = (EditText)selectedView.findViewById(R.id.modello);
         String strModello = modello.getEditableText().toString();
   }

或者如果你想动态地维护它,那么你可以在没有条件的情况下编写它,因为它将在for循环中显示如下所示。

   @Override
   public void onClick(View v)
   {
      adapter = (AircraftAdapter)recyclerView.getAdapter();

       for(int i = 0;i<adapter.getItemCount(); i++)
       {
           View selectedView = adapter.getChildAt(i);
           EditText modello = (EditText)selectedView.findViewById(R.id.modello);
           String strModello = modello.getEditableText().toString();
       }
   } 

答案 1 :(得分:2)

您需要在适配器中创建一个接口,例如:

public interface MyInterface {
    void onEditTextClicked();
}

将您的界面添加到您的适配器:

private MyInterface = mMyInterface; 

在你的构造函数中:

public AircraftAdapter(Context context, ArrayList<String> aryLstMyCarModel, MyInterface myInterface) {
    this.context = context;
    this.arrayList = aryLstMyCarModel;
    mInterface = myInterface;
}

在你的onClick添加:

mInterface.onEditTextClicked();

在您的片段中,您需要实现新界面。