从另一个活动更新TextView

时间:2019-03-03 19:55:18

标签: java android int adapter send

我有一个Adapter(ViewHolder)类,当我按下一个按钮时,我想更新Home()活动的TextView,最好不要立即启动该活动。

适配器中的代码:

public class GameViewAdapter extends RecyclerView.Adapter<GameViewAdapter.GameViewHolder> {
public static Object OnItemClickListener;
private ArrayList<GameItem> mGameList;
private OnItemClickListener mListener;
public static MyCallback callback;


public GameViewAdapter(ArrayList<GameItem> mGameList, GameViewAdapter.OnItemClickListener mListener, MyCallback callback) {
    this.mGameList = mGameList;
    this.mListener = mListener;
    this.callback = callback;
}

@Override
public GameViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    Context context = viewGroup.getContext();
    View v = LayoutInflater.from(context).inflate(R.layout.game_entry, viewGroup, false);
    GameViewHolder GVH = new GameViewHolder(v, mListener);
    return GVH;
}

@Override
public void onBindViewHolder(@NonNull GameViewHolder gameViewHolder, int position) {
    gameViewHolder.bind(mGameList.get(position));
}

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

class GameViewHolder extends RecyclerView.ViewHolder {

    private ImageView itemCover;
    private TextView itemTitle;
    private TextView itemDescription;
    private PopupWindow popupWindow;
    private ImageView popUpImage;
    private TextView PopUpTitle;
    private EditText customAmount;
    private Button add;
    private Button addcustom;
    private Button exit;

    public GameViewHolder(View itemView, GameViewAdapter.OnItemClickListener mListener) {
        super(itemView);
        setupViews(itemView);
    }

    public void bind(final GameItem gameItem) {
        Glide.with(this.itemCover).load(gameItem.getCover()).into(this.itemCover);
        this.itemTitle.setText(gameItem.getTitle());
        this.itemDescription.setText(gameItem.getDescription());
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopUp(itemView, gameItem);
            }
        });
    }

    private void setupViews(View itemView) {
        addcustom = itemView.findViewById(R.id.addcustom);
        popUpImage = itemView.findViewById(R.id.popupimg);
        PopUpTitle = itemView.findViewById(R.id.popuptitle);
        customAmount = itemView.findViewById(R.id.gameamount);
        itemCover = itemView.findViewById(R.id.GameCover);
        itemTitle = itemView.findViewById(R.id.GameTitle);
        itemDescription = itemView.findViewById(R.id.GameAmount);
        exit = itemView.findViewById(R.id.exit);
    }

    private void showPopUp(final View itemView, final GameItem gameItem) {
        //Declaration
        final View popupView = LayoutInflater.from(itemView.getContext()).inflate(R.layout.popup, null);
        final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        final ImageView popupItemCover = popupView.findViewById(R.id.popupimg);
        final TextView popupItemTitle = popupView.findViewById(R.id.popuptitle);
        //Set Data
        Glide.with(popupItemCover).load(gameItem.getCover()).into(popupItemCover);
        popupItemTitle.setText(gameItem.getTitle());
        popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);

        //ExitButton
        exit = popupView.findViewById(R.id.exit);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        add = popupView.findViewById(R.id.addaverage);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onDataChanged(Integer.valueOf(gameItem.getTime()));
                Toast.makeText(popupView.getContext(), String.valueOf(gameItem.getTime()), Toast.LENGTH_SHORT).show();
            }
        });
    }

}

public interface MyCallback {
    void onDataChanged(Integer gameTime);
}

public interface OnItemClickListener {
    void onGameClick(int position);
}
}

我已经在“主要活动”中实现了这一点:

   public void onDataChanged(int gameTime) {
    // it will fire when you call your callback.onDataChanged(); in your adapter
    // here can change your textview
    GameTime.setText(Integer.valueOf(gameTime));
}

}

我也尝试过使用BroadcastReceiver,但没有成功。

谢谢!

编辑:

添加回调时,我的应用因以下回溯而崩溃:

java.lang.NullPointerException: Attempt to invoke interface method 'void com.reogen.gametime.Adapters.GameViewAdapter$MyCallback.onDataChanged(java.lang.String)' on a null object reference
    at com.reogen.gametime.Adapters.GameViewAdapter$GameViewHolder$3.onClick(GameViewAdapter.java:127)

第127行为:callback.onDataChanged(Integer.valueOf(gameItem.getTime()));

1 个答案:

答案 0 :(得分:1)

您可以创建可观察的内容,例如:

在您的适配器中创建:

public interface IMyCallback {
    void onDataChanged(int gameTime);
}

适配器中的全局字段:

private IMyCallback callback

适配器的内部构造器

public MyAdapter(... , IMyCallback callback) {
    ...
    this.callback = callback;
}

现在,您的活动make实现IMyCallback并重写onDataChanged方法。 当您创建适配器时,以回调YourActivity.this的形式发送。

最后,您需要像这样

    add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callback.onDataChanged(Integer.valueOf(gameItem.getTime()));
                }

此调用之后,您将在活动类的重写方法onDataChanged中获得更新。享受

------------更新----------- 在您的活动中,当您实现接口时,您将获得类似

的方法
     @Override
    public void onDataChanged(int gameTime) {
        // it will fire when you call your callback.onDataChanged(); in your adapter
        // here can change your textview
        textview.setText(String.valueOf(gameTime));
    }