android继承ListActivity实现列表ListView功能
解决方法:
1、layout目录下新建main.xml文件,内容:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:textColor="#60c"
android:textSize="16sp" >
</TextView>
2、在string.xml的<resources>标签之间添加
<string-array name="countries_array">
<item>American</item>
<item>China</item>
</string-array>
3、MainActivity.java内容:
package com.example.ff;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
/**
* 继承于ListActivity,整个界面由一个ListView组成的情况
* @author eddy
*
*/
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//从资源文件中得到数组对象
String[] countries = getResources().getStringArray(R.array.countries_array);
//得到适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.main,countries);
//得到ListView对象
ListView listView = super.getListView();
//过滤列表
listView.setTextFilterEnabled(true);
//绑定适配器
super.setListAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "你选中了"+((TextView)view).getText(), Toast.LENGTH_LONG)
.show();
}
});
}
}