我有一个自定义列表适配器的活动。如果填充列表视图的数组列表为空,则向用户显示文本框,否则文本框变为不可见,并显示列表视图。每当数组列表为空时,文本框就会出现,但当数组不存在时,我会看到空白屏幕。我不确定这是因为我的列表视图是不可见的还是因为它没有正确填充。这个工作昨天完全正常,但自那时以来我对这个类做了很多改变,我不确定哪一个导致了这个问题。我一直在尝试遵循过去几个小时的代码来解决这个问题,我注意到如果我在适配器的getView()
方法中添加一个log
调用,它将不会显示,如果我将它添加到构造函数中,它会显示精细。我还确保调用适配器,并使用log
在if语句中输入正确的条件。我希望你们中的一个能够发现问题,因为我不能。listview填充不正确/不可见
方法,它调用适配器:
活动的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout23"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<fragment
android:id="@+id/fragment"
android:name="com.example.albert.betterapp.menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_menu" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:id="@+id/openbetslayout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="49"
android:tag=""
android:background="@drawable/bettypeselector"
android:clickable="true">
<TextView
android:id="@+id/openbetstxtview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:clickable="false"
android:gravity="center"
android:text="Open Bets"
android:textColor="#B4B5AE"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/seperator">
</LinearLayout>
<LinearLayout
android:id="@+id/closedbetslayout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="49"
android:tag=""
android:background="@drawable/bettypeselector"
android:clickable="true">
<TextView
android:id="@+id/settledbetstxtview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:clickable="false"
android:gravity="center"
android:text="Settled Bets"
android:textColor="#B4B5AE"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="fill_parent"
android:id = "@+id/displayallbetsrefresh"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/nobetstxtbox"
android:text="You currently don't have any open bets saved."
android:textStyle="bold"
android:layout_gravity="center"
android:gravity="center"
android:visibility="gone"
android:textSize="17sp"
android:textColor="#067103"/>
<ListView
android:id="@+id/betslistviews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/betcolor"></ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
适配器:
private class MyListAdapter1 extends ArrayAdapter<BetDisplayer> {
public MyListAdapter1() {
super(DisplayAllBets.this, R.layout.activity_singletotalbet, openbetsarray);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("TESTING", "123123123");
itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.activity_singletotalbet, parent, false);
}
Button v = (Button) itemView.findViewById(R.id.detailsbutton);
BetDisplayer currentwriter = openbetsarray.get(position);
Log.d("TESTING", currentwriter.getSelections());
Log.d("TESTING2", currentwriter.getSelections());
String selections = currentwriter.getSelections();
int numberofselections = 0;
for (int i = 0; i < selections.length(); i++) {
if (selections.charAt(i) == '/') {
numberofselections++;
}
}
if (numberofselections == 1) {
TextView descriptor = (TextView) itemView.findViewById(R.id.no);
descriptor.setText("Single");
} else if (numberofselections == 2) {
TextView descriptor = (TextView) itemView.findViewById(R.id.no);
descriptor.setText("Double");
} else if (numberofselections == 3) {
TextView descriptor = (TextView) itemView.findViewById(R.id.no);
descriptor.setText("Treble");
} else {
TextView descriptor = (TextView) itemView.findViewById(R.id.no);
descriptor.setText("Accumulator" + "(" + numberofselections + ")");
}
TextView status = (TextView) itemView.findViewById(R.id.status);
status.setText(currentwriter.getStatus());
Log.d("ERRORDEBUG", currentwriter.toString());
if (status.getText().toString().equals("open")) {
TextView winnings = (TextView) itemView.findViewById(R.id.winnings);
winnings.setText("-");
/*LinearLayout displaylayout = (LinearLayout) itemView.findViewById(R.id.displaylayout);
displaylayout.setBackgroundColor(Color.parseColor("#FFFFFF"));
Button b = (Button) itemView.findViewById(R.id.detailsbutton);
b.setBackgroundResource(R.drawable.btnback);*/
}
else if (status.getText().toString().equals("lost")) {
TextView winnings = (TextView) itemView.findViewById(R.id.winnings);
winnings.setText("0");
/*LinearLayout displaylayout = (LinearLayout) itemView.findViewById(R.id.displaylayout);
Button b = (Button) itemView.findViewById(R.id.detailsbutton);
b.setBackgroundResource(R.drawable.btnbacklost);
displaylayout.setBackgroundResource(R.drawable.lost);*/
}
else {
TextView winnings = (TextView) itemView.findViewById(R.id.winnings);
winnings.setText(Integer.toString(currentwriter.getReturns()));
/* Button b = (Button) itemView.findViewById(R.id.detailsbutton);
b.setBackgroundResource(R.drawable.btnback);
LinearLayout displaylayout = (LinearLayout) itemView.findViewById(R.id.displaylayout);
displaylayout.setBackgroundResource(R.drawable.won);*/
}
TextView winningss = (TextView) itemView.findViewById(R.id.winningss);
winningss.setText(Integer.toString(currentwriter.getReturns()));
TextView stakes = (TextView) itemView.findViewById(R.id.stakes);
stakes.setText(Integer.toString(currentwriter.getStake()));
TextView tokens = (TextView) itemView.findViewById(R.id.tokens);
tokens.setText(currentwriter.getId().substring(0,10));
TextView teams = (TextView) itemView.findViewById(R.id.teams);
TextView finalteams = (TextView) itemView.findViewById(R.id.actteams);
String userselectionssetter = currentwriter.getUserselections();
String actteamssetter = currentwriter.getActualselections();
teams.setText(userselectionssetter);
finalteams.setText(actteamssetter);
return itemView;
}
}
你记录测试结果表明,“getView”不会被调用(至少在你的MyListAdapter1实例)。需要检查的事项:阵列适配器是否有任何要显示的数据? (检查构造函数中openbetsarray的大小)。阵列适配器报告它是否正确计数? (检查getCount返回openbetsarray的大小)。 MyListAdapter1或MyListAdapter2是否被创建? –
@SoundConception我在适配器的构造函数中放置了一个日志调用,该函数返回openbetsarray.size(),并且它正确地返回2,表示适配器已创建并且具有要使用的数据。我不明白你对getCount方法的看法,以及我如何使用它。此外,有没有可能的列表视图不可见? – Alk
正如我所说不需要覆盖getCount()他的适配器从ArrayAdapter继承 –
ceph3us