這是一個(gè) Adapter :
public class LiveGiftRankingListAdapter extends RecyclerView.Adapter<LiveGiftRankingListAdapter.RankingItemVH> {
private Context mContext;
private List<GiftRankingItemBean> mDataList;
public LiveGiftRankingListAdapter(Context context, List<GiftRankingItemBean> 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);
}
}
}
這是對應(yīng)的 Activity :
public class LiveGiftRankingListActivity extends BaseActivity {
private RecyclerView mRecyclerView;
private TitleBar mTitleBar;
private List<GiftRankingItemBean> 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());
}
}
奇怪的是列表總是沒數(shù)據(jù)顯示出來。
看 log 的輸出是這樣的: (我只打開了 error 級(jí)別的日志)
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==========
發(fā)現(xiàn)Adapter
的onBindViewHolder
只執(zhí)行了一次,而且tv_no
椅野、tv_user_name
乎完、 tv_gift_amount
都沒數(shù)據(jù)顯示出來矫夯。
然后用盡各種方法調(diào)試背稼,用控制變量法周循,不斷注釋,測試焚挠,注釋或杠,測試。
最后定位到問題代碼在這一句:
Adapter的 tv_no.setText(position + 1);
如果我把 postion + 1
改成其他字符串就沒問題宣蔚,比如 hello
。
額认境。胚委。才想起 TextView.setText()
有重載,系統(tǒng)把 position + 1
當(dāng)作資源 id 了叉信,然后根據(jù)這個(gè) id 去尋找資源亩冬,結(jié)果當(dāng)然是沒找到。
但是坑的是它不會(huì)拋出異常硼身,而且出問題以后它就不再往下執(zhí)行了硅急。。佳遂。
我打開 verbose
級(jí)別的日志發(fā)現(xiàn)是這樣的:
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
級(jí)別的日志营袜,導(dǎo)致沒發(fā)現(xiàn)這個(gè)資源找不到的 info (系統(tǒng)竟然只把這種問題只當(dāng)作 info ,連 warming
都不是)丑罪。好吧荚板。。其實(shí)最大的問題是我自己寫代碼粗心大意吩屹,正確的寫法是 tv_no.setText( position + 1 + "")
跪另,我忘了再加一個(gè)雙引號(hào)。
為了以后少遇到這種問題:
- 其它類型轉(zhuǎn)換字符串都用這種方式:
String.valueOf()
煤搜,嚴(yán)謹(jǐn)一點(diǎn)免绿。 - 遇到 bug,排除問題的時(shí)候要打開全部日志擦盾,調(diào)到
verbose
級(jí)別嘲驾,細(xì)細(xì)查看。