我有一个 recyclerview ,我设计了一个在适配器中充气的行,我想抓住微调器的id来执行一些操作这是我无法做到的。 这是我的 ApolloActivity.java ,我试图在我想要微调器的id的fab的 onClick()中给注销器视图充气。
private List<ApolloPharmacyEntity> entity = null;
private RecyclerView rv;
private FloatingActionButton fab;
Spinner spinner;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setTitle("Apollo Pharmacy");
setContentView(R.layout.activity_apollo);
rv=(RecyclerView)findViewById(R.id.recyclerview2);
Log.i("TAG","recycler view"+rv);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Context context=view.getContext();
LayoutInflater inflater = null;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.apollopharmacy_listrow,null);
spinner = (Spinner)view.findViewById(R.id.spinner);//this is becoming null
String Text = spinner.getSelectedItem().toString();
Log.i("TAG","spinnervalue"+spinner);
Intent intent = new Intent(Apollo.this, Cart.class);
startActivity(intent);
}
});
initializeData(); initializeAdapter();
`
这是我的适配器
public static class EntityViewHolder extends RecyclerView.ViewHolder {
TextView tabName;
TextView gobacktomenutv;
Spinner spinner;
String[] itemCount = { "1", "2", "3", "4","5","6","7","8","10"};
RelativeLayout addrl;
Dialog dialog;
EntityViewHolder( View itemView) {
super(itemView);
spinner = (Spinner)itemView.findViewById(R.id.spinner);
tabName = (TextView)itemView.findViewById(R.id.textView4);
ArrayAdapter aa = new ArrayAdapter(itemView.getContext(), android.R.layout.simple_spinner_dropdown_item, itemCount);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(aa);
}
}
List<ApolloPharmacyEntity> details;
ApolloPharmacyAdapter(List<ApolloPharmacyEntity> details){
this.details = details;
}
@Override
public ApolloPharmacyAdapter.EntityViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.apollopharmacy_listrow, viewGroup, false);
ApolloPharmacyAdapter.EntityViewHolder pvh = new ApolloPharmacyAdapter.EntityViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(ApolloPharmacyAdapter.EntityViewHolder holder, int position) {
holder.tabName.setText(details.get(position).getTabName());
}
@Override
public int getItemCount() {
return details.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
apollopharmacy_listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayout3"
android:paddingLeft="20dp"
android:layout_below="@id/relativelayout2">
<TextView
android:text="ASPIRIN-250 mg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:textColor="#224e6d"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/textView4" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/relativelayout3"
android:paddingTop="10dp"
android:paddingLeft="30dp"
>
<TextView
android:text="6 tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#224e6d"
android:textSize="26sp"
android:textStyle="bold"
android:id="@+id/textView6" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:layout_width="90dp"
android:layout_height="2dp"
android:background="#c0c0c0"
android:layout_marginTop="40dp"/>
</RelativeLayout>
<TextView
android:text="1 STRIP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#224e6d"
android:layout_marginTop="50dp"
android:textStyle="bold"
android:textSize="26sp"
android:id="@+id/textView7" />
<RelativeLayout
android:layout_width="100dp"
android:layout_height="45dp"
android:background="@drawable/add_rounded"
android:id="@+id/relativeLayout"
android:layout_marginBottom="15dp"
android:layout_alignBottom="@+id/textView7"
android:layout_alignParentEnd="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Transfer"
android:text="ADD"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/relativelayout3"
>
<TextView
android:text="₹"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:textSize="34sp"
android:textColor="#82BCB4"
android:layout_marginStart="200dp"
android:id="@+id/textView8" />
<TextView
android:text="26"
android:layout_width="wrap_content"
android:layout_height="80sp"
android:layout_marginTop="25dp"
android:textSize="50sp"
android:textColor="#224e6d"
android:textStyle="bold|normal"
android:layout_marginStart="230dp"
android:id="@+id/textView9" />
<Spinner
android:layout_width="55dp"
android:layout_height="55dp"
android:background="@drawable/spinner_outline"
android:id="@+id/spinner"
android:layout_marginEnd="30dp"
android:layout_alignBottom="@+id/textView8"
android:layout_alignEnd="@+id/textView8"
android:layout_marginBottom="9dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_star_border_black_24px"
android:layout_marginTop="15dp"
android:layout_marginEnd="12dp"
android:id="@+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:0)
在您的 ApolloActivity.java 的onCreate()
方法中,您尚未将ApolloPharmacyAdapter
设置为您的RecyclerView。因此apollopharmacy_listrow.xml
的布局不会膨胀。
请使用rv.setAdapter()
onCreate()
方法
此外,如果没有找到listrow的详细信息,RecyclerView将不会给listrow充气。在onCreate()中不能像这样使用微调器,因为根据RecyclerView中的行数,可能有多个Spinner实例。
您必须指定要从您的Activity中引用哪个微调器。
如果您通过向下一个Activity发送微调项来详细说明您想要实现的目标,我可以提供帮助。