Ich habe eine Reihe von Möglichkeiten gegoogelt, wie man das Objekt erhält, das in einer Recycler-Ansicht angeklickt wird, aber keiner von ihnen scheint zu funktionieren. Der Code unten sollte funktionieren, aber ich verstehe nicht, warum es nicht ist. In Android Studio ist die getPosition() -Methode durchgestrichen. Mir gehen die Ideen aus, gibt es eine Möglichkeit, zumindest auf den Namen der Recycler-Ansicht zu klicken?Recycler Artikel anzeigen onClick
ViewHolder
public class ShoppingListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public CardView cv;
public TextView name;
public TextView description;
Context context;
public ShoppingListViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
name = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("RecyclerView", "onClick:" + getPosition());
}
});
}
public void bindView(int position) {
}
@Override
public void onClick(View v) {
Log.d("TAG", "onClick " + getPosition() + " " + name);
}
Adapter
public class MainShoppingListViewActivity extends AppCompatActivity {
List<Data> list = new ArrayList<>();
RecyclerView recyclerView;
ShoppingListViewAdapter adapter;
Context context;
EditText listName;
Firebase ref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_shoppinglist_view);
Firebase.setAndroidContext(this);
ref = new Firebase("https://shoppinglistshare.firebaseio.com/");
// Getting the recycler view, using the Recycler View
recyclerView = (RecyclerView) findViewById(R.id.shopping_list_recycler_view);
// The setLayoutManager which contains the main container where the Recycler View is contained
recyclerView.setLayoutManager(new LinearLayoutManager(MainShoppingListViewActivity.this));
// Floating action bar initiated, background color is set and then onClickListener to act when the
// button is pressed to which the createDialogBox(); method is called
FloatingActionButton addFloat = (FloatingActionButton) findViewById(R.id.fab);
addFloat.setBackgroundTintList(ColorStateList.valueOf(Color
.parseColor("#3F51B5")));
addFloat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Click action
createDialogBox();
}
});
}
/*
* This method is used to add a new item to the Recycler View
*/
public void addNewShoppingListItem(String val) {
ref = new Firebase("https://shoppinglistshare.firebaseio.com/");
adapter = new ShoppingListViewAdapter(list, MainShoppingListViewActivity.this);
recyclerView.setAdapter(adapter);
adapter.addItem(new Data(val, "Created by: Me"));
ref.child("List Name: ").setValue(val);
}
/*
* This method is used to add a new item to the Recycler View
*/
public void removeShoppingListItem(String val) {
adapter = new ShoppingListViewAdapter(list, MainShoppingListViewActivity.this);
recyclerView.setAdapter(adapter);
adapter.addItem(new Data(val, "Created by: Me"));
}
/*
* This method is called inside the float action bar to create a dialog box when the floating action button is pressed.
*/
public void createDialogBox() {
// Alert Dialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Using Layout Inflater class and assigned to a variable and using getLayout
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout.
// Using the view class to inflate the layout.
View rootView = inflater.inflate(R.layout.custom_alert_dialog, null);
listName = (EditText) rootView.findViewById(R.id.ShoppinglistName);
// Using the AlertDialog Builder created above as "builder" to set the view.
builder.setView(rootView)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String val = listName.getText().toString();
if (!val.isEmpty()) {
addNewShoppingListItem(val);
} else {
Toast.makeText(MainShoppingListViewActivity.this, "Please enter a value", Toast.LENGTH_SHORT).show();
}
}
})
// Show the dialog
.show();
}
}
Logs
FATAL EXCEPTION: main
Process: familyshopshare.com.familyshopshare, PID: 17889
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.widget.Toast.<init>(Toast.java:102)
at android.widget.Toast.makeText(Toast.java:259)
at familyshopshare.com.familyshopshare.ShoppingListViewHolder$1.onClick(ShoppingListViewHolder.java:33)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Zur Info: Es gibt 2 onClicks in den Konstruktor funktioniert und protokolliert den Punkt Position, aber wenn ich ein setzen Toast da stürzt es die App ab. Allerdings würde ich bevorzugen, dass die Klicks in der onClick-Methode –
"getPosition" durchgestrichen werden, weil es veraltet ist (drücken Sie 'Strg + Q' darauf, Dokumentation wird Ihnen das sagen). Sie sollten stattdessen 'getAdapterPosition()' verwenden. In Bezug auf Ihr Problem - zeigen Sie Ihre ViewHolder-Implementierung ist nicht genug, scheint das Problem woanders, in Adapter vielleicht. Wie erstellen Sie einen Ansichtshalter? – dimsuz
Es stürzt wahrscheinlich ab, weil Ihr 'Context' null ist, ich sehe nicht, wo Sie es zuweisen. Sie könnten es aus 'itemView.getContext()' – dimsuz