这是一个 Adapter :
public class LiveGiftRankingListAdapter extends RecyclerView.Adapter {
private Context mContext;
private List mDataList;
public LiveGiftRankingListAdapter(Context context, List dataList) {
mContext = context;
mDataList = dataList;
}
@Override
public RankingItemVH onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_live_gift_ranking_list, parent, false);
return new RankingItemVH(itemView);
}
@Override
public void onBindViewHolder(RankingItemVH holder, int position) {
DLog.e("onBindViewHolder", "===========onBindViewHolder==========");
holder.bindViewData(position);
}
@Override
public int getItemCount() {
return mDataList.size();
}
class RankingItemVH extends RecyclerView.ViewHolder {
TextView tv_no;
TextView tv_user_name;
TextView tv_gift_amount;
public RankingItemVH(View view) {
super(view);
tv_no = (TextView) view.findViewById(R.id.tv_no);
tv_user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
tv_gift_amount = (TextView) itemView.findViewById(R.id.tv_gift_amount);
}
public void bindViewData(int position) {
GiftRankingItemBean bean = mDataList.get(position);
tv_no.setText(position + 1);
tv_user_name.setText("haha " + position);
tv_gift_amount.setText(position * 10);
}
}
}
这是对应的 Activity :
public class LiveGiftRankingListActivity extends BaseActivity {
private RecyclerView mRecyclerView;
private TitleBar mTitleBar;
private List dataList;
private LiveGiftRankingListAdapter mAdapter;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_live_gift_ranking_list);
mTitleBar = new TitleBar(this);
mTitleBar.setTitle("礼物排行榜");
mRecyclerView = (RecyclerView) findViewById(R.id.rl_ranking_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
dataList = new ArrayList<>();
mAdapter = new LiveGiftRankingListAdapter(this, dataList);
mRecyclerView.setAdapter(mAdapter);
test();
}
private void test() {
for (int i = 0; i < 5; i++) {
GiftRankingItemBean item = new GiftRankingItemBean();
item.setNickname("haha " + i);
item.setGiftValue(i * 10 + "");
dataList.add(item);
}
mAdapter.notifyDataSetChanged();
DLog.e("test", "dataList.size() = " + dataList.size());
DLog.e("test", "getItemCount = " + mAdapter.getItemCount());
}
}
奇怪的是列表总是没数据显示出来。
看 log 的输出是这样的: (我只打开了 error 级别的日志)
08-25 18:23:46.553 21328-21328/com.hoho.chatui E/test: getItemCount = 5
08-25 18:23:46.553 21328-21328/com.hoho.chatui E/test: dataList.size() = 5
08-25 18:23:46.553 21328-21328/com.hoho.chatui E/test: getItemCount = 5
08-25 18:23:46.620 21328-21328/com.hoho.chatui E/onBindViewHolder: ===========onBindViewHolder==========
发现Adapter
的onBindViewHolder
只执行了一次,而且tv_no
、tv_user_name
、 tv_gift_amount
都没数据显示出来。
然后用尽各种方法调试,用控制变量法,不断注释,测试,注释,测试。
最后定位到问题代码在这一句:
Adapter的 tv_no.setText(position + 1);
如果我把 postion + 1
改成其他字符串就没问题,比如 hello
。
额。。才想起 TextView.setText()
有重载,系统把 position + 1
当作资源 id 了,然后根据这个 id 去寻找资源,结果当然是没找到。
但是坑的是它不会抛出异常,而且出问题以后它就不再往下执行了。。。
我打开 verbose
级别的日志发现是这样的:
08-25 18:23:46.553 21328-21328/com.hoho.chatui E/test: getItemCount = 10
08-25 18:23:46.553 21328-21328/com.hoho.chatui E/test: dataList.size() = 10
08-25 18:23:46.553 21328-21328/com.hoho.chatui E/test: getItemCount = 10
08-25 18:23:46.566 21328-21649/com.hoho.chatui I/MobclickAgent: Extend current session: 5BE55D12218F991680664E3A6BA40551
08-25 18:23:46.620 21328-21328/com.hoho.chatui E/onBindViewHolder: ===========onBindViewHolder==========
08-25 18:23:46.620 21328-21328/com.hoho.chatui W/ResourceType: No package identifier when getting value for resource number 0x00000001
08-25 18:23:46.621 21328-21328/com.hoho.chatui I/AssetManager: AM res not found getResourceText id = 1 caller = android.content.res.Resources.getText:337 android.widget.TextView.setText:4214 com.hoho.chatui.live.adapter.LiveGiftRankingListAdapter.onBindViewHolder:42 com.hoho.chatui.live.adapter.LiveGiftRankingListAdapter.onBindViewHolder:19 android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder:5471 android.support.v7.widget.RecyclerView$Adapter.bindViewHolder:5504
我通常只打开 error
级别的日志,导致没发现这个资源找不到的 info (系统竟然只把这种问题只当作 info ,连 warming
都不是)。好吧。。其实最大的问题是我自己写代码粗心大意,正确的写法是 tv_no.setText( position + 1 + "")
,我忘了再加一个双引号。
为了以后少遇到这种问题:
- 其它类型转换字符串都用这种方式:
String.valueOf()
,严谨一点。 - 遇到 bug,排除问题的时候要打开全部日志,调到
verbose
级别,细细查看。