zoukankan      html  css  js  c++  java
  • Android ListView的使用(一)

    初次接触listview,以为直接在Android Studio 中将控件给拖过去,就能够使用了,结果半天显示不了。

    后来总算知道原因了。

    先上代码: activity_main.xml 显示页面 里加入listview

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <ListView
            android:id="@+id/list_test"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.constraint.ConstraintLayout>

    显示代码:MainActivity

    package com.example.administrator.myapplication;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    
            setContentView(R.layout.activity_main);
    
    
            //要显示的数据
            String[] strs = {"111","222","333","444","555","666","777","888","999","101","102","103","104"};
            //创建ArrayAdapter
            ArrayAdapter<String> adapter = new ArrayAdapter<String>
                    (this,android.R.layout.simple_expandable_list_item_1,strs);
            //获取ListView对象,通过调用setAdapter方法为ListView设置Adapter设置适配器
            ListView list_test = (ListView) findViewById(R.id.list_test);
            list_test.setAdapter(adapter);
    
        }
    }

    到此,运行项目,已经可以显示了。

    这这里走了点弯路。

    1.首先listview的数据要通过适配器去添加adapter

    2.setContentView(R.layout.activity_main); 要放在前面,我是这么理解的,要先把页面个加载出来,才能将数据显示在页面上,不然没地方去显示。

  • 相关阅读:
    nyoj 311 完全背包
    nyoj 737 石子合并(一)
    nyoj 232 How to eat more Banana
    nyoj 456 邮票分你一半
    nyoj 236 心急的C小加
    nyoj 195 飞翔
    nyoj 201 作业题
    SOS 调试扩展 (SOS.dll)
    使用Windbg和SoS扩展调试分析.NET程序
    windbg命令分类与概述
  • 原文地址:https://www.cnblogs.com/sunxun/p/9236090.html
Copyright © 2011-2022 走看看