更新活动类外部的TextView

时间:2016-11-20 15:00:51

标签: java android android-layout

我在viewPager(水平)下面有一个LinearLayout,其中有TextView个。{/ p>

LinearLayout是一个状态栏,可以保存所有玩家的统计信息,因此会经常更新。这就是为什么我决定使用方法来做到这一点:更新状态栏。

问题

我需要更新它,例如,当用户购买Mine(这是ViewPager的页面)时,但是用于收听播放器在页面上点击的代码是Adapter。当我尝试在此类上插入TextView时,我只是不能

  

所以我想知道如何制作一个方法,可以通过我的所有方法访问   类,以刷新那些TextView的值。

我有一个 CenterRepository 类,但看起来我无法在其中添加Android代码,所以我基本上无法在那里添加TextView

代码

这是适配器

public class MineAdapter extends PagerAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
private CenterRepository data;
private TextView expLvlTxt;
private TextView expTxt;
private TextView coinsTxt;

public MineAdapter(Context context) {
    mContext = context;
    mLayoutInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    data = CenterRepository.getSingletonInstance();
}


@Override
public int getCount() {

    //Fill Data directly from Repository
    return data.getListOfLavels().size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, final int position) {

    System.out.println("Code executed");

    final View itemView = mLayoutInflater.inflate(R.layout.carousal_page, container,
            false);

[跳过的代码]

// Unlock Button
    itemView.findViewById(R.id.unlockButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (data.getCurrentUser().getGold() >=
                    data.getListOfLavels().get(position).getUnlockCost()) {

                //If User can afford the purchase, remove lock image and buy it
                data.getListOfLavels().get(position).setUnlocked(true);

                data.getCurrentUser().setGold(
                        data.getCurrentUser().getGold()
                                - data.getListOfLavels().get(position).getUnlockCost()); // Update user's gold


                itemView.findViewById(R.id.unlockButton).setVisibility(View.INVISIBLE); // Remove lock button
                itemView.findViewById(R.id.mineCost).setVisibility(View.INVISIBLE);


                Toast.makeText(mContext,"Reduced "
                        + data.getListOfLavels().get(position).getUnlockCost(),
                        Toast.LENGTH_LONG).show();

                expLvlTxt.setText(String.valueOf(data.getCurrentUser().getExpLvl()));
                expTxt.setText(String.valueOf(data.getCurrentUser().getExp()));
                coinsTxt.setText(String.valueOf(data.getCurrentUser().getGold()));

            } else {

                // Not enough money
                Toast.makeText(mContext, "Not enough money to purchase You need " +
                        (data.getListOfLavels().get(position).getUnlockCost()
                                - data.getCurrentUser().getGold()) + " more", Toast.LENGTH_SHORT).show();
            }

        }
    });

这里是中心存储库

public class CenterRepository {

private User currentUser;
private static CenterRepository singletonInstance;
private ArrayList<Mine> minesList = new ArrayList<>();

private CenterRepository() {
}

public void addMine(Mine mine){
    minesList.add(mine);
}
public void setCurrentUser(User currentUser) {
    this.currentUser = currentUser;
}

public static CenterRepository getSingletonInstance() {
    if (null == singletonInstance) {
        singletonInstance = new CenterRepository();
    }
    return singletonInstance;
}
public User getCurrentUser() {
    return currentUser;
}

public ArrayList<Mine> getListOfLavels() {
    return minesList;
}

public static int randInt(int min, int max) {
    Random rand = new Random();
    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    return rand.nextInt((max - min) + 1) + min;
    }
}

0 个答案:

没有答案