我编写了自己的ListView BaseAdapter。我有一个有23个托盘的ArrayList。前七个来自实例游戏,后一个来自实例表。但如果我运行ListView,我只得到前七项。如果我不重复使用convertview(... if(convertview == null || true){...)我会得到每23个项目。没有重新使用这些物品会有问题吗?
package k0f.de.americanfootball;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by benjaminbecker on 26.02.16.
*/
public class AmericanFootballAdapter extends BaseAdapter {
private final LayoutInflater inflator;
private ArrayList<Object> arrayList = new ArrayList<Object>();
public AmericanFootballAdapter(Context context) {
inflator = LayoutInflater.from(context);
}
public void setNewDataGames(ArrayList<Object> arrayList){
this.arrayList=arrayList;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Context context = parent.getContext();
// falls nötig, convertView bauen
// Holder bereits vorhanden
if (convertView == null) {
// Layoutdatei entfalten
if((position%2)==0){
convertView = inflator.inflate(R.layout.listitem_game, parent,
false);
}else{
convertView = inflator.inflate(R.layout.listitem_table, parent,
false);
}
// Holder erzeugen
holder = new ViewHolder();
Object ob = getItem(position);
if(ob instanceof AmericanFootballDataGames){
holder = holderGames(holder, convertView, context, (AmericanFootballDataGames) ob);
convertView.setTag(holder);
}
if(ob instanceof AmericanFootballDataTable){
holder = holderTable(holder, convertView, context, (AmericanFootballDataTable) ob);
convertView.setTag(holder);
}
} else {
Log.v("Ben","HolderGame");
holder = (ViewHolder) convertView.getTag();
convertView.setTag(holder);
} /*else {
Log.v("Ben","HolderTable");
holderG = (ViewHolderGame) convertView.getTag();
convertView.setTag(holderG);
}
*/
return convertView;
}
static class ViewHolder {
TextView tv1, tv2, tv3;
}
private ViewHolder holderGames(ViewHolder holder, View convertView, Context context, AmericanFootballDataGames afdg){
String erg = afdg.getTdheim() + " : " +
afdg.getTdgast() + " (" +
afdg.getQ1heim() + " : " +
afdg.getQ1gast() + " , " +
afdg.getQ2heim() + " : " +
afdg.getQ2gast() + " , " +
afdg.getQ3heim() + " : " +
afdg.getQ3gast() + " , " +
afdg.getQ4heim() + " : " +
afdg.getQ4gast() + ")";
holder.tv2 = (TextView) convertView.findViewById(R.id.game);
holder.tv3 = (TextView) convertView.findViewById(R.id.erg);
holder.tv1 = (TextView) convertView.findViewById(R.id.date);
holder.tv1.setText(afdg.getAnstoss()+System.getProperty("line.separator")+afdg.getDatum());
holder.tv2.setText(afdg.getHeim() + System.getProperty("line.separator") + afdg.getGast());
holder.tv3.setText(erg);
if(afdg.getTdheim().toString()=="?") {
holder.tv3.setTextColor(ContextCompat.getColor(context, R.color.colorListHalfLight));
}else{
holder.tv3.setTextAppearance(context, android.R.style.TextAppearance_Medium);
holder.tv3.setTextColor(ContextCompat.getColor(context, R.color.colorListHalfDark));
}
return holder;
}
private ViewHolder holderTable(ViewHolder holder, View convertView, Context context, AmericanFootballDataTable afdt){
holder.tv2 = (TextView) convertView.findViewById(R.id.game);
holder.tv3 = (TextView) convertView.findViewById(R.id.erg);
holder.tv1 = (TextView) convertView.findViewById(R.id.date);
holder.tv1.setText(Integer.toString(afdt.getPlatz()));
holder.tv2.setText(afdt.getMannschaft());
holder.tv3.setText(afdt.getPplus()+" "+afdt.getPminus());
return holder;
}
}
这是包含和不包含表数据的屏幕。
答案 0 :(得分:1)
您将覆盖getViewTypeCount()以返回2,表示此适配器可以处理两种不同类型的视图。如果这样做,您还必须覆盖getItemViewType()以返回一个int,指示哪个类型的视图位于给定位置。然后,您需要在getView()中为该位置充气正确的视图,以便ListView可以正确地回收和重用该视图以用于该类型的其他项目。
答案 1 :(得分:0)
发现我的错误。在返回convertView之前必须在holder上设置文本;而不是在复杂的情况下。