• android listview simpleAdaper


    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"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainDemo" >
    
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="" />
        
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    
    </LinearLayout>

    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"
        android:orientation="horizontal"
        tools:context=".MainActivity"
        android:id="@+id/datalist">
        
            <ImageView 
             android:id="@+id/pic"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="left"
             android:src="@drawable/ic_launcher"/>
            
        <LinearLayout 
           
           android:layout_width="180px"
           android:layout_height="wrap_content"
           android:orientation="vertical"
           tools:context=".MainActivity"
           android:gravity="left">
      
      
        <TextView 
           android:id="@+id/company"
           android:textSize="16px"
           android:padding="3px"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>
      
        <TextView 
           android:id="@+id/ceo"
           android:padding="3px"
           android:textSize="10px"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>   
    </LinearLayout>
        <LinearLayout 
          
           android:layout_width="200px"
           android:layout_height="wrap_content"
           android:orientation="vertical"
           tools:context=".MainActivity"
           android:gravity="left">
      
      
        <TextView 
           android:id="@+id/type"
           android:textSize="16px"
           android:padding="1px"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>
      
        <ImageView 
           android:id="@+id/score"
           android:padding="1px"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>  
        
    </LinearLayout>
    </LinearLayout>

    java 

    package app.android.MainDemo;
    
    import android.os.Bundle;
    
    import java.text.Collator;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.Activity;
    import android.app.ListActivity;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.content.pm.ResolveInfo;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainDemo extends Activity {
    
        private int pic[]=new int[]
                {
                R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                };
        
        private String data[][]=new String[][]
                {
                {"百度","李彦宏","搜索",R.drawable.ic_launcher+""},
                {"阿里巴巴","马云","电子商务",R.drawable.ic_launcher+""},
                {"腾讯","马化腾","社交网络",R.drawable.ic_launcher+""},
                {"新浪","曹国伟","社交媒体",R.drawable.ic_launcher+""},
                {"腾讯","马化腾","社交网络",R.drawable.ic_launcher+""},
                {"新浪","曹国伟","社交媒体",R.drawable.ic_launcher+""},
                };
                
                private ListView dataList=null;
                private List<Map<String,String>> list=new ArrayList<Map<String,String>>();
                private SimpleAdapter simpleAdapter=null;
                private TextView info=null;
                
           
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_demo);
           
            this.dataList=(ListView) super.findViewById(R.id.datalist);
            
            this.info=(TextView)super.findViewById(R.id.info);
            
    //
    //      //将我们每一条记录添加到我们的Map当中
            for(int i=0;i<data.length;i++){
            //定义我们的Map数据
            Map<String,String> map=new HashMap<String, String>();
            //添加数据
            
            map.put("pic", String.valueOf(pic[i]));
            map.put("company",data[i][0]);
            map.put("ceo", data[i][1]);
            map.put("type", data[i][2]);
            map.put("score", data[i][3]);
            this.list.add(map);
            }
            
            this.simpleAdapter=new SimpleAdapter
                    (
                    this, 
                    this.list, 
                    R.layout.maindemo_datalist, 
                    new String[]{"pic","company","ceo","type","score"}, 
                    new int[]{R.id.pic,R.id.company,R.id.ceo,R.id.type,R.id.score}
                    );
          this.dataList.setAdapter(simpleAdapter);
          this.dataList.setOnItemClickListener(new  OnItemClickListener() 
          {
    
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                
                @SuppressWarnings("unchecked")
                Map<String,String> map=(Map<String, String>)
               MainDemo.this.simpleAdapter.getItem(arg2);
                
                Toast.makeText(MainDemo.this, "onItemClick=>:"+arg2, 3000).show();
                
                String company=map.get("company");
                String ceo=map.get("ceo");
                String type=map.get("type");
                MainDemo.this.info.setText("您选择的公司是:=>"+company+"ceo为"+ceo+"type:"+type);
                
            }
        });
          
        }
        
      
    
    
    }

    效果:

  • 相关阅读:
    Windows10 JDK1.8安装及环境变量配置
    Adobe Premiere Pro 2020破解教程
    如何消除任务栏系统更新失败的图标
    微服务架构-Gradle下载安装配置教程
    第十五次-语法制导的语义翻译
    第十四次——算符优先分析
    第09组 Alpha冲刺(4/4)
    第09组 Alpha冲刺(3/4)
    第09组 Alpha冲刺(2/4)
    第09组 Alpha冲刺(1/4)
  • 原文地址:https://www.cnblogs.com/laopo/p/5823996.html
Copyright © 2020-2023  润新知