ListView根本不显示 [英] ListView not showing at all
问题描述
我已经设置了一个自定义适配器,并且在列表视图中填充了数据,问题是列表视图没有显示出来.在调试模式下,listView显示它确实已连接了适配器,但看不到任何东西.
I have setup a custom adapter, and inflating the data in a list view, problem is list view not showing up. In debugging mode the listView is showing that it indeed has an adapter attached to it, but I can't see any thing.
这是初始化适配器的代码.
Here is the code to initialize adapter.
contactAdapter = new ContactAdapter(this, R.layout.contact_layout, contactList);
master_listView.setAdapter(contactAdapter);
这是ContactAdapter本身的代码
Here the code for ContactAdapter itself
public class ContactAdapter extends BaseAdapter {
Contact contact;
Context context;
LayoutInflater layoutInflater;
List<Contact> listContact;
int resourceID;
public ContactAdapter(Context context, int layoutResourceID, List<Contact> listContact) {
this.context = context;
this.listContact = listContact;
resourceID = layoutResourceID;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent){
if(convertView==null){
layoutInflater = ((Activity) context).getLayoutInflater();
convertView = layoutInflater.inflate(resourceID, parent, false);
}
contact = listContact.get(position);
TextView tv_name = (TextView)convertView.findViewById(R.id.contact_tvName);
TextView tv_number = (TextView)convertView.findViewById(R.id.contact_tvNumber);
tv_name.setText(contact.getName());
tv_number.setText(contact.getNumber());
return convertView;
}
}
这里是用于保存数据的数据结构.
Here is data structure for saving data.
public class Contact {
String number;
String name;
public Contact(String name, String number) {
super();
this.name = name;
this.number = number;
}
public String getNumber() {
return number;
}
public String getName() {
return name;
}
}
最后是布局
<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="match_parent"
android:background="#ffbababa"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:padding="12dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/relativeLayout">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ib_Speak"
android:background="@drawable/mic_icon"
android:textColor="#ffffff"
android:layout_alignTop="@+id/tvSTT"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak Something!"
android:textColor="#ff343134"
android:textSize="16sp"
android:id="@+id/tvSTT"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_centerHorizontal="true"
android:layout_below="@+id/relativeLayout" />
</RelativeLayout>
</RelativeLayout>
我的版式是否存在问题,因为我没有收到任何错误,据我所知,所有数据都已正确设置. 帮助将不胜感激.
Is there a problem with my layout, because I am not getting any kind of error, also all the data is correctly setup, as far as I know. Help would be appreciated.
推荐答案
您的getCount()
方法返回0
@Override
public int getCount() {
return 0;
}
更改它以返回列表的大小
change it to return the size of your list
@Override
public int getCount() {
return listContact.size();
}
这篇关于ListView根本不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!