通过在我的getView()中创建一个开关块,我终于有幸得到了节标题。这样做我已经使用我的适配器创建了一个问题,因为现在我的ListView一遍又一遍地重复顶部项目。我找到了其他有类似问题的人,但是他们通过添加convertView.setTag(holder)来解决;我已经有了这个,所以我相信我的问题与我设置开关块的方式有关。可能是语法问题导致事情无法正确对齐。
任何帮助将不胜感激。这是我的适配器:
public class PlayerAdapter extends BaseAdapter {
private Context mContext;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<Player> mPlayers = new ArrayList<>();
private TreeSet<Integer> sectionHeader = new TreeSet<>();
public PlayerAdapter(Context context, ArrayList<Player> players) {
mContext = context;
mPlayers = players;
}
public void addItem(final Player player) {
mPlayers.add(player);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final Player player) {
mPlayers.add(player);
sectionHeader.add(mPlayers.size() -1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mPlayers.size();
}
@Override
public Object getItem(int position) {
return mPlayers.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public boolean isEnabled(int position) {
int rowType = getItemViewType(position);
if(rowType == TYPE_SEPARATOR) {
return false;
}
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
int rowType = getItemViewType(position);
if (convertView == null) {
Player player = mPlayers.get(position);
switch (rowType){
case TYPE_ITEM:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_list_item_layout, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.playerNameTextView);
holder.playerValueTextView = (TextView) convertView.findViewById(R.id.playerValueTextView);
holder.remainingCapTextView = (TextView) convertView.findViewById(R.id.remainingCapTextView);
holder.username = (TextView) convertView.findViewById(R.id.opponentUsername);
holder.status = (TextView) convertView.findViewById(R.id.statusTextView);
holder.vsTeamAbbrev = (TextView) convertView.findViewById(R.id.vsTeam);
holder.resultsTextView = (TextView) convertView.findViewById(R.id.resultsTextView);
DecimalFormat formatter = new DecimalFormat("$#,###");
holder.playerNameTextView.setText(player.getName());
holder.playerValueTextView.setText(formatter.format(Double.parseDouble(player.getValue())));
holder.remainingCapTextView.setText(formatter.format(Double.parseDouble(player.getCap())));
holder.username.setText(player.getUsername());
holder.status.setText(player.getStatus());
holder.vsTeamAbbrev.setText(player.getVsTeamAbbrev());
holder.resultsTextView.setText(player.getResultsTextView());
if (player.isMatchMade()) {
holder.status.setVisibility(View.VISIBLE);
}
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_section_header, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.lastPlayer);
holder.lastPlayerDate = (TextView) convertView.findViewById(R.id.lastPlayerDate);
holder.playerNameTextView.setText(player.getName());
holder.lastPlayerDate.setText(player.getLastPlayerDate());
convertView.setTag(holder);
break;
}
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private static class ViewHolder {
//item views
TextView playerValueTextView;
TextView playerNameTextView;
TextView remainingCapTextView;
TextView username;
TextView status;
TextView vsTeamAbbrev;
TextView resultsTextView;
//section header views
TextView lastPlayer;
TextView lastPlayerDate;
}
}
答案 0 :(得分:0)
这是一个非常简单的错误。我不得不将填充我的视图的代码移到else子句下面。这样,如果转换视图不为null,它仍然可以gettag()并填充数据。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
Player player = mPlayers.get(position);
int rowType = getItemViewType(position);
if (convertView == null) {
switch (rowType){
case TYPE_ITEM:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_list_item_layout, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.playerNameTextView);
holder.playerValueTextView = (TextView) convertView.findViewById(R.id.playerValueTextView);
holder.remainingCapTextView = (TextView) convertView.findViewById(R.id.remainingCapTextView);
holder.username = (TextView) convertView.findViewById(R.id.opponentUsername);
holder.status = (TextView) convertView.findViewById(R.id.statusTextView);
holder.vsTeamAbbrev = (TextView) convertView.findViewById(R.id.vsTeam);
holder.resultsTextView = (TextView) convertView.findViewById(R.id.resultsTextView);
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_section_header, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.lastPlayer);
holder.lastPlayerDate = (TextView) convertView.findViewById(R.id.lastPlayerDate);
convertView.setTag(holder);
break;
}
//convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (rowType){
case TYPE_ITEM:
DecimalFormat formatter = new DecimalFormat("$#,###");
holder.playerNameTextView.setText(player.getName());
holder.playerValueTextView.setText(formatter.format(Double.parseDouble(player.getValue())));
holder.remainingCapTextView.setText(formatter.format(Double.parseDouble(player.getCap())));
holder.username.setText(player.getUsername());
holder.status.setText(player.getStatus());
holder.vsTeamAbbrev.setText(player.getVsTeamAbbrev());
holder.resultsTextView.setText(player.getResultsTextView());
if (player.isMatchMade()) {
holder.status.setVisibility(View.VISIBLE);
}
break;
case TYPE_SEPARATOR:
holder.playerNameTextView.setText(player.getName());
holder.lastPlayerDate.setText(player.getLastPlayerDate());
break;
}
return convertView;
}
private static class ViewHolder {
//item views
TextView playerValueTextView;
TextView playerNameTextView;
TextView remainingCapTextView;
TextView username;
TextView status;
TextView vsTeamAbbrev;
TextView resultsTextView;
//section header views
TextView lastPlayer;
TextView lastPlayerDate;
}
}