2017-05-10 3 views
0

私は重複したコードで同じオーバーライドされたメソッドを持つアダプタを設定しているスイッチケースを使用しています。同じオーバーライドメソッドを使用しているandroidのアダプタ

private void settingAdapter(int position) { 
    switch (position) { 
     case 0: 
      adapter = new SongAdapter(getContext(), false, false) 
      { 
       @Override 
       protected void onOverScrolled() { 
        super.onOverScrolled(); 
        if(nextPageCount==null||nextPageCount.isEmpty()) 
        { 
         return; 
        } 
        else { 
         int count = Integer.parseInt(nextPageCount); 
         getList(count); 
        } 
       } 
      }; 
      customList.setAdapter(adapter); 
      break; 
     case 1: 
      adapter = new ArtistAdapter(getContext(), false, false, false){ 
       @Override 
       protected void onOverScrolled() { 
        super.onOverScrolled(); 
        if(nextPageCount==null||nextPageCount.isEmpty()) 
        { 
         return; 
        } 
        else { 
         int count = Integer.parseInt(nextPageCount); 
         getList(count); 
        } 
       } 
      }; 
      customList.setAdapter(adapter); 
      break; 
     case 2: 
      adapter = new AlbumAdapter(getContext(), false, false){ 
       @Override 
       protected void onOverScrolled() { 
        super.onOverScrolled(); 
        if(nextPageCount==null||nextPageCount.isEmpty()) 
        { 
         return; 
        } 
        else { 
         int count = Integer.parseInt(nextPageCount); 
         getList(count); 
        } 
       } 
      }; 
      customList.setAdapter(adapter); 
      break; 
     case 3: 
      adapter = new PlaylistAdapter(getContext(), false, false){ 
       @Override 
       protected void onOverScrolled() { 
        super.onOverScrolled(); 
        if(nextPageCount==null||nextPageCount.isEmpty()) 
        { 
         return; 
        } 
        else { 
         int count = Integer.parseInt(nextPageCount); 
         getList(count); 
        } 
       } 
      }; 
      customList.setAdapter(adapter); 
      break; 
    } 


} 

答えて

0

あなたが最良の戦略は、新しいファイルにクラスでCustomAdapterアダプタを作成あなたの活動でそれをインポートして、インスタンスにあなたがそれを必要とするたびに作成され、同じアダプターを使用している場合。

アダプタを設定しない場合は、アダプタを設定して、default:に設定してください。ここにはアダプタが1つだけ必要です。

1

Adaptersをすべて同じクラスにすることはできませんか?

public class BaseAdapter extends SomeAdapterOfYours { 
    //... everything needed 

    @Override 
    protected void onOverScrolled() { 
     super.onOverScrolled(); 
     if(nextPageCount==null||nextPageCount.isEmpty()){ 
      return; 
     } 
     else { 
      int count = Integer.parseInt(nextPageCount); 
      getList(count); 
     } 
    } 
} 

そして、あなたのカスタム・アダプター:

public class ArtistAdapter extends BaseAdapter { 
    //custom adapter stuff 
} 

public class SongAdapter extends BaseAdapter { 
    //custom adapter stuff 
} 

のように...

関連する問題