ste9206 ste9206 - 6 months ago 55
Android Question

How to get the correct Item from recyclerView getAdapter( )?

XML File for recycle view:

<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" />




this is my recyclerView adapter:

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);

}

}

}


and this is what I want to do (in fragment class):

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;


my question is: how could I return the value from editText(in recyclerView) to the fragment (in this if -else if statement)?
Thanks

Answer

You can get it by using below code.

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

Or if you want to maintain it dynamically then you can write this without if condition as it will show in for loop ine by one as below .

   @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();
       }
   }