所以我有這個片段,其中包含多個TextView,當用戶點擊「下一個」按鈕或「上一個」按鈕時,它會被更改。我在哪裏可以定義片段中的textViews以多次更改其內容?
的onCreateView方法中我有:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_time_table, container, false);
update(inflatedView,worker.current.getTarih(),worker.current.getHicri(),worker.current.getGün(),worker.current.getImsak(),worker.current.getSabah(),worker.current.getGüneş(),worker.current.getÖğle(),worker.current.getIkindi(),worker.current.getAkşam(),worker.current.getYatsı());
// Inflate the layout for this fragment
return inflatedView;
}
上的更新方法
我
public void update(View dimension,String tarih, String hicri, String gun, String imsak, String sabah, String gunes, String ogle, String ikindi, String aksam, String yatsi){
TextView gunView = (TextView) dimension.findViewById(R.id.gun);
TextView imsakView = (TextView) dimension.findViewById(R.id.imsak);
TextView sabahView = (TextView) dimension.findViewById(R.id.sabah);
TextView gunesView = (TextView) dimension.findViewById(R.id.gunes);
TextView ogleView = (TextView) dimension.findViewById(R.id.ogle);
TextView ikindiView = (TextView) dimension.findViewById(R.id.ikindi);
TextView aksamView = (TextView) dimension.findViewById(R.id.aksam);
TextView yatsiView = (TextView) dimension.findViewById(R.id.yatsi);
gunView.setText(gun);
imsakView.setText(imsak);
sabahView.setText(sabah);
gunesView.setText(gunes);
ogleView.setText(ogle);
ikindiView.setText(ikindi);
aksamView.setText(aksam);
yatsiView.setText(yatsi);
}
更新方法被調用用戶的點擊。對我來說,每次調用update()
時都會重新定義textview。 我嘗試在onCreate方法上定義TextView,但我一直在獲取NPE。
我打賭的'worker'是空某一部分......很難說沒有了logcat的雖然 –
在'onCreateView'或'onActivityCreated'(你需要在'onCreateView'返回'View'實例變量因爲您需要在'onCreateView'之外訪問它) –
您應該只將'worker.current'作爲一個參數傳遞給'update'方法。沒有必要爲這一個對象拆分所有字段 –