新建APP类负责全局的上下文类
public class App extends Application {
public static App appcontext;
@Override
public void onCreate() {
super.onCreate();
appcontext=this;
}
}
主界面自己搭建,R.layou是下面的界面名
//将数组传给适配器, ?为LsitView的名字
?.setAdapter(new ListAdapter(array));
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bf.com.myapplication.Activity.TelInfoActivity">
<ListView
android:id="@+id/list_tel_select"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
单个Item的布局自己定义
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_telinfo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView
android:id="@+id/tv_telinfo_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20dp"/>
</RelativeLayout>
新建一个适配器
public class ListAdapter extends BaseAdapter{
public List<TelInfo> data;
//代表传过来的数组
public ListAdapter(List<TelInfo> data){
this.data=data;
}
@Override
//数组的长度
public int getCount() {
return data==null?0:data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
//对划出去的布局进行复用,进行的优化
public View getView(int position, View convertView, ViewGroup parent) {
viewholder holder=null;
if (convertView==null){
//R.layout.tel_info是单个item对应地址
convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.tel_info,null);
holder=new viewholder(convertView);
convertView.setTag(holder);
}else {
holder= (viewholder) convertView.getTag();
}
TelInfo telInfo=data.get(position);
holder.name.setText(telInfo.getName());
holder.number.setText(telInfo.getNumber());
return holder.convertView;
}
public class viewholder{
public View convertView;
public TextView name;
public TextView number;
//对Id进行复用,进行的优化
public viewholder(View convertView) {
name = (TextView) convertView.findViewById(R.id.tv_telinfo_name);
number = (TextView) convertView.findViewById(R.id.tv_telinfo_number);
this.convertView = convertView;
}
}
}
有个实例方便数组的各种参数的调用
public class TelInfo {
private String name;
private String number;
//要将阐述传送过来
public TelInfo(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
public void setName(String name) {this.name = name;}
public void setNumber(String number) {this.number = number;}
}
一般人们会从数据库读取文件,所以写一个类方便调用
public class DB {
private static final String DB_PATH = "data/data/com.bf.com.myapplication/databases";
private static final String DB_NAME = "commonnum.db";
//代表只调用一次,节省时间
static { copyDB(); }
//可以将输入流传进来,也可以新建一个Activity来承接
public static void copyDB() {
InputStream input = null;
FileOutputStream output = null;
try {
//注意要用这个App.appcontext是要新建一个APP类,在后面
input = App.appcontext.getAssets().open("db/commonnum.db");
File file = new File(DB_PATH);
if (!file.exists()) file.mkdirs();
output = new FileOutputStream(DB_PATH + File.separator + DB_NAME);
int len = 0;
byte[] buffer = new byte[1024];
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null) output.close();
if (input != null) input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//将本地数据库的东西传送给实例,并返回一个主界面一个数组,让主界面传个适配器
public static List<TelInfo> readTelInfo(int idx) {
List<TelInfo> array = new ArrayList<>();
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + "/" + DB_NAME, null);
Cursor cursor = db.rawQuery("select * from table"+idx+";", null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String number = cursor.getString(cursor.getColumnIndex("number"));
TelInfo select = new TelInfo(name, number);
array.add(select);
}
cursor.close();
return array;
}
}