Android:切换到ListActivity时无法设置setContentView

Android:切换到ListActivity时无法设置setContentView,第1张

概述[更新]我收到错误,上面写着“你的内容必须有一个ListView,其id属性是’ android.R.id.list’”.我的xml中似乎没有任何东西是ListView.但这需要吗? 这是我上一个问题的后续问题 android: which view should I use for showing text and image? 我阅读了有关为LinearLayout创建ListView的文章. [更新]我收到错误,上面写着“你的内容必须有一个ListVIEw,其ID属性是’ android.R.ID.List’”.我的xml中似乎没有任何东西是ListVIEw.但这需要吗?

这是我上一个问题的后续问题
android: which view should I use for showing text and image?

我阅读了有关为linearLayout创建ListVIEw的文章.但是,当我将“extends Activity”更改为“扩展ListActivity”时,我的以下代码在setContentVIEw()函数中失败,任何想法为什么?

private TextVIEw mSelection;//private ImageVIEw mImages;static final String[] keywords = new String[]{"China","Japan","USA","Canada"};/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.contactLayout);    mSelection = (TextVIEw)findVIEwByID(R.ID.Contactnames);    ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.contactlayout,R.ID.Contactnames,keywords);    setlistadapter(adapter);    }

我的布局来自这篇文章:http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent"androID:layout_height="?androID:attr/ListPreferredItemHeight"androID:padding="6dip"><ImageVIEw    androID:ID="@+ID/icon"    androID:layout_wIDth="wrap_content"    androID:layout_height="fill_parent"    androID:layout_marginRight="6dip"    androID:src="@drawable/icon" /><linearLayout    androID:orIEntation="vertical"    androID:layout_wIDth="0dip"    androID:layout_weight="1"    androID:layout_height="fill_parent">    <TextVIEw        androID:ID="@+ID/Contactnames"        androID:layout_wIDth="fill_parent"        androID:layout_height="0dip"        androID:layout_weight="1"        androID:gravity="center_vertical"        androID:text="My Application" />    <TextVIEw        androID:layout_wIDth="fill_parent"        androID:layout_height="0dip"        androID:layout_weight="1"         androID:singleline="true"        androID:ellipsize="marquee"        androID:text="Simple application that shows how to use relativeLayout" /></linearLayout>
解决方法 我想你误解了我在上一个问题中向你展示的其他帖子.他们解释了如何为列表中的每一行使用自定义布局,而不是如何为活动定义整个布局文件.你需要这样的东西:

(main.xml)<?xml version="1.0" enCoding="utf-8"?><ListVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:cachecolorHint="#00000000"    androID:ID="@androID:ID/List"></ListVIEw>

注意非常重要的一行androID:ID =“@ androID:ID / List”.你必须在ListVIEw中拥有它,因为它告诉AndroID你的列表在哪里.如果背景不是黑色,则cachecolorHint非常有用 – 有关详细信息,请参阅this post.

通过上述行,您可以为活动提供一个可以正确识别的列表.这是一个基本的例子:

public class TestProject extends ListActivity {    final static String[] ITEMS = {"blah","floop","gnarlp","stuff"};    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.Listrow,R.ID.textvIEw,ITEMS);        setlistadapter(adapter);    }}

那么Listrow布局就是这样的:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content">    <TextVIEw androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:ID="@+ID/textvIEw"/></linearLayout>

这是一个非常简单的布局.如果你想要更复杂的东西,你必须使用BaseAdapter进行更改,因为这样可以在每行之前调用getVIEw(…).您可以根据每行的内容使用不同的布局.但是,BaseAdapter在您第一次尝试时看起来很可怕,所以请注意! 总结

以上是内存溢出为你收集整理的Android:切换到ListActivity时无法设置setContentView全部内容,希望文章能够帮你解决Android:切换到ListActivity时无法设置setContentView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1122807.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存