gpt4 book ai didi

android - 如何从 recyclerView getAdapter( ) 获取正确的 Item?

转载 作者:行者123 更新时间:2023-11-29 14:46:03 24 4
gpt4 key购买 nike

用于回收 View 的 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);

}

}

}

这就是我想要做的(在 fragment 类中):

  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 中)返回到 fragment (在这个 if -else if 语句中)?谢谢

最佳答案

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

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

或者如果你想动态地维护它,那么你可以在没有 if 条件的情况下编写它,因为它将在 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();
}
}

关于android - 如何从 recyclerView getAdapter( ) 获取正确的 Item?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37480548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com