android - GridView 中的特定 TextView 未在应用程序初始化时更新

标签 android gridview android-fragments textview timertask

我在 GridView 中有 4 个 TextViews,在给定的 Fragment 中有一个自定义适配器。我的主要 Activity 通过 TimerTask 收到通知,为正确的 TextView 更新文本颜色。这适用于所有 TextViews,但应用首次启动时第一个除外。之后它像其余的 TextViews 一样正常工作。

在相关的 Fragment 中:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.short_press_info_fragment, container, false);
    GridView grid = (GridView) view.findViewById(R.id.grid_view);
    infoAdapter = new ShortPressInfoAdapter(mKeyInfo, getActivity());
    grid.setAdapter(infoAdapter);
    return view; 
}

在适配器中:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(convertView == null) {
        Log.d("GRID_VIEW", "inflating");
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.grid_item, null);
        TextView text = texts[position] = (TextView) v.findViewById(R.id.gridItemText);
        Log.d("GRID_VIEW", "creating view");
        text.setText(mKeyInfo[position]);
        switch(position) {
        case 0:
            text.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
            break;
        case 1:
            text.setBackgroundColor(mContext.getResources().getColor(R.color.yellow));
            break;
        case 2:
            text.setBackgroundColor(mContext.getResources().getColor(R.color.green));
            break;
        case 3:
            text.setBackgroundColor(mContext.getResources().getColor(R.color.red));
            break;
        default:
            break;
        }
    }
    return v;
}

在主要的 Activity 中:

public void emphasizeShort(final PressID p, final boolean b) {
runOnUiThread(new Runnable() {
  @Override
  public void run() {
    if(b) {
      Log.d(TAG, "setting short " + p + " to black");
      getShortTextView(p).setTextColor(getResources().getColor(R.color.black));
    }
    else {
      Log.d(TAG, "setting short " + p + " to white");
      getShortTextView(p).setTextColor(getResources().getColor(R.color.white));
    }
  }
});
}

无论我在应用程序启动时尝试更改哪个 TextView,日志都显示相同的输出,但第一个 TextView 是唯一一个根本不这样做的输出不改变。

什么会导致 View 在应用程序启动时而不是之后忽略更改?从这段代码中我不明白为什么第一个 TextView 被挑出来。

如有任何帮助,我们将不胜感激。

编辑: 下面是发送请求以强调 TextView 的函数。

@Override
public boolean onTouch(final View v, final MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) { // button pressed
    pressTime = System.currentTimeMillis();
    timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        Log.d("TIMER_TASK", "short press "
            + (System.currentTimeMillis() - pressTime));
        mFourButtonListener.onShortPress(buttonID);
      }
    }, SHORT_PRESS_DELAY);
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        Log.d("TIMER_TASK", "long press "
            + (System.currentTimeMillis() - pressTime));
        mFourButtonListener.onLongPress(buttonID);
      }
    }, LONG_PRESS_DELAY);
    return true;
  } else if (event.getAction() == MotionEvent.ACTION_UP) {
    duration = System.currentTimeMillis() - pressTime;
    timer.cancel();
    timer.purge();
    if (duration >= SHORT_PRESS_DELAY && duration < LONG_PRESS_DELAY)
      mFourButtonListener.onShortRelease(buttonID);
    else if (duration >= LONG_PRESS_DELAY)
      mFourButtonListener.onLongRelease(buttonID);
    return true;
  }
  return false;
}
}

我越来越绝望了。

最佳答案

这里有很多缺失的代码,但如果您遇到应用程序首次启动时无法运行的问题,那么问题很可能是由 runOnUiThread 引起的,这根本不是启动时可靠。 (即,并不是每一个发布的任务都能保证运行。)

添加一个类级别的Handler变量

Handler h;

并在 onCreate

期间创建它
h = new Handler();

然后使用h.post()代替runOnUiThread

关于android - GridView 中的特定 TextView 未在应用程序初始化时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993630/

相关文章:

android - RecyclerView 行为 - 当键盘打开/关闭时变为空

c# - 如何使用 Javascript 在 GridView 中查找文本框

java - 如何在 Android 的 Fragment 中使用 'bundle' 而不是 'intent.putExtra()'

android - 可以像这样将参数传递给 fragment 吗?

android - Android 电子市场上的应用程序是否允许使用捐赠按钮?

android - solo.searchButton 总是返回 true (Android 2.3.6 with robotium)

ASP.NET Gridview - 存在什么属性来确定网格是否呈现?

c# - 为什么在 UI 中使用 DataTable 是错误的?

android - fragment 在 ViewPager 中不可见

android - 父类(super class)android生命周期