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

    上一章使用ListView和ArrayAdapter 进行了最简单的操作。

    本文来自文档来自:http://www.runoob.com/w3cnote/android-tutorial-adapter.html

    这里继续来学习SimpleAdapter 的使用。

    • BaseAdapter:抽象类,实际开发中我们会继承这个类并且重写相关方法,用得最多的一个Adapter!
    • ArrayAdapter:支持泛型操作,最简单的一个Adapter,只能展现一行文字~
    • SimpleAdapter:同样具有良好扩展性的一个Adapter,可以自定义多种效果!
    • SimpleCursorAdapter:用于显示简单文本类型的listView,一般在数据库那里会用到,不过有点过时, 不推荐使用!


    先上个效果图吧:有点类似QQ 里面 说说的 那种

    上代码

    先创建一个页面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_item"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.constraint.ConstraintLayout>

    第二步:创建一个用于listview显示的布局页面list_news.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
    
        <!-- 定义一个用于显示头像的ImageView -->
        <ImageView
            android:id="@+id/imgtou"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:baselineAlignBottom="true"
            android:paddingLeft="8dp" />
    
        <!-- 定义一个竖直方向的LinearLayout,把QQ呢称与说说的文本框设置出来 -->
        <LinearLayout
            android:id="@+id/new_line"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8px"
                android:textColor="#1D1D1C"
                android:textSize="20sp" />
    
            <TextView
                android:id="@+id/says"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8px"
                android:textColor="#B4B4B9"
                android:textSize="14sp" />
    
            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8px"
                android:textColor="#B4B4B9"
                android:textSize="14sp" />
    
        </LinearLayout>
    
    </LinearLayout>

    最后,上activity代码:

    package action.sun.com.listtest;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MainActivity extends AppCompatActivity {
    
        private String[] names = new String[]{"Tom", "Jack", "Json"};
        private String[] says = new String[]{"111111,2222222", "33333333~", "444444444~"};
        private String[] times = new String[]{"1天前", "3天前~", "2天前~"};
        private int[] imgIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < names.length; i++) {
                Map<String, Object> showitem = new HashMap<String, Object>();
                showitem.put("touxiang", imgIds[i]);
                showitem.put("name", names[i]);
                showitem.put("says", says[i]);
                showitem.put("time", times[i]);
                listitem.add(showitem);
            }
    
            //创建一个simpleAdapter
            SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem,
                    R.layout.list_news, new String[]{"touxiang", "name", "says","time"},
                    new int[]{R.id.imgtou, R.id.name, R.id.says, R.id.time});
            ListView listView = (ListView) findViewById(R.id.list_item);
            listView.setAdapter(myAdapter);
        }
    }

    这样,运行,效果就能够实现了,这个很简单就能够实现了。

  • 相关阅读:
    JVM 重排序
    Dispatcher & Redirect
    Struts2-ActionContext
    eclipse+tomcat+maven debug的时候总是出现source not found /Edit lookup path...的问题解决方案
    web Listener
    优质博客
    IDEA中jdk设置
    chrome json插件
    IDEA快速复习
    MarkDown编辑器下载
  • 原文地址:https://www.cnblogs.com/sunxun/p/9237355.html
Copyright © 2011-2022 走看看