从RecyclerView中的Button获取数据

时间:2018-11-04 04:33:39

标签: java android firebase firebase-realtime-database

所以我有一个RecyclerView,它显示了大厅中打开的游戏的列表。每次更新RecyclerView时,都会在屏幕上添加一个按钮,我要创建一个按钮,以便每当用户单击该按钮时,它都会获取其所在部分的ID。

因此,游戏会像这样添加到数据库中。

public void createLobbyGame () {
    User current = new User(userName, 0, getSide());
    String id = FCGames.push().getKey();

    gameMaker = new GameMaker(current, null, 0, numGames, false, false);

    FCGames.child(id)
            .setValue(gameMaker).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                Toast.makeText(FlipCoinLobby.this, "Game creation successful.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(FlipCoinLobby.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}

这会在我的数据库中创建一个如下所示的节点:

enter image description here

所以现在将ID设置为0,但这仅仅是因为我忘记使ID等于节点的名称。

因此,当将某些内容添加到我的数据库时,将使用按钮在应用程序中绘制该内容。我不确定如何使该按钮显示与其关联的数据库节点的ID。

这是我在ViewHolder类中为按钮设置的clicklistener

    Button joinButton = itemView.findViewById(R.id.joinRV);
    joinButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), gm.getId(), Toast.LENGTH_SHORT).show();
            }
        });

仅调用getId()方法根本不起作用,而且我并不期望这样做。但是我有点不知道如何在单击按钮时获取按钮的相关ID


适配器类:

    package com.example.brent.fifty_fifty;

import android.content.Intent;
import android.util.Log;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import static android.view.LayoutInflater.*;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

GameMaker gm;
private OnItemClickListener listener;
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

// data is passed into the constructor
MyRecyclerViewAdapter(Context context, List<String> data) {
    this.mInflater = from(context);
    this.mData = data;
}

public MyRecyclerViewAdapter(List<String> data, OnItemClickListener listener) {
    this.mData = data;
    this.listener = listener;
}
public interface OnItemClickListener {
    void onItemClick(String id);
}

// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
    return new ViewHolder(view);
}

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    String userName = mData.get(position);
    //Double wager = Double.parseDouble(mData.get(position));
    holder.userName.setText(userName);
    //holder.wager.setText(wager.toString());

}

// total number of rows
@Override
public int getItemCount() {
        return mData.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView userName, wager;
    RelativeLayout parentLayout;

    ViewHolder(View itemView) {
        super(itemView);
        userName = itemView.findViewById(R.id.UsernameRV);
        //wager = itemView.findViewById(R.id.wagerRV);
        itemView.setOnClickListener(this);

        Button joinButton = itemView.findViewById(R.id.joinRV);
        joinButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                listener.onItemClick(data);
                Toast.makeText(itemView.getContext(), "It worked", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void bind(final String data, final OnItemClickListener listener) {
    }

    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(mData.get(position), listener);
    }

    @Override
    public void onClick(View view) {
        if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
    }
}

// convenience method for getting data at click position
String getItem(int id) {
    return mData.get(id);
}

// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
    this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
    void onItemClick(View view, int position);
}

}

1 个答案:

答案 0 :(得分:1)

请查看作为recyclerview的侦听器的接口的实现。

Please look this implementation. This is good one if you are using JAVA.

此处在itemView上设置了clicklistener,您只需要用按钮替换itemView即可。

public class MyRecyclerViewAdapter extends RecyclerView.Adapter< MyRecyclerViewAdapter.ViewHolder> {

public interface OnButtonClickListener {
    void onButtonClick(String id);
}

private final List<String> items;
private final OnButtonClickListener listener;

public MyRecyclerViewAdapter(List<String> items, OnButtonClickListener listener) {
    this.items = items;
    this.listener = listener;
}

@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout. recyclerview_row, parent, false);
    return new ViewHolder(v);
}

@Override public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bind(items.get(position), listener);
}

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

static class ViewHolder extends RecyclerView.ViewHolder {

    private Button joinButton;

    public ViewHolder(View itemView) {
        super(itemView);
        joinButton = (Button) itemView.findViewById(R.id. joinRV);
    }

    public void bind(final String id, final OnButtonClickListener listener) {
        joinButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                listener.onItemClick(id);
            }
        });
    }
}}

为了您的活动:

recycler.setAdapter(new MyRecyclerViewAdapter(items, new MyRecyclerViewAdapter.OnButtonClickListener() {
@Override public void onButtonClick(String id) {
    Toast.makeText(this, id, Toast.LENGTH_LONG).show();
}}));