简体   繁体   English

android - 列表视图和适配器不显示任何项目:(

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

I have a problem in my Android application.我的 Android 申请有问题。 I have a Custom ListView Adapter, but when I launch the application the list does not show any item!我有一个自定义 ListView 适配器,但是当我启动应用程序时,列表不显示任何项目!

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.更好的是,将您的活动设为 ListActivity ,Android 将为您完成很多工作。

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);并删除: 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:或者你可以在你的 xml 中定义一个列表,并且,如上所述,找到它是这样的:

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

and in xml:在 xml 中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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