Arowana
Arowana

Reputation: 193

Android custom adapter and ListView shows nothing

I want to create a ListView populated by my own layout and my data. So I looked at examples and I found that I need to extend ArrayAdapter<>. So I made a CustomAdapter and did everything by following an example.

There is no error in the code but the ListView @id/dashboard doesn't appear in my MainActivity. I can see the TextView from activity_main.xml but not the ListView. The sliding menu of the DrawerLayout is working too.

I inflate overview.xml in my CustomAdapter.

Can you give me an hint why it's not working? I'm working on it for hours.

Thank's

CustomAdapter

public class CustomAdapter extends ArrayAdapter<Ssoverview> {

    private int layoutId;
    private Context context;
    private ArrayList<Ssoverview> list;

    public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
        super(context, resource);
        this.layoutId = resource;
        this.context = context;
        this.list = ssList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;

        if (v == null) {
            v = (RelativeLayout) ((Activity)context).getLayoutInflater().inflate(this.layoutId, null); //, parent, false);
        }

        Ssoverview ss = this.list.get(position);
        if (ss != null) {
            TextView titleTV=(TextView) v.findViewById(R.id.title);
            titleTV.setText(ss.getTitle());
            TextView authorTV=(TextView) v.findViewById(R.id.author);
            authorTV.setText(ss.getAuthor());
        }
        Log.d("Deb", "Fabrication de la View : "+position);
        return v;
    }

}

activity_main.xml :

 <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:orientation="vertical">  
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Ole" />           
            <ListView
                android:id="@+id/dashboard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true" >
            </ListView>
        </RelativeLayout>
    </FrameLayout>

    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

overview.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@color/grey"
        android:layout_marginTop="10sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Titre" />

        <TextView
            android:id="@+id/note"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:text="Note : x/5 y votes" />

        <TextView
            android:id="@+id/author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="35dp"
            android:text="John Doe" />
    </RelativeLayout>

And the MainActity where I set the adapter

public class MainActivity extends Activity {

    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.dashboard);

        //Declaration factices des Ssoverview
        ArrayList<Ssoverview> ssList = new ArrayList<Ssoverview>();
        Ssoverview ss1 = new Ssoverview("Dune", "David", "4");
        Ssoverview ss2 = new Ssoverview("Le petit poucet", "MLP", "2");
        Ssoverview ss3 = new Ssoverview("Locked", "Hervé", "4,2");

        ssList.add(ss1);
        ssList.add(ss2);
        ssList.add(ss3);

        Log.d("D", ssList.get(0).getTitle());

        //Declaration de l'adapter avec le layout et la ssList
        CustomAdapter adapter = new CustomAdapter(this, R.layout.overview, ssList);


        lv.setAdapter(adapter);

        Log.d("D","Fin de onCreate");
    }

}

Upvotes: 1

Views: 869

Answers (1)

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

change

public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
        super(context, resource);
        this.layoutId = resource;
        this.context = context;
        this.list = ssList;
    }

to

public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
        super(context, resource , ssList); <-- change this line
        this.layoutId = resource;
        this.context = context;
        this.list = ssList;
    }

in arrayAdapter class super in constructor handle size of your list, as you don't pass your data this set to 0 in default so getView not called at all

Upvotes: 2

Related Questions