Android简单的ListViewDemo及每个控件的点击事件

 

ListView是什么?

ListView是一个 数据控件,可以展示从数据库中读取的数据。是.net3.5的新控件。

它比gridview更灵活,而且支持多种模板,支持分页。

 


 

 文章地址 http://www.cnblogs.com/mmyblogs/p/6058322.html(转载请保留)


 

 

接下来我就为大家演示一个简单的利用SimpleAdapter实现ListView及其点击事件的例子。

 

main.xml主页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

list_item.xml每个item的排布

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="60dp"
        android:layout_height="60dp">

        <ImageView
            android:id="@+id/image"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"/>

        <TextView
            android:id="@+id/context"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="15sp"/>

    </LinearLayout>

</LinearLayout>

MainActivity.java核心代码

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import android.widget.SimpleAdapter;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.widget.Toast;

public class MainActivity extends Activity
{
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        List<Map<String,Object>>datalist=new ArrayList<Map<String,Object>>();
        ListView lv=(ListView) findViewById(R.id.lv);
        
        Map<String,Object> map1=new HashMap<String,Object>();
        map1.put("image",R.mipmap.ic_launcher);
        map1.put("title","AIDE资源");
        map1.put("context","AIDE   资源  分享");
        Map<String,Object> map2=new HashMap<String,Object>();
        map2.put("image",R.mipmap.ic_launcher);
        map2.put("title","了解AIDE");
        map2.put("context","AIDE   了解  分享");
        Map<String,Object> map3=new HashMap<String,Object>();
        map3.put("image",R.mipmap.ic_launcher);
        map3.put("title","玩转AIDE");
        map3.put("context","AIDE   玩转  分享");
        Map<String,Object> map4=new HashMap<String,Object>();
        map4.put("image",R.mipmap.ic_launcher);
        map4.put("title","控件之TextView");
        map4.put("context","AIDE   TextView  分享");
        
        datalist.add(map1);
        datalist.add(map2);
        datalist.add(map3);
        datalist.add(map4);
        
        lv.setAdapter(new SimpleAdapter(this,datalist,R.layout.list_item,new String[]{"image","title","context"},new int[]{R.id.image,R.id.title,R.id.context}));
        lv.setOnItemClickListener(new OnItemClickListener(){
            //list点击事件
                @Override
                public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
                {
                    // TODO: Implement this method
                    switch(p3){
                        case 0://第一个item
                            Toast.makeText(MainActivity.this,"AIDE   资源  分享",Toast.LENGTH_SHORT).show();
                            break;
                            case 1://第二个item
                            Toast.makeText(MainActivity.this,"AIDE   了解  分享",Toast.LENGTH_SHORT).show();
                                break;
                                case 2://第三个item
                            Toast.makeText(MainActivity.this,"AIDE   玩转  分享",Toast.LENGTH_SHORT).show();
                                    break;
                    }
                }
                
            
        });
        
        }
}
以上就是全部代码,下面是效果图

转载于:https://www.cnblogs.com/mmyblogs/p/6058322.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值