0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】細かい注意点

0
Posted at

拡張forの中の処理を一度も通っていない

list.size() == 0のとき、拡張for文の中の処理はすべてスルーしてしまう。

List<hogeDto> list = new ArrayList<>();
//list.size == 0
for(hogeDto dto : list){
    Log.d("log",dto.getId());
}

adapterのgetViewが一度も呼ばれていない

getCount = 0の時はよばれない。
getCountにlist.size() == 0のlistを入れている時は注意する。

NotFound ResourceId エラーが出てしまう

textViewに数字をsetすると、リソースIDと勘違いしてしまう。
"3"なんてリソースIDで指定しているオブジェクトはないので、
エラーになってしまう。
→Stringに変換してからsetTextするとエラーにはならない。

int number = 3;
TextView view = findViewById(R.id.hoge);
view.setText(number);

APIをたたいて取得したデータをActivityでListにつめて、ListをAdapterに渡してnewするとListがNullで落ちてしまう

①onCreate()の中で、adapterにカラのListをつめてLayoutを描画する。
②onResponse()が呼ばれると、ListにAPIから取得したデータをつめて
再度Layoutを描画する。
※APIからデータを取ってくる速度は、
AdapterのgetViewで描画が完了する速度よりも遅いため。

HogeActivity {
  onCreate(){
    ①SharedPreferencesを定義する(API取得時に使用。null対策。)
    ②List<hogeDto> list = new ArrayList<>();
    ③APIからデータを取得してListにつめるメソッドを呼ぶ
    ④adapterにListを渡す
  }
  
  //③のメソッド
  API.getInstance.getHogePoint(){
    onResponse(){
      //Listにデータをつめる処理
      //adapterのデータが変わったよ!という通知
      adapter.notifyDataSetChanged();
    }
  }
}

adapter.notifyDataSetChanged();でエラーになってしまう

runnableを使えばエラーは消える。

/**
* adapter更新処理
*/
private Runnable adapterNotify = new Runnable() {
    @Override
    public void run() {
       adapter.notifyDataSetChanged();
   }
};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?