Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android RecyclerView pass位置始终是最后一个_Java_Android_Android Recyclerview_Android Adapter - Fatal编程技术网

Java Android RecyclerView pass位置始终是最后一个

Java Android RecyclerView pass位置始终是最后一个,java,android,android-recyclerview,android-adapter,Java,Android,Android Recyclerview,Android Adapter,您好,我想将位置从RecyclerView的适配器传递到MainActivity,因此我设计了一个界面: 在RecycleServiceAdapter中: publuc interface recyclerViewAdapterListener{ void onDeviceConnect(data, position) } public void onBindViewHolder(@NonNull ViewHolder holder, int position) { //he

您好,我想将位置从
RecyclerView
的适配器传递到
MainActivity
,因此我设计了一个界面:

在RecycleServiceAdapter中:

 publuc interface recyclerViewAdapterListener{
   void onDeviceConnect(data, position)
 }

 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
   //here I call onDeviceConnect
   holder.button.setOnClickListener(new View.OnClickListener(){
   public void onClick(View view) {
     listener.onDeviceConnect(data, position);
   }
 
在维护中:

 private int pos;

 public void onDeviceConnect(data,position){
  pos = position; 
 }
我知道问题是
pos=position
,这将始终是最后一个位置

但我需要来自<代码>RecyclerView的数据和位置,因为当我的设备连接时

它将更改回收视图的项目

例:(在主要活动中)

ex:(在RecycleServiceAdapter中)

任何好主意都可以解决它,谢谢。

在MainActivity:

 private int pos;

 public void onDeviceConnect(data,position){
  pos = position; 
 }
`


`

尝试将
位置更换为
支架。getAdapterPosition()


或者您也可以在
ViewHolder
类的构造函数中
setOnClickListener
,在该构造函数中调用适配器上的
onDeviceConnect
?无需回调到MainActivity。您可以在adapter@Shay Kin:I call ondeviconnect中直接调用
updateItem
,在RecycleServiceAdapter onBindViewHolderPosition=position中会得到问题的最后一个位置,因为我不想得到最后一个位置。
   public void updateItem(data,pos){
     if (dataList.size() != 0){
          dataList.set(pos, data);
          notifyItemChanged(pos);
   }
public class MainActicity extends AppCompatActivity implements YourRecyleAdaptor.recyclerViewAdapterListener{
private int pos;


@Override
 public void onDeviceConnect(data,position){
  pos = position; 
 }
 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
     //here I call onDeviceConnect
     holder.button.setOnClickListener(new View.OnClickListener(){
     public void onClick(View view) {
         listener.onDeviceConnect(data, holder.getAdapterPosition());
   }