import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clickBtn(View view)
{
int id=view.getId();
if(id==R.id.btn1)
{
//显式的Intent启动Activity
Intent intent=new Intent(this,FirstActivity.class);
startActivity(intent);
}else if(id==R.id.btn2)
{
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);
finish();
}else if(id==R.id.btn3)
{
Intent intent=new Intent(this,ThirdActivity.class);
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package veryedu.cls9.lession4.listview;
import java.text.ChoiceFormat;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
ListView list1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
list1=(ListView)findViewById(R.id.arrayList);
String[] weeks={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
ArrayAdapter adapter=
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, weeks);
list1.setAdapter(adapter);
list1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
// TextView txt=(TextView)view;
// Toast.makeText(FirstActivity.this,"aaa:", Toast.LENGTH_LONG)
// .show();
Log.i("mytest", "pos:"+pos+" id="+id);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
}
package veryedu.cls9.lession4.listview;
import java.text.ChoiceFormat;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
ListView list1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
list1=(ListView)findViewById(R.id.arrayList);
String[] weeks={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
ArrayAdapter adapter=
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, weeks);
list1.setAdapter(adapter);
list1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
// TextView txt=(TextView)view;
// Toast.makeText(FirstActivity.this,"aaa:", Toast.LENGTH_LONG)
// .show();
Log.i("mytest", "pos:"+pos+" id="+id);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
}
package veryedu.cls9.lession4.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ThirdActivity extends Activity {
ListView listview3=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
listview3=(ListView)findViewById(R.id.listview3);
MyListAdapter adapter=new MyListAdapter(this);
listview3.setAdapter(adapter);
}
//列表数据的优化要做到能够分页读取数据
public class MyListAdapter extends BaseAdapter
{
Context context;
LayoutInflater inflater=null;//布局文件的解析器
List list=new ArrayList();
public MyListAdapter(Context context)
{
this.context=context;
//获得布局文件解析器
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list=loadData();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=null;
//convertView代表小时的行的内容 我们可以重用这个view
//提高效率节省内存空间
if(convertView==null)
{
Log.i("mytest", "创建新View");
//解析行布局文件 并生成对应的View对象
view= inflater.inflate(R.layout.item2, null);
}else
{
view=convertView;
}
Log.i("mytest","pos:"+position+" view="+view+" convertView="+convertView);
//获得这一行要显示的数据
final Map data=(Map)list.get(position);
//把数据填充进view的子控件中
Integer icon_resId=(Integer)data.get("icon");
Bitmap icon= BitmapFactory.decodeResource(context.getResources(), icon_resId);
//获得ImageView对象
ImageView imgView=(ImageView) view.findViewById(R.id.gameIcon);
imgView.setImageBitmap(icon);
TextView text=(TextView)view.findViewById(R.id.gameTitle);
text.setText(""+data.get("title"));
Button btn=(Button)view.findViewById(R.id.downloadBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "开始下载"+data.get("title"), Toast.LENGTH_LONG).show();
}
});
return view;
}
public List<Map<String, Object>> loadData()
{
List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
Map<String, Object> m1=new HashMap<String, Object>();
m1.put("icon", R.drawable.bird);
m1.put("title","愤怒的小鸟");
Map<String, Object> m2=new HashMap<String, Object>();
m2.put("icon", R.drawable.cat);
m2.put("title","愤怒的小猫");
Map<String, Object> m3=new HashMap<String, Object>();
m3.put("icon", R.drawable.chicken);
m3.put("title","愤怒的小鸡");
Map<String, Object> m4=new HashMap<String, Object>();
m4.put("icon", R.drawable.cow);
m4.put("title","愤怒的奶牛");
Map<String, Object> m5=new HashMap<String, Object>();
m5.put("icon", R.drawable.dog);
m5.put("title","愤怒的小狗");
Map<String, Object> m6=new HashMap<String, Object>();
m6.put("icon", R.drawable.duck);
m6.put("title","愤怒的鸭子");
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
list.add(m5);
list.add(m6);
for(int i=0;i<20;i++)
{
Map<String, Object> m=new HashMap<String, Object>();
m.put("icon", R.drawable.cow);
m.put("title","愤怒的奶牛"+i);
list.add(m);
}
return list;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.third, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="veryedu.cls9.lession4.listview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="veryedu.cls9.lession4.listview.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="veryedu.cls9.lession4.listview.FirstActivity"
android:label="@string/title_activity_first" >
</activity>
<activity
android:name="veryedu.cls9.lession4.listview.SecondActivity"
android:label="@string/title_activity_second" >
</activity>
<activity
android:name="veryedu.cls9.lession4.listview.ThirdActivity"
android:label="@string/title_activity_third" >
</activity>
</application>
</manifest>