2014-09-03 94 views
1

所以我试图用自定义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

+0

向你展示* xml *文件和你检查列表是否为emtpy的代码来切换** textView的可见性** – 2014-09-03 00:58:01

+0

通过将textview的id声明为'android:id =“@ android:id/empty“',如果列表为空,android知道显示这个文本。它是自动的。请注意,该id是Android默认的'@android:id/empty',而不是'@ + id/empty' – Pedro 2014-09-03 01:07:04

回答

1

你没有切换的TextView的知名度,是无形的,如果的ListView是空的,尽量去除TextView的和运行应用程序时,的ListView应该出现。

使用空的TextView您应该让您的活动延伸ListViewActivity或者您在使用listView.setEmptyView(youTextView);处理能见度切换的Java在设置空的TextView的ListView

+1

visiblity =“gone”解决了它。非常感谢! – Pedro 2014-09-03 01:29:18

+0

检查我的答案更新,如果listView真的是空的,则显示空的textview – 2014-09-03 01:30:00

相关问题