首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Listview in layout Android

Listview in layout Android
EN

Stack Overflow用户
提问于 2011-12-06 01:14:43
回答 3查看 723关注 0票数 1

我有一个string-array,我需要在布局中显示它。代码当前显示了列表,但列表不在布局中,它创建了一个新的“屏幕”,其中您只能看到列表。

这是我无法在布局列表视图中显示列表的代码。

代码语言:javascript
复制
import android.app.ListActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class idchart extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.idchart);

    Resources res = getResources();

 // To get the string array associated with particular resource ID.
 String[] ids = res.getStringArray(R.array.idchart_array);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.test_list_item,ids);
 setListAdapter(adapter);
 }

}

布局:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_main"
android:orientation="vertical" >


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/logo" />


<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<RelativeLayout
    android:id="@+id/relative"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</RelativeLayout>

</LinearLayout>

我还没能找到任何有帮助的东西:(所以我希望你能。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-06 01:29:06

代码语言:javascript
复制
ListView listView=(ListView)findViewById(R.id.lv);
//rest of code
listView.setAdapter(adapter);

而且您可以避免扩展ListActivity。

如果您使用的是ListActivity,那么

代码语言:javascript
复制
ListView listView=getListView();
票数 1
EN

Stack Overflow用户

发布于 2011-12-06 01:21:30

您的idchart类应该是常规Activity的子类,而不是仅用于显示列表的ListActivity

更新

替换

代码语言:javascript
复制
public class idchart extends ListActivity 

使用

代码语言:javascript
复制
public class idchart extends Activity 
票数 0
EN

Stack Overflow用户

发布于 2011-12-06 02:04:10

如果你扩展ListActivity,你的ListView必须有一个特定的id:

代码语言:javascript
复制
android:id="@id/android:list"

从文档中:

代码语言:javascript
复制
ListActivity has a default layout that consists of a single, full-screen list in the 
center of the screen. However, if you desire, you can customize the screen layout by
setting your own view layout with setContentView() in onCreate(). To do this, your own
view MUST contain a ListView object with the id "@android:id/list"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8389122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档