繁体   English   中英

从RecyclerView适配器类调用Activity中的方法

[英]Calling methods in Activity from RecyclerView adapter class

我有一个带有recyclerview和按钮的活动。 在onBindViewHolder的recyclerview适配器中,将onClickListener设置为列表项的布局。 单击此按钮后,我想将选择的联系人(列表项类型)传递给我的活动,然后传递到列表中,以便可以将其作为请求发送到我的服务器。

单击该项目时,我希望颜色更改为突出显示的蓝色,并且让屏幕底部的按钮说“继续-x联系人”(x =选择了许多联系人。)直到我将按钮添加到活动中。

我遇到的问题是按钮没有与按钮ID关联,并且在单击项目后崩溃了,因为无法在空对象引用上调用setText()。

错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference

我不明白为什么按钮为null,除非这是我从适配器调用活动对象的方式。

感谢任何帮助或建议!

RecyclerViewAdapter :(我在适配器中有其他方法和ViewHolder类,它们工作正常。这是唯一影响结果afaik的方法)

private CreateGroupAccountStage2 createGroupAccountStage2 = new CreateGroupAccountStage2();
public List<Contacts> contactsList;
public List<Contacts> selectedContacts = new ArrayList<>();

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
final Contacts contacts = contactsList.get(position);
viewHolder.contactName.setText(contacts.getContactName());
viewHolder.contactItem.setOnClickListener(new View.OnClickListener() { //Set on click listener to item layout
  @Override
  public void onClick(View view) {
    if (!contacts.getIsPressedValue()) {
      contacts.setPressedTrue(); // Method that changes boolean value stored in the Contact object
      viewHolder.contactItem.setBackgroundResource(R.color.createGroupAccountContactItemPressed);
      selectedContacts.add(contacts);
    } else {
      viewHolder.contactItem.setBackgroundResource(R.color.whiteText);
      contacts.setPressedFalse(); // Method that changes boolean value stored in the Contact object
      selectedContacts.remove(contacts);
    }
    createGroupAccountStage2.updateSelectedContacts(contactsList);
  }
});
}

活动:

Button stage2Continue;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_create_group_stage2);

  stage2Continue = (Button) findViewById(R.id.createGroupAccountStage2ContinueBTN);
  setUpActionBar();
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

  ArrayList<Contacts> fullContactsList = new ArrayList<>();
  fullContactsList.add(new Contacts(R.drawable.human_photo, "Human One", "humanone@gmail.com"));
  fullContactsList.add(new Contacts(R.drawable.human_photo, "Human Two", "humantwo@gmail.com"));

  setUpContactsRecyclerView(fullContactsList); //Sets up recycler view (works fine)
}

public void updateSelectedContacts(List<Contacts> contacts) {
  int listSize = contacts.size();
  updateContinueButton(listSize);
}

public void updateContinueButton(int selectedContactsListSize) {
  String listSize = Integer.toString(selectedContactsListSize);
  String buttonText = "Continue - " + listSize + " contacts";
  if(selectedContactsListSize > 0) {
    stage2Continue.setText(buttonText);
  } else {
    stage2Continue.setVisibility(View.INVISIBLE);
  }
}

布局XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="grouppay.dylankilbride.com.activities.CreateGroupAccountStage2">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ToolbarTheme">

<android.support.v7.widget.Toolbar
    android:id="@+id/createAccountStage2Toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/whiteText"
    app:popupTheme="@style/ToolbarTheme" />

</android.support.design.widget.AppBarLayout>

<Button
    android:id="@+id/createGroupAccountStage2ContinueBTN"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_margin="40dp"
    android:layout_alignParentBottom="true"
    android:text="@string/generic_continue"
    android:textSize="15sp"
    android:visibility="invisible"
    android:textColor="@color/whiteText"
    android:background="@drawable/generic_rounded_continue_button_bg"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/createGroupAccountStage2ContactsRV"
    android:layout_width="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

联系人列表项布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:padding="10dp"
android:id="@+id/createGroupAccountStage2ContactLL">

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/createGroupAccountStage2ContactImgTV"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/human_photo"
    app:civ_border_width="1dp"
    app:civ_border_color="@color/profileImageBorder"/>

<TextView
    android:id="@+id/createGroupAccountStage2ContactNameTV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:textSize="20sp"
    android:fontFamily="@font/cairo"
    tools:text="Human Example"
    />

</LinearLayout> 

看来您需要在RecyclerViewAdapter中实现onCreateViewHolder() ,以便使用包含您的createGroupAccountStage2ContinueBTN按钮的布局使ViewHolder膨胀。

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = mInflater.inflate(R.layout.your_item_layout, parent, false);
    return new ViewHolder(itemView);
}

检查您的导入,如果在您的代码中导入了正确的contactName视图,则您有可能在任何其他xml文件中的另一个视图中使用相同的ID,并且由于导入错误,该视图将为null,因为该视图不是充气。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM