Android学习第三天--listview方式七

跟六有点不同

主xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

<ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

放置内容的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" >

    <ImageView
        android:id="@+id/iv_headid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_stunameid"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/tv_stutelephoneid"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

实体类

package cn.core.entity;

import java.util.HashMap;
import java.util.Map;

public class Students {
    private Integer stuId;
    private String stuName;
    private String stuTelephone;
    private Map<String,String> map;
    private Integer stuHead;
    
    public Students() {
        super();
    }
    public Students(Integer stuId, String stuName, String stuTelephone, Integer stuHead) {
        super();
        this.stuId = stuId;
        this.stuName = stuName;
        this.stuTelephone = stuTelephone;
        this.stuHead = stuHead;
    }




    public Map<String,String> getMap() {
        
        map =new HashMap<String,String>();
        map.put("stuHead", String.valueOf(stuHead));
        map.put("stuName", stuName);
        map.put("stuTelephone", stuTelephone);        
        
        return map;
    }
    
    
    
    public Integer getStuId() {
        return stuId;
    }
    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public String getStuTelephone() {
        return stuTelephone;
    }
    public void setStuTelephone(String stuTelephone) {
        this.stuTelephone = stuTelephone;
    }
    
}

主java类

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.core.entity.Students;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
    private static final String TAG="LAOGUO";
    List<Map<String,String>> list= null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView listView=(ListView)findViewById(R.id.listview1);
        
        Students stu1=new Students(1,"是","13860483016",R.drawable.head1);
        Students stu2=new Students(2,"的飞","15959440256",R.drawable.head2);
            
        
        
        list=new ArrayList<Map<String,String>>();
        
        list.add(stu1.getMap());
        list.add(stu2.getMap());
        
        
        
        
        ListAdapter listAdapter = new SimpleAdapter(this, list, R.layout.activity_lv, new String[]{"stuHead","stuName","stuTelephone"}, new int[]{R.id.iv_headid,R.id.tv_stunameid,R.id.tv_stutelephoneid});
        
        listView.setAdapter(listAdapter);
        
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                String str=arg0.getItemAtPosition(position).toString();
                String tel=(String)list.get(position).get("stuTelephone");
                    
                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+tel));
                MainActivity.this.startActivity(intent);
                
                
                return false;
            }
        });
        
        
        
    }


}

 

posted @ 2013-03-10 00:27  小三小山  阅读(142)  评论(0编辑  收藏  举报