为什么我的listview不起作用,android

为什么我的listview不起作用,android,第1张

概述我正在尝试为微调器和列表做一个简单的编码, 这只是为了显示数据.但是,我不知道,为什么我为列表视图写的数据不显示.任何人都可以帮我解决这个问题吗?这是我的编码.MainActivity.javapublicclassMainActivityextendsActivity{String[]Category={"ScienceT

我正在尝试为微调器和列表做一个简单的编码,
 这只是为了显示数据.
但是,我不知道,
为什么我为列表视图写的数据不显示.
任何人都可以帮我解决这个问题吗?

这是我的编码.

MainActivity.java

public class MainActivity extends Activity {    String[] category = {    "ScIEnce Trail",    "MegaBUG Return"};String [] months = {    "June",    "July",    "August",    "Septemeber",    "November",    "December",};Spinner s1;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        //GrIDVIEw        setlistadapter(new ArrayAdapter<String>(this,androID.R.layout.simple_List_item_1,category));         //SpinnerVIEw        s1 = (Spinner) findVIEwByID(R.ID.spinner1);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,androID.R.layout.simple_spinner_item, months);        s1.setAdapter(adapter);        s1.setonItemSelectedListener(new OnItemSelectedListener()        {            public voID onItemSelected(AdapterVIEw<?> arg0,VIEw arg1, int arg2, long arg3) {                int index = s1.getSelectedItemposition();                Toast.makeText(getBaseContext(), "You have seleted item :" + months[index] , Toast.LENGTH_SHORT).show();            }            public voID onnothingSelected(AdapterVIEw<?>arg0) {}            });        }        private voID setlistadapter(ArrayAdapter<String> arrayAdapter) {        // Todo auto-generated method stub    }        public voID onListItemClick(ListVIEw parent, VIEw v, int position,long ID)        {        Toast.makeText(this, "You have selected " + category[position], Toast.LENGTH_SHORT).show();        }        }

activity_main.xml中

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:paddingBottom="@dimen/activity_vertical_margin"    androID:paddingleft="@dimen/activity_horizontal_margin"    androID:paddingRight="@dimen/activity_horizontal_margin"    androID:paddingtop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" > <ListVIEw     androID:ID="@+ID/androID:List"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_below="@+ID/spinner1"     androID:layout_margintop="60dp" >                    </ListVIEw>    <Spinner        androID:ID="@+ID/spinner1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignleft="@+ID/ListVIEw1"        androID:layout_alignParenttop="true"        androID:layout_margintop="22dp" /></relativeLayout>

解决方法:

您的活动必须扩展ListActivity.

使用以下

      public class MainActivity extends ListActivity {    String[] category = {    "ScIEnce Trail",    "MegaBUG Return"};String [] months = {    "June",    "July",    "August",    "Septemeber",    "November",    "December",};Spinner s1;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        //GrIDVIEw        setlistadapter(new ArrayAdapter<String>(this,androID.R.layout.simple_List_item_1,category));         //SpinnerVIEw        s1 = (Spinner) findVIEwByID(R.ID.spinner1);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,androID.R.layout.simple_spinner_item, months);        s1.setAdapter(adapter);        s1.setonItemSelectedListener(new OnItemSelectedListener()        {            public voID onItemSelected(AdapterVIEw<?> arg0,VIEw arg1, int arg2, long arg3) {                int index = s1.getSelectedItemposition();                Toast.makeText(getBaseContext(), "You have seleted item :" + months[index] , Toast.LENGTH_SHORT).show();            }            public voID onnothingSelected(AdapterVIEw<?>arg0) {}            });        }}

删除它

     private voID setlistadapter(ArrayAdapter<String> arrayAdapter) {     }

public voID setlistadapter(listadapter adapter)// ListActivity的方法

在API级别1中添加

提供列表视图的光标.

更多信息 @

http://developer.android.com/reference/android/app/ListActivity.html

在我的设备上拍摄

总结

以上是内存溢出为你收集整理的为什么我的listview不起作用,android全部内容,希望文章能够帮你解决为什么我的listview不起作用,android所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1106766.html

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

发表评论

登录后才能评论

评论列表(0条)

保存