gpt4 book ai didi

java - 如何处理卡片 View 中不同的按钮 onclick 并传递 Intent ?

转载 作者:行者123 更新时间:2023-12-01 16:28:22 25 4
gpt4 key购买 nike

我有一个卡片 View ,它显示即将举行的 Activity ,包括标题、日期等,并且有两个 ImageView 应充当按钮:如果单击其中一个,则应转到参与确认 Activity ,如果单击另一个,则应转到参与确认 Activity 详细 Activity 。这两者还应该使用 Intent (名称、日期、用户 ID、 token )传递信息。我已关注this tutorial ,因为这是最接近我的问题的解决方案,但它仍然不起作用。无论我如何更改,这些都是我得到的错误:尝试在空对象引用上调用虚拟方法 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' (这是我调用 setButtons 函数时给出的) ,或者如果 onclick 以某种方式工作, Intent 不会被传递,并且我得到一个 nullpointerException。我确保我使用了正确的 ID 和名称。附加信息:我正在使用 Laravel 和改造。 Activity Activity :

public class EventsActivity extends AppCompatActivity {
private static final String TAG = "EventsActivity";
private static final int ACTIVITY_NUM = 1;
private static final String PREFS_NAME = "LoginPrefs";
public static final String EVENT_DETAIL = "EVENT_DETAIL";
private Context mContext = EventsActivity.this;
private Event mEvent = new Event();
private String token, name;
private int id;
private EventAdapter adapter;
private RecyclerView recyclerView;
private List<Event> eventList = new ArrayList<>();
private List<Response> result;
private TextView noEvents;
private Button add, more;

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

//Get token and id from shared preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
token = settings.getString("token", "");
id = settings.getInt("id", 0);
Log.d(TAG, "onResponse: id " + id + "token " + token);

noEvents = findViewById(R.id.noEvents);
recyclerView = findViewById(R.id.event_recyclerview);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
recyclerView.setLayoutManager(layoutManager);
adapter = new EventAdapter(mContext, eventList);
recyclerView.setAdapter(adapter);

adapter.setOnItemClickListener(new EventAdapter.OnItemClickListener() {
@Override
public void onAddClicked(int position) {
addClick(position);
}

@Override
public void onMoreClicked(int position) {
moreClick(position);
}
});

loadData();
setButtons();
}

public void addClick(int position){
final Event event = eventList.get(position);
Intent i = new Intent(EventsActivity.this, EventInvitationActivity.class);
i.putExtra("name", event.getName());
i.putExtra("name", event.getDescription());
i.putExtra("name", event.getDate());
i.putExtra("name", event.getLocation());
Log.i("hello", event.getName());
startActivity(i);
}

public void moreClick(int position){
final Event event = eventList.get(position);
Intent i = new Intent(EventsActivity.this, EventDetailActivity.class);
i.putExtra("name", event.getName());
Log.i("hello", event.getName());
startActivity(i);
}

void setButtons() {
add = findViewById(R.id.add_icon);
more = findViewById(R.id.more_icon);
Log.d(TAG, "setButtons: called");
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: add onclicklistener");
int position = Integer.parseInt(v.getTag().toString());
addClick(position);
}
});
more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
moreClick(position);
}
});
}

//Listing events
private void loadData(){
ApiEvents service = RetrofitBuilder.getRetrofitInstance().create(ApiEvents.class);
Call<List<Event>> call = service.listEvents("Bearer" + token, id);
call.enqueue(new Callback<List<Event>>() {
@Override
public void onResponse(@NonNull Call<List<Event>> call, @NonNull Response<List<Event>> response) {
if (response.isSuccessful() && response.body()!=null) {
Log.d(TAG, "onResponse: loadData called");
eventList = response.body();
adapter = new EventAdapter(mContext, eventList);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new EventAdapter.OnItemClickListener() {
@Override
public void onAddClicked(int position) {
addClick(position);
}

@Override
public void onMoreClicked(int position) {
moreClick(position);
}
});
}
else if(response.body() == null){
noEvents.setVisibility(View.VISIBLE);
}
else{
Log.d(TAG, "onResponse: " + response.body());
}
}

@Override
public void onFailure(@NonNull Call<List<Event>> call, @NonNull Throwable t) {
Log.d("Response", "onFailure: " + t.toString());
}
});
}

}

事件适配器

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.MyViewHolder> {
private Context mContext;
private List<Event> eventList;
private static final String TAG = "EventDetailActivity";
private OnItemClickListener clickListener;

public EventAdapter(Context mContext, List<Event> eventList){
this.mContext = mContext;
this.eventList = eventList;
}

public interface OnItemClickListener {
void onAddClicked(int position);
void onMoreClicked(int position);
}

public void setOnItemClickListener(OnItemClickListener clickListener) {
this.clickListener = clickListener;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_layout, parent,
false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
Event event = eventList.get(position);
holder.itemView.setTag(event);
holder.title.setText(eventList.get(position).getName());
holder.description.setText(eventList.get(position).getDescription());
holder.date.setText(eventList.get(position).getDate());
holder.location.setText(eventList.get(position).getLocation());
holder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(clickListener != null){
int position = (int)v.getTag();
clickListener.onAddClicked(position);
final Event event = (Event)v.getTag();
if(event!= null){
Intent i = new Intent(mContext, EventInvitationActivity.class);
i.putExtra("EVENT_DETAIL", event);
v.getContext().startActivity(i);
}
}
}
});

holder.more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(clickListener != null){
int position = (int)v.getTag();
clickListener.onMoreClicked(position);
final Event event = (Event)v.getTag();
if(event!= null){
Intent i = new Intent(mContext, EventDetailActivity.class);
i.putExtra("EVENT_DETAIL", event);
v.getContext().startActivity(i);
}
}
}
});


@Override
public int getItemCount() {
return eventList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title, description, date, location;
Button add, more;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.event_name);
description = itemView.findViewById(R.id.event_description);
date = itemView.findViewById(R.id.event_date);
location = itemView.findViewById(R.id.event_location);
add = itemView.findViewById(R.id.add_icon);

}

}

这就是我试图在两个 Activity 中获取传递 Intent 的方式:

//Getting resources
mName = findViewById(R.id.eventName);
mDescription = findViewById(R.id.description);
mLocation = findViewById(R.id.location);
mDate = findViewById(R.id.date);

mEvent = Objects.requireNonNull(getIntent().getExtras()).getParcelable(EVENT_DETAIL);
if(mEvent != null){
mName.setText(mEvent.getName());
mDescription.setText(mEvent.getDescription());
mLocation.setText(mEvent.getLocation());
mDate.setText(mEvent.getDate());
}

这是卡片 View 中的按钮示例:

<Button
android:id="@+id/add_icon"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_below="@+id/image"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:layout_toEndOf="@id/event_description"
android:drawableStart="@drawable/ic_add_black_24dp"
android:scaleType="centerCrop"
android:transitionName="img"
android:visibility="visible"
android:textColor="@color/colorAccent"
/>

所以问题是,我怎样才能做到这一点?

编辑我已经弄清楚问题是什么:添加和更多 ImageView 作为 CardView 的一部分位于 event_layout 上,而不是程序正在寻找它们的 Activity_events 上。但是,当这两个按钮放置在 CardView 上时,我真的不知道如何使它们工作。

最佳答案

在您的MyViewHolder构造函数中,您没有初始化more,因此它将为空,因此会抛出空引用异常

关于java - 如何处理卡片 View 中不同的按钮 onclick 并传递 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62115098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com