zoukankan      html  css  js  c++  java
  • android中ListView的简单使用

    ListView控件在android中控件类中,算是比较复杂的一种,其实现一般包含以下几个部分

    1)数据的准备

    2)构建适配器

    3)显示数据

    在使用ListVIew这个控件时,我发现好多人都出现了这样的问题"your content must have a listview whose id attribute is"android.R.id.List”错误,大致如下图

    然后好多网友给出的答案就是,在.xml中 android:id="@+id/list" 改为 android:id="@+id/android:list“

    其实并不一定是这样的错误。首先要区别采用的是何种使用ListView方法。ListView的控件有两种使用方法,

    一、使用android内部的ListView【以一个例子来说明】

    1.主.xml文件 【注意红色部分】

    <LinearLayout 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"
        tools:context=".MainActivity"
        android:orientation="vertical">
     <ListView
            android:id="@+id/android:list"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:drawSelectorOnTop="false"
            android:scrollbars="vertical"
           
            ></ListView>
     </LinearLayout>

    2.ListView显示的内容.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/linelayout"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">
    <TextView
        android:id="@+id/key"
        android:layout_height="wrap_content"
       
        android:layout_width="wrap_content"
        android:textColor="#aa0000"
        />
    <TextView
         android:id="@+id/value"
        android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         />
    </LinearLayout>

    3.java文件

    继承了ListActivity类,而不是Activity类

    public class MainActivity extends ListActivity {

    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
    //注意此处  没有用findViewById来取得ListView控件的id

    //创建hashmap型数据
      ArrayList<HashMap<String,String>> mylist=new ArrayList<HashMap<String,String>>();

      HashMap<String,String> map1=new HashMap<String,String>();
      HashMap<String,String> map2=new HashMap<String,String>();

      map1.put("key", "张三");
      map1.put("value", "学生");
      map2.put("key", "李四");
      map2.put("value", "老师");
      mylist.add(map1);
      mylist.add(map2);
     //构建适配器
      SimpleAdapter listAdapter=new SimpleAdapter(this,mylist,R.layout.hashmap,
        new String[]{"key","value"},new int[]{R.id.key,R.id.value});

    //适配器中的数据时如何传递到ListView的,  
      setListAdapter(listAdapter);

    }

    二、自定义ListView

    1.主xml文件

    与上面只有一处区别

    android:id="@+id/list"

    2.listview的布局文件: 同上

    3.java文件

    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      listview=(ListView)findViewById(R.id.list);
      ArrayList<HashMap<String,String>> mylist=new ArrayList<HashMap<String,String>>();

      HashMap<String,String> map1=new HashMap<String,String>();
      HashMap<String,String> map2=new HashMap<String,String>();

      map1.put("key", "张三");
      map1.put("value", "学生");
      map2.put("key", "李四");
      map2.put("value", "老师");
      mylist.add(map1);
      mylist.add(map2);
     
      SimpleAdapter listAdapter=new SimpleAdapter(this,mylist,R.layout.hashmap,
        new String[]{"key","value"},new int[]{R.id.key,R.id.value});
      //listview这个对象添加setAdapter对象
     listview.setAdapter(listAdapter);

  • 相关阅读:
    webpack loader和插件的编写原理
    vue和react原理性知识点
    详谈Javascript类与继承
    vue项目中要实现展示markdown文件[转载]
    前端知识总结--2 js部分
    前端知识总结--html
    react相关知识点总结
    优秀文章
    项目部署服务器2
    项目部署服务器
  • 原文地址:https://www.cnblogs.com/stoneFang/p/6715341.html
Copyright © 2011-2022 走看看