简体   繁体   中英

android - listview and Adapter does not show any item:(

I have a problem in my Android application. I have a Custom ListView Adapter, but when I launch the application the list does not show any item!

My Code:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class AccountContents extends Activity {
    private ListView ls1;
    String username = fourshared.username;
    String password = fourshared.password;
    private AccountItem[] rootContents;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rootContents = fourshared.rootContents;
        CArrayAdapter adapter = new CArrayAdapter(AccountContents.this, rootContents);  
        setContentView(R.layout.main);
        ls1 = new ListView(AccountContents.this); 
        ls1.setAdapter(adapter);
    }
}

Does your layout contain a list view? If so, you should look that up and set the adapter on that:

ls1 = (ListView)findViewById(R.id.your_list_view_id);

Better still, make your activity a ListActivity and Android will do a lot of the work for you.

You need to add the list to your activity. To do this, add this line:

setContentView(R.layout.main);

and remove: setContentView(R.layout.main);

This will add the list into view, but remove the existing views.

or you can define a list in your xml, and, as mentioned above, find it like this:

ls1 = (ListView)findViewById(R.id.your_list_view_id);

and in xml:

<ListView android:id="@+id/ls1" ......allOtherAtributesHere...... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM