ListActivity学习

ListActivity 是Activity 的一个子类。Activity 类要通过setContentView(R.layout.main); 来加载一个xlm 布局。而如果我们的界面只需要一个ListView,那我们直接加载ListActivity 类就行了,不用xlm 布局了。Maxiee 照着网上的教程编了这样一个程序:

ListActivity类

ListAcitvity 实例代码如下:


package com.example.listactivitytest;

import android.os.Bundle;

import android.app.ListActivity;

import android.view.Menu;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

public class MainActivity extends ListActivity{

private String[] cityArray = new String[] {"A","B","C","D","E","F","G","H","I","J","K","L","I"};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//setContentView(R.layout.activity_main);

ArrayAdapter<String> arrayAdapterRef = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cityArray);

this.setListAdapter(arrayAdapterRef);

}

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

// TODO Auto-generated method stub

super.onListItemClick(l, v, position, id);

Toast.makeText(this"你点了第"+(id+1)+"项n内容为"+cityArray[(int) id], Toast.LENGTH_SHORT).show();

}

@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;

}

}