所以我試圖用自定義ArrayAdapter創建一個自定義ListView。 我得到一個加載ListView的片段,創建並設置我自己的ArrayAdapter,名爲CardsAdapter。帶有自定義ArrayAdapter的ListView沒有顯示項目
問題?該列表沒有顯示出來。只顯示設置爲顯示列表是否爲空的默認文本(android:id =「@ android:id/empty」)。
對於測試此方法的問題cards.createDummy();
用3個虛擬對象填充列表以顯示。要顯示
片段,我聲明並創建列表視圖
public class MainFragment extends Fragment implements Callback{
private ListView listView;
private CardsAdapter cardsAdapter;
private CardList cards; //contains the array to be shown
public MainFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cards = new CardList(getActivity().getApplicationContext());
cards.createDummy(); //Fills up dummy list
View view = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView) view.findViewById(R.id.list);
//Creates the adapter with the dummy list cards.cardList
cardsAdapter = new CardsAdapter(getActivity().getApplicationContext(),cards.cardList);
listView.setAdapter(cardsAdapter);
//don't bother with this
HttpHandle handle = new HttpHandle(listView, this);
handle.download("http://someapp.herokuapp.com/");
// Inflate the layout for this fragment
return view;
}
列表視圖的佈局文件(通訊員R.layout.fragment_main
) 我有列表和一個TextView如果列表爲空:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E6E6E6" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:text="Retrieving data"/>
</RelativeLayout>
我定製ArrayAdapter:
public class CardsAdapter extends ArrayAdapter<Card> {
public LinearLayout cardView;
public CardsAdapter(Context context, ArrayList cards) {
super(context, R.layout.card, cards);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.card, parent, false);
}
Card card = getItem(position);
this.cardView = (LinearLayout) rowView.findViewById(R.id.tableRow1);
TextView title = (TextView) rowView.findViewById(R.id.title);
title.setText(card.getTitle());
return rowView;
}
}
由CardsAdapter使用卡片佈局(在構造函數R.layout.card
參考) 文件:res/layout/card.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_marginBottom="7dp"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:adjustViewBounds="True"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:paddingLeft="7dp"
android:src="@drawable/photo" />
<TextView
android:id="@+id/title"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingLeft="3dp"
android:maxLines="2"
android:ellipsize="end"
android:text="@string/title"/>
<TextView
android:id="@+id/dateStart"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingLeft="3dp"
android:maxLines="2"
android:ellipsize="end"/>
</TableRow>
</LinearLayout>
注: 創建的虛擬目錄,而不是空:我與調試工具 的檢查的話應用程序運行,並不會崩潰。但是,這隻能說明空的TextView
向你展示* xml *文件和你檢查列表是否爲emtpy的代碼來切換** textView的可見性** – 2014-09-03 00:58:01
通過將textview的id聲明爲'android:id =「@ android:id/empty「',如果列表爲空,android知道顯示這個文本。它是自動的。請注意,該id是Android默認的'@android:id/empty',而不是'@ + id/empty' – Pedro 2014-09-03 01:07:04