一:main 界面代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/myList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="2px"
android:layout_marginLeft="4px">
</ListView>
</LinearLayout>
guideLIst 界面代码:
<?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="horizontal">
<ImageView
android:id="@+id/ivimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3px"
android:layout_marginTop="3px"/>
<TextView
android:id="@+id/tvMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="80px"
android:textSize="30sp"/>
</LinearLayout>
JAVA代码:
package com.example.dell.test;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView myList;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.myList);
List<HashMap <String,Object>>list =new ArrayList<HashMap<String,Object>>();//定义List 里面存储的是Hashmap类型的元素
HashMap<String,Object>map1=new HashMap<String, Object>();
HashMap<String,Object>map2=new HashMap<String, Object>();
HashMap<String,Object>map3=new HashMap<String, Object>();
HashMap<String,Object>map4=new HashMap<String, Object>();
HashMap<String,Object>map5=new HashMap<String, Object>();
map1.put("image",R.drawable.baidu);
map1.put("name","百度");
map2.put("image",R.drawable.xinlang);
map2.put("name","新浪");
map3.put("image",R.drawable.tenxent);
map3.put("name","腾讯");
map4.put("image",R.drawable.guge);
map4.put("name","谷歌");
map5.put("image",R.drawable.souhu);
map5.put("name","搜狐");
list.add(map1);//将每个map添加到list中
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
/*
*声明 SimpleAdapter
*第一个参数是 Activity上下文
*第二个参数是 数据源
*第三个参数是 每一行的布局文件资源文件
*第四个参数是HashMap中的key;
*第五个参数是 guidelist.xml文件中组件的id*/
SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.guidelist,new String[]{"image","name"},new int[] {R.id.ivimg,R.id.tvMsg});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new Action());
}
class Action implements AdapterView.OnItemClickListener {
private Intent intent;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
intent =new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
startActivity(intent);
break;
case 1:
intent =new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.sina.com"));
startActivity(intent);
break;
case 2:
intent =new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
startActivity(intent);
break;
case 3:
intent =new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.qq.com"));
startActivity(intent);
break;
case 4:
intent =new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(intent);
break;
case 5:
intent =new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.souhu.com"));
startActivity(intent);
break;
}
}
}
}
//注意为用户添加联网许可