更新:此根本原因不在此代码中。 显然它与我放在列表视图背景中的图像视图有关。似乎列表位于背景图像下方的屏幕底部。 但是,奇怪的是,相同的布局在邻居选项卡中也很好用,它也显示了列表视图。
我有一个应该显示64个数据项的列表适配器。就Log.d语句显示,getCount
确实返回64。但是getView
只被调用一次,索引i
为0。
到目前为止,getView末尾记录的数据看起来不错。
对不起那堆代码,但我不知道哪个部分是问题所在。如果您认为错误可能出现在列表片段本身或您建议查看的内容中,我很乐意分享更多内容。
public class SpieleAdapter extends BaseAdapter {
Context context;
DateFormat df = new SimpleDateFormat("dd.mm.yyyy HH:mm");
public SpieleAdapter (Context context) {
super();
this.context = context;
}
@Override
public int getCount() {
Log.d("SpieleAdapter", "Anzahl spiele: " + WorldCupManager.getGameList().size());
return WorldCupManager.getGameList().size();
}
@Override
public Object getItem(int i) {
return WorldCupManager.getGameList().get(i);
}
@Override
public long getItemId(int i) {
return (long) i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Log.d("SpieleAdapter", "Liefere View Nr. " + i);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.zelle_spiel, viewGroup, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.gruppeView = (TextView) view.findViewById(R.id.gruppeText);
viewHolder.heimView = (TextView) view.findViewById(R.id.heimText);
viewHolder.gastView = (TextView) view.findViewById(R.id.gastText);
viewHolder.heimToreView = (TextView) view.findViewById(R.id.heimToreText);
viewHolder.gastToreView = (TextView) view.findViewById(R.id.gastToreText);
viewHolder.zeitOrtView = (TextView) view.findViewById(R.id.zeitOrtText);
viewHolder.zusatzView = (TextView) view.findViewById(R.id.zusatzText);
view.setTag(viewHolder);
}
ViewHolder viewHolder = (ViewHolder) view.getTag();
Spiel spiel = (Spiel) WorldCupManager.getGameList().get(i);
Gruppe gruppe = spiel.gruppe;
viewHolder.gruppeView.setText(gruppe.name);
Team heim = spiel.heim;
viewHolder.heimView.setText (heim == null ? spiel.platzhalterHeim : heim.name);
Team gast = spiel.gast;
viewHolder.heimView.setText (gast == null ? spiel.platzhalterGast : gast.name);
viewHolder.heimView.setText(spiel.heim.name);
viewHolder.gastView.setText(spiel.gast.name);
viewHolder.heimToreView.setText("" + spiel.toreHeim);
viewHolder.gastToreView.setText("" + spiel.toreGast);
viewHolder.zusatzView.setText(spiel.zusatz);
viewHolder.zeitOrtView.setText(df.format(spiel.anstoss) + " in " + spiel.ort.name);
Log.d("SpieleAdapter", "Daten: " + viewHolder.gruppeView.getText () + " " + viewHolder.heimView.getText () + " " + viewHolder.gastView.getText () + " " + viewHolder.zeitOrtView.getText () + " " + viewHolder.gruppeView.getText () + " " );
return view;
}
static class ViewHolder {
public TextView gruppeView;
public TextView heimView;
public TextView gastView;
public TextView heimToreView;
public TextView gastToreView;
public TextView zeitOrtView;
public TextView zusatzView;
}
}
和相关的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dip"
android:padding="4dip" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="4dip"
android:paddingTop="0dip"
android:alpha="0.2"
android:background="@android:color/black">
</RelativeLayout>
<TextView
android:id="@+id/gruppeText"
android:layout_width="fill_parent"
android:layout_height="14dip"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:text="Gruppe A"
android:textColor="@android:color/white"
android:alpha="0.5"
android:textStyle="bold"
android:textSize="10dip"/>
<TextView
android:id="@+id/heimText"
android:layout_width="wrap_content"
android:layout_below="@id/gruppeText"
android:layout_height="20dip"
android:singleLine="true"
android:text="Heim Team"
android:textColor="@android:color/white"
android:alpha="0.5"
android:textStyle="bold"
android:textSize="16dip"/>
<TextView
android:id="@+id/heimToreText"
android:layout_width="wrap_content"
android:layout_below="@id/gruppeText"
android:layout_toRightOf="@id/heimText"
android:layout_height="20dip"
android:singleLine="true"
android:text="12"
android:textColor="@android:color/white"
android:alpha="0.5"
android:gravity="right"
android:layout_alignParentRight="true"
android:textStyle="bold"
android:textSize="16dip"/>
<TextView
android:id="@+id/gastText"
android:layout_width="wrap_content"
android:layout_below="@id/heimText"
android:layout_height="20dip"
android:singleLine="true"
android:text="Gast Team"
android:textColor="@android:color/white"
android:alpha="0.5"
android:textStyle="bold"
android:textSize="16dip"/>
<TextView
android:id="@+id/gastToreText"
android:layout_width="wrap_content"
android:layout_below="@id/heimText"
android:layout_toRightOf="@id/gastText"
android:layout_height="20dip"
android:singleLine="true"
android:text="12"
android:textColor="@android:color/white"
android:alpha="0.5"
android:gravity="right"
android:layout_alignParentRight="true"
android:textStyle="bold"
android:textSize="16dip"/>
<TextView
android:id="@+id/zeitOrtText"
android:layout_width="wrap_content"
android:layout_below="@id/gastText"
android:layout_height="14dip"
android:singleLine="true"
android:text="Datum, Uhrzeit und Ort"
android:textColor="@android:color/white"
android:alpha="0.5"
android:textStyle="bold"
android:textSize="12dip"/>
<TextView
android:id="@+id/zusatzText"
android:layout_width="wrap_content"
android:layout_below="@id/gastText"
android:layout_toRightOf="@id/zeitOrtText"
android:layout_height="14dip"
android:singleLine="true"
android:text="12"
android:textColor="@android:color/white"
android:alpha="0.5"
android:gravity="right"
android:layout_alignParentRight="true"
android:textStyle="bold"
android:textSize="12dip"/>
</RelativeLayout>
我目前在Mac上使用Android Studio,如果这有所不同。
编辑以回应Arash的问题: 这是设置适配器的片段类。
public class SpieleFragment extends ListFragment {
private SpieleAdapter spieleAdapter = null;
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
//Check UI Thread
boolean onUiThread = (Looper.myLooper() == Looper.getMainLooper());
if (onUiThread) {
Log.d("SpieleFragment", "runs on UI Thread");
} else {
Log.d("SpieleFragment", "does not run un UI Thread");
}
if (spieleAdapter != null) {
spieleAdapter.notifyDataSetChanged();
}
Log.d("SpieleFragment", "Got message data changed");
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_spiele, container, false);
spieleAdapter = new SpieleAdapter(this.getActivity());
setListAdapter(spieleAdapter);
LocalBroadcastManager.getInstance(WMApp.getContext()).registerReceiver(mMessageReceiver,
new IntentFilter(WorldCupManager.UPDATE_EVENT));
return rootView;
}
@Override
public void onDestroy() {
// Unregister since the activity is about to be closed.
// This is somewhat like [[NSNotificationCenter defaultCenter] removeObserver:name:object:]
LocalBroadcastManager.getInstance(WMApp.getContext()).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
答案 0 :(得分:1)
我认为你使用了不同的类对象作为参考,所以请参考下面的代码
ViewHolder viewHolder=null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.zelle_spiel, viewGroup, false);
viewHolder = new ViewHolder();
.....
.....
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag();
}
......
......
答案 1 :(得分:1)
我不知道是否存在误解,但您已发布SpieleAdapter
,但在ListFragment
您已初始化GruppenAdapter