从另一个Activity更新TextView

问题描述 投票:-2回答:1

我有一个Adapter(一个ViewHolder)类,当按下Button时,我想更新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()));

java android int adapter send
1个回答
1
投票

您可以创建observable,如:

在您的适配器中创建:

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));
    }
© www.soinside.com 2019 - 2024. All rights reserved.