*Android 学习之 Map , List*
点击前
进入界面后
点击cha01
进入hello界面(界面未设置)
点击进入test界面(界面未设置)
具体实现
:
在安卓的项目下新建俩个包,包里再设置新的界面
在安卓的Androidmanifest文件中注册四个界面
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sshhsun.mapidemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.sshhsun.test01.Mhello01"
android:label="chap01/Hello" >
<intent-filter>
<action android:name="com.sshhsun.test.Sample" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.sshhsun.test01.Mtest01"
android:label="chap01/test" >
<intent-filter>
<action android:name="com.sshhsun.test.Sample" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.sshhsun.test02.Mhello02"
android:label="chap02/Hello" >
<intent-filter>
<action android:name="com.sshhsun.test.Sample" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.sshhsun.test02.Mtest02"
android:label="chap02/test" >
<intent-filter>
<action android:name="com.sshhsun.test.Sample" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MHelloActivity" >
</activity>
</application>
</manifest>
主代码中
package com.sshhsun.mapidemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
List<Map<String, Object>> data = getData();
String[] from = { "title" };// 代表数据从哪里来
int[] to = { android.R.id.text1 };// 数据显示到哪里
SimpleAdapter adapter = new SimpleAdapter
(this,
data,
android.R.layout.simple_list_item_1,
from,
to);// 适配器,用来将数据和视图进行绑定
setListAdapter(adapter);
}
public List<Map<String,Object>> getData()
{
Intent mstartIntent = getIntent();
String mbasePath = mstartIntent.getStringExtra("prefixPath");//获取点击的章节信息
List<Map<String, Object>> data=new ArrayList<Map<String,Object>>();
Intent intent2=new Intent();
intent2.setAction("com.sshhsun.test.Sample" );
PackageManager mpm=getPackageManager();
List<ResolveInfo> infos=mpm.queryIntentActivities(intent2, 0);
Map <String,Boolean> ismap=new HashMap <String,Boolean>();
for(ResolveInfo info:infos)
{
String label=info.loadLabel(mpm).toString();
String[] labelPath = label.split("/");
if(mbasePath==null||"".equals(mbasePath))
{
String mprefix=labelPath[0];
if(ismap.get(mprefix)==null)
{
Map <String,Object> map=new HashMap<String, Object>();
ismap.put(mprefix, true);
map.put("title",mprefix );
Intent i=new Intent(this,MainActivity.class);
i.putExtra("prefixPath", mprefix);
map.put("intent", i);
data.add(map);
}
}else if(label.startsWith(mbasePath))
{
String title=labelPath[1];
Map<String,Object> map=new HashMap<String,Object>();
Intent i=new Intent();
i.setClassName(info.activityInfo.packageName, info.activityInfo.name);
map.put("title", title);
map.put("intent",i);
data.add(map);
}
}
return data;
}
public List<Map<String,Object>> getTestData()
{
List<Map<String, Object>> data=new ArrayList<Map<String,Object>>();
Map<String,Object> map1=new HashMap<String, Object>();
map1.put("title", "part 1");
map1.put("intent", new Intent(this,MHelloActivity .class));
data.add(map1);
Map<String,Object> map2=new HashMap<String, Object>();
map2.put("title", "part 2");
Intent i=new Intent(this,MainActivity .class);
i.putExtra("position",1);
map2.put("intent",i);
data.add(map2);
Map<String,Object> map3=new HashMap<String, Object>();
map3.put("title", "part 3");
Intent i2=new Intent(this,MainActivity .class);
i2.putExtra("position",2);
map3.put("intent",i2);
data.add(map3);
Map<String,Object> map4=new HashMap<String, Object>();
map4.put("title", "part 4");
Intent i3=new Intent(this,MainActivity .class);
i3.putExtra("position",3);
map4.put("intent",i3);
data.add(map4);
String[] mfrom={"title"};
int[] mto={android.R.id.text1};
SimpleAdapter adapter=new SimpleAdapter(this, data, android.R.layout.simple_list_item_1, mfrom, mto);
setListAdapter(adapter);
Intent intent1=getIntent();
int p=intent1.getIntExtra("position",-1);
if(p!=-1){//判定通过意图传递的参数
Map<String,Object> map5=new HashMap<String, Object>();
map5.put("title", "点击了"+p+"行");
data.add(map5);
}return data;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
//Log.e("","");
// Intent intent=new Intent(this,MainActivity.class);//创建意图,再次启动MainActivity
// intent.putExtra("position", position);//在意图中放入参数
Map<String,Object> map=(Map<String,Object>)l.getItemAtPosition(position);
Intent mintent=(Intent) map.get("intent");
startActivity(mintent);
}
}
“`