我有一個自定義列表適配器的活動。如果填充列表視圖的數組列表爲空,則向用戶顯示文本框,否則文本框變爲不可見,並顯示列表視圖。每當數組列表爲空時,文本框就會出現,但當數組不存在時,我會看到空白屏幕。我不確定這是因爲我的列表視圖是不可見的還是因爲它沒有正確填充。這個工作昨天完全正常,但自那時以來我對這個類做了很多改變,我不確定哪一個導致了這個問題。我一直在嘗試遵循過去幾個小時的代碼來解決這個問題,我注意到如果我在適配器的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