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;
                    }
                }
                
            
        });
        
        }
}

以上就是全部代碼,下面是效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM