所以我正在尝试使用自定义ArrayAdapter创建自定义ListView。 我有一个加载ListView的片段,创建并设置我自己的名为 CardsAdapter 的ArrayAdapter。
问题?列表没有显示出来。只显示默认文本(android:id =“@ android:id / empty”),设置为显示列表是否为空。
对于测试此方法的问题,cards.createDummy();
填写列表,显示3个虚拟对象。
我声明并创建listview的片段
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
)
我有列表,如果列表为空则显示文本视图:
<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
答案 0 :(得分:1)
如果 ListView 为空,您没有将 TextView 的可见性切换为不可见,请尝试删除 TextView 并运行应用程序, ListView 应该出现。
使用空TextView,您应该使活动扩展 ListViewActivity ,或者您必须为 ListView设置空 TextView 在 Java 中使用listView.setEmptyView(youTextView);
来处理可见性切换。