我正在使用回收站视图来列出服务器的报价。在回收器适配器中,我试图通过单击或点击相应的行来获取价格记录的ID。在onCreateViewHolder里面。当点击保存行的相对布局时,我总是获得相同的ID(这是服务器返回的第一个ID),尽管我从服务器正确地获取了所有行。造成这种情况的原因。是缓存吗?以及如何解决它。
这是我的代码
@Override
public ListViewPriceListRowHolder onCreateViewHolder(final ViewGroup viewGroup, int position) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.text_fragment, viewGroup, false);
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.price_list_row,null);
ListViewPriceListRowHolder holder = new ListViewPriceListRowHolder(v);
holder.rellayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
priceID = (TextView)viewGroup.findViewById(R.id.tv_price_row);
String priceIDStr = priceID.getText().toString();
//here I am getting the same price id every time I click on diiferent row
Toast.makeText(mContext,priceIDStr,Toast.LENGTH_SHORT).show();
}
});
// return new ListViewRowHolder(itemView);
return holder;
}
我的ViewHolder
package com.mdawaina.helper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.volley.toolbox.NetworkImageView;
import com.mdawaina.swftcc.R;
public class ListViewPriceListRowHolder extends RecyclerView.ViewHolder {
public NetworkImageView thumbnail ;
public TextView priceID;
public TextView orderID;
public TextView SPName;
public TextView catID;
public TextView price;
public TextView orderDetailsSuply;
public TextView approve;
public RelativeLayout rellayout;
public ListViewPriceListRowHolder(View view){
super(view);
this.priceID = (TextView) view.findViewById(R.id.tv_price_id_row);
this.orderID = (TextView) view.findViewById(R.id.tv_order_id_row);
this.SPName = (TextView) view.findViewById(R.id.tv_sp_name_row);
this.catID = (TextView)view.findViewById(R.id.tv_cat_id_row);
this.price = (TextView)view.findViewById(R.id.tv_price_row);
this.orderID = (TextView)view.findViewById(R.id.tv_order_id_row);
this.orderDetailsSuply = (TextView)view.findViewById(R.id.tv_orderdetailsuplly_row);
this.approve = (TextView)view.findViewById(R.id.tv_approve_row);
this.rellayout = (RelativeLayout) view.findViewById(R.id.pl_rellayout);
view.setClickable(true);
}
}
我的行布局
<?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="match_parent"
android:padding="20dp"
android:id="@+id/pl_rellayout"
android:background="#FFF"
>
<com.android.volley.toolbox.NetworkImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/networkImage"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The Title Goes Here"
android:textColor="@color/blue"
android:textSize="18dp"
android:maxLines="2"
android:id="@+id/tv_sp_name_row"
android:paddingEnd="5dp"
android:paddingRight="5dp"
android:textColorHighlight="@color/colorPrimary"
android:shadowColor="@color/colorPrimaryDark"
android:shadowDx="3.4"
android:shadowDy="2.5"
android:layout_marginLeft="130dp"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/tv_price_row"
android:layout_alignEnd="@+id/tv_price_row" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="14500000"
android:id="@+id/tv_price_row"
android:layout_alignBottom="@+id/networkImage"
android:layout_alignRight="@id/location_icon_row"
android:layout_alignEnd="@id/location_icon_row"
android:layout_marginRight="35dp"
android:layout_marginBottom="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="التفاصيل"
android:id="@+id/tv_orderdetailsuplly_row"
android:maxLines="10"
android:layout_below="@+id/tv_price_row"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:layout_marginRight="35dp"
android:layout_marginLeft="10dp"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="قبول العرض"
android:id="@+id/btnAcceptOffer"
android:background="@color/lightBlue"
android:textColor="@color/white"
android:layout_below="@+id/tv_orderdetailsuplly_row"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="35dp"
android:layout_marginLeft="10dp"
android:shadowRadius="@integer/google_play_services_version"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL"
android:id="@+id/tv_cat_id_row"
android:maxLines="0"
android:layout_alignBottom="@+id/networkImage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="35dp"
android:layout_marginStart="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL"
android:id="@+id/tv_order_id_row"
android:maxLines="0"
android:layout_alignBottom="@+id/networkImage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="35dp"
android:layout_marginStart="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL"
android:id="@+id/tv_price_id_row"
android:maxLines="0"
android:layout_alignBottom="@+id/networkImage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="35dp"
android:layout_marginStart="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL"
android:id="@+id/tv_approve_row"
android:maxLines="0"
android:layout_alignBottom="@+id/networkImage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="35dp"
android:layout_marginStart="35dp" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/location_icon_row"
android:src="@drawable/ic_price_tag"
android:layout_alignBottom="@+id/networkImage"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/details_icon_row"
android:src="@drawable/ic_order_long_text"
android:layout_below="@+id/location_icon_row"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
答案 0 :(得分:0)
OnCreateViewHolder我相信每个不同类型的视图持有者只有在被添加到列表中时才会被调用一次。您可以将单击侦听器移动到onBindViewHolder方法,并使用传入的位置和持有者来获取列表中的正确项目。
这样的事情应该有效:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
(ListViewPriceListRowHolder) holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = holder.priceId.getText().toString()
// do stuff with the id
}
});
}
答案 1 :(得分:0)
试试这个:
@Override
public ListViewPriceListRowHolder onCreateViewHolder(final ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.price_list_row,null);
return v;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public NetworkImageView thumbnail ;
public TextView priceID;
public TextView orderID;
public TextView SPName;
public TextView catID;
public TextView price;
public TextView orderDetailsSuply;
public TextView approve;
public RelativeLayout rellayout;
public MyViewHolder(final View view) {
super(view);
priceID = (TextView) view.findViewById(R.id.tv_price_id_row);
orderID = (TextView) view.findViewById(R.id.tv_order_id_row);
SPName = (TextView) view.findViewById(R.id.tv_sp_name_row);
catID = (TextView)view.findViewById(R.id.tv_cat_id_row);
price = (TextView)view.findViewById(R.id.tv_price_row);
orderID = (TextView)view.findViewById(R.id.tv_order_id_row);
orderDetailsSuply=(TextView)view.findViewById(R.id.tv_orderdetailsuplly_row);
approve = (TextView)view.findViewById(R.id.tv_approve_row);
rellayout = (RelativeLayout) view.findViewById(R.id.pl_rellayout);
rellayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
priceID = (TextView)viewGroup.findViewById(R.id.tv_price_row);
String priceIDStr = priceID.getText().toString();
//here I am getting the same price id every time I click on diiferent row
Toast.makeText(mContext,priceIDStr,Toast.LENGTH_SHORT).show();
}
});
}
}