![](/img/trans.png)
[英]Calling StartActivityResult inside adapter class and updating the recyclerview
[英]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.