加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Android:ListView和重写后MyListView的简单使用1

(2023-07-01 20:24:11)
分类: androidios
转:https://blog.csdn.net/ssh159/article/details/55210899

什么是 ListView ?它有什么用?


如这个页面,多个新闻信息:,一个个在xml写是不可能的!

所以,我们需要用ListView循环写!

一个LinearLayout 布局中,

就一个图片,一个标题,一个时间,一个评论数量!是不是很少啊?



一、普通的ListView 例子

为了方便大家理解,我就写个超级简单的例子吧:

代码的组成:一个方法,2个xml页面,图片若张(自备)




1、EsotericActivity.java

(我这里的xml 很多重复,所以我用了循环,但是有时候还是不能偷懒)

    package com.open_open.android_plantest;
     
    import android.app.Activity;
    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 EsotericActivity extends Activity {
        private ListView listOne;
        //定义一个集合存放所有学生信息
        private SimpleAdapter adapter;
        private List> data=null;
        private String planArray[]={"训练须知","训练须知2","训练须知3","训练须知4","训练须知5","训练须知6"};
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_esoteric);
     
            listOne = (ListView) findViewById(R.id.listOne);
    //构建适配器【首先定义好自己的布局】
            getData();  //获取数据
            String[] from = {"Photo1", "Title1", "Title2", "Photo2"};
            int[] to = {R.id.photo1, R.id.title1, R.id.title2, R.id.photo2};
            adapter = new SimpleAdapter(this, data, R.layout.activity_plan, from, to);
            listOne.setAdapter(adapter);
     
        }
        private void getData() {
            data = new ArrayList>();
    //构建一项内容
            Map item = new HashMap();
     
            for(int i=0;i
                item = new HashMap();
                item.put("Photo1", R.drawable.plan1);
                item.put("Title1", planArray[i]);
                item.put("Title2", "");
                item.put("Photo2", R.drawable.go_in);
                data.add(item);
            }
        }
     
    }


正常的情况:getDate() 的写法

  private void getData() {
        data = new ArrayList>();
//构建一项内容
        Map item=new HashMap();

        item=new HashMap();
        item.put("Photo",R.drawable.intel);
        item.put("Title","Intel/英特尔 I7-4790K LGA 1150 4.0GHZ");
        item.put("Test","英特尔官方旗舰店");
        item.put("Comment","成交:14");
        data.add(item);

        item=new HashMap();
        item.put("Photo",R.drawable.intel);
        item.put("Title","英特尔至强/Xeon E5-2630v2 6核12线程2.6GHzDell/戴尔 E5-2630v2");
        item.put("Test","酷睿专卖店");
        item.put("Comment","成交:7");
        data.add(item);

        item=new HashMap();
        item.put("Photo",R.drawable.jijia);
        item.put("Title","GIGABYTE/技嘉GTX1080 Xtreme W-8G萤火虫 水之力GTX1080游戏显卡");
        item.put("Test","技嘉旗舰店");
        item.put("Comment","成交:13");
        data.add(item);

        item=new HashMap();
        item.put("Photo",R.drawable.shandi);
        item.put("Title","Sandisk/闪迪 SDSSDA-120G SSD笔记本 台式机电脑固态硬盘非128G");
        item.put("Test","和瑟数码专营店");
        item.put("Comment","成交:1711");
        data.add(item);

        item=new HashMap();
        item.put("Photo",R.drawable.sanxing);
        item.put("Title","Kingston/金士顿 SV300S37A/120G");
        item.put("Test","易华旗舰店");
        item.put("Comment","成交:65");
        data.add(item);

        item=new HashMap();
        item.put("Photo",R.drawable.sanxing);
        item.put("Title","发布在即:三星Galaxy C5 Pro获Wi-Fi认证");
        item.put("Test","三星旗舰店");
        item.put("Comment","成交:142");
        data.add(item);

        item=new HashMap();
        item.put("Photo",R.drawable.leishe);
        item.put("Title","Razer/雷蛇 炼狱蝰蛇 3500dpi 激光 5个");
        item.put("Test","雷蛇亿果专卖店  ");
        item.put("Comment","成交:2w");
        data.add(item);
    }




2、activity_esoteric.xml




   
   
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.open_open.android_plantest.EsotericActivity">
     
                            android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/cblue"
                android:orientation="horizontal"
                android:gravity="center">
     
                                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                                            android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/go_back"
                        android:gravity="left"
                        android:layout_margin="5dp"/>
                                            android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="训练秘籍"
                        android:textSize="20dp"
                        android:textColor="@android:color/white"
                        android:textStyle="bold"
                        android:layout_margin="10dp"
                        android:layout_centerHorizontal="true" />
               
           
     
                            android:id="@+id/listOne"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@color/cbrown"
                android:dividerHeight="0.3dp"
                android:listSelector="@android:color/holo_blue_light"/>
     
       
     



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有