ListView not showing at all

安稳与你 提交于 2019-11-26 22:27:28

问题


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);

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;
}
}

And finally here is the layout

<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.


回答1:


Your getCount() method is returning 0

@Override
public int getCount() {
    return 0;
}

change it to return the size of your list

@Override
public int getCount() {
    return listContact.size();
}


来源:https://stackoverflow.com/questions/27229843/listview-not-showing-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!