2017-02-05 27 views
0

Я не могу заставить мой RecyclerAdapter правильно реагировать на добавление данных. Вот моя активность, которая использует RecyclerViewnotifyDataSetChanged не работает с Custom RecyclerAdapter

public class MainActivity extends AppCompatActivity { 
FloatingActionButton fab; 
FloatingActionMenu fam; 

//startActivity(intent); 

private RecyclerView mRecyclerView; 
ArrayList<TeamInfo> teamsInfo; 
SelectedTeamsRecyclerAdapter adapter; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    /*if(savedInstanceState == null) { 

     if(teamsInfo == null) { 
      Log.d("TEAM SELECTION", "teams Info == null"); 
      teamsInfo = new ArrayList<>(); 
     } 
    } 
    else { 
     if(teamsInfo == null) { 
      teamsInfo = savedInstanceState.getParcelableArrayList("selectedTeams"); 
     } 

    }*/ 
    teamsInfo = new ArrayList<>(); 
    setContentView(R.layout.activity_main); 
    fab = (FloatingActionButton) findViewById(R.id.menu_item); 
    fam = (FloatingActionMenu) findViewById(R.id.menu); 
    fam.setClosedOnTouchOutside(true); 

    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show(); 
      addClicked(); 
     } 
    }); 




    mRecyclerView = (RecyclerView) findViewById(R.id.selected_teams_recyclerview); 
    adapter = new SelectedTeamsRecyclerAdapter(this, teamsInfo); 
    adapter.setHasStableIds(true); 
    mRecyclerView.setAdapter(adapter); 

    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 
    //teamsInfo = TeamInfo.restoreTeamInfo(this); 


    //adapter.setHasStableIds(true); 


    //mRecyclerView.setItemAnimator(null); 

    //DragSortRecycler dragSortRecycler = new DragSortRecycler(); 
    mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build()); 
    //mRecyclerView.addOnItemTouchListener(dragSortRecycler); 
    //mRecyclerView.addOnScrollListener(dragSortRecycler.getScrollListener()); 
    //dragSortRecycler.setViewHandleId(R.id.selected_teams_draghandle); //View you wish to use as the handle 


    /*dragSortRecycler.setOnItemMovedListener(new DragSortRecycler.OnItemMovedListener() { 
     @Override 
     public void onItemMoved(int from, int to) { 
      itemMoved(from, to); 
     } 
    }); 
    */ 

} 

public void itemMoved(int from, int to) { 
    TeamInfo item = teamsInfo.remove(from); 
    teamsInfo.add(to, item); 
    adapter.notifyItemInserted(teamsInfo.size() - 1); 
} 

public void addClicked() { 

    fam.close(true); 
    getNewTeamValues(); 

    //Intent intent = new Intent(this, NewTeamActivity.class); 


} 

public void addNewTeam(String teamName, int primaryColor, int secondaryColor) { 
    teamsInfo.add(new TeamInfo(teamName, primaryColor, secondaryColor)); 
    //adapter.mTeamsInfo.add(new TeamInfo(teamName, primaryColor, secondaryColor)); 
    //adapter.addItem(new TeamInfo(teamName, primaryColor, secondaryColor)); 
    adapter.notifyDataSetChanged(); 
} 

public void getNewTeamValues() { 


    TextView nameTV;s 
    MaterialDialog dialog = new MaterialDialog.Builder(this) 
      .onPositive(new MaterialDialog.SingleButtonCallback() { 
       @Override 
       public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { 
        // TODO 

        LineColorPicker primaryColorPicker = (LineColorPicker) dialog.getCustomView().findViewById(R.id.primaryColor); 
        LineColorPicker secondaryColorPicker = (LineColorPicker) dialog.getCustomView().findViewById(R.id.secondaryColor); 
        EditText newTeamNameEditText = (EditText) dialog.getCustomView().findViewById(R.id.teamNameEditText); 
        View v = dialog.getContentView(); 
        String teamName = newTeamNameEditText.getText().toString(); 
        int primaryColor = primaryColorPicker.getColor(); 
        int secondaryColor = secondaryColorPicker.getColor(); 

        addNewTeam(teamName, primaryColor, secondaryColor); 

       } 
      }) 
      .title("Enter Team Info") 
      .customView(R.layout.dialog_new_team, true) 
      .positiveText("Add") 
      .negativeText("Cancel").build(); 

    dialog.show(); 
} 




} 

А вот мой заказ адаптер

public class SelectedTeamsRecyclerAdapter extends RecyclerView.Adapter<SelectedTeamsRecyclerAdapter.ViewHolder>{ 

public ArrayList<TeamInfo> mTeamsInfo; 
Context mContext; 

public SelectedTeamsRecyclerAdapter(Context context, ArrayList<TeamInfo> teamsInfo) { 
    mTeamsInfo = teamsInfo; 
    mContext = context; 

} 

public class ViewHolder extends RecyclerView.ViewHolder { 

    public TextView teamNameTV; 
    public View primaryColorV; 
    public View secondaryColorV; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     teamNameTV = (TextView) itemView.findViewById(R.id.selected_teams_teamname); 
     primaryColorV = (View) itemView.findViewById(R.id.selected_teams_primarycolor); 
     secondaryColorV = (View) itemView.findViewById(R.id.selected_teams_secondarycolor); 
    } 
} 

@Override 
public int getItemCount() { 
    return mTeamsInfo.size(); 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    TeamInfo teamInfo = mTeamsInfo.get(position); 
    holder.teamNameTV.setText(teamInfo.getTeamName()); 
    holder.primaryColorV.setBackgroundColor(teamInfo.getPrimaryColor()); 
    holder.secondaryColorV.setBackgroundColor(teamInfo.getSecondaryColor()); 

} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(
      R.layout.selected_teams_list_item, parent, false); 
    ViewHolder holder = new ViewHolder(view); 
    return holder; 
} 


@Override 
public long getItemId(int position) { 
    return position; 
} 

public void addItem(TeamInfo teamInfo) { 
    mTeamsInfo.add(teamInfo); 

} 

/*public ArrayList<TeamInfo> getTeamsInfo() { 
    return this.teamsInfo; 
}*/ 


} 

Я написал почти точно такой же код в прошлом, и все работало нормально.

ответ

0

Прежде всего вы не добавили mRecyclerView.setLayoutManager(mLayoutManager) попытайтесь добавить и сообщить мне, если у вас возникли проблемы снова.

+0

О, боже мой. Я потратил часы вчера вечером, пытаясь понять это. Вот что я получаю для кодирования в 3:00 утра. – Sheel7

 Смежные вопросы

  • Нет связанных вопросов^_^