package sleek.listview;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MyListViewActivity extends ListActivity {
/** Called when the activity is first created. */
//定义SQLiteDatabase对象,用于处理数据库的操作
private SQLiteDatabase db = null;
//定义Cursor游标,用于管理数据,比如获得数据库的每一行数据
private Cursor cursor = null;
//定义一个列表储存 mData
private List<Map<String, Object>> mData;
//获取自定义的function
function function = new function();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化数据库,创建文件夹和数据库文件
function.initDatabase();
mData = getData();
MyAdapter adapter = new MyAdapter(this);
setListAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//使用SQLiteDatabase的openOrCreateDatabase获取SDcard的数据库
//config.f 是在config.java里定义的文件路径变量
db = SQLiteDatabase.openOrCreateDatabase(config.f, null);
Map<String, Object> map = new HashMap<String, Object>();
try {
//查询test_listview数据
cursor = db.query("test_listview", new String[]{}, null, null, null, null, null);
//通过强大的cursor把数据库的资料一行一行地读取出来
while(cursor.moveToNext()){
String text = cursor.getString(cursor.getColumnIndex("text"));
map = new HashMap<String, Object>();
map.put("text", text);
map.put("img", R.drawable.info);
list.add(map);
}
} catch (Exception e) {
// TODO: handle exception
Log.i("LogDemo", e.toString());
}
return list;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
function.showDialog((String)mData.get(position).get("text"), MyListViewActivity.this);
//Toast.makeText(MyListViewActivity.this, (String)mData.get(position).get("text"), Toast.LENGTH_LONG).show();
}
public final class ViewHolder{
public ImageView img;
public TextView text;
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
ViewHolder holder = null;
public MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder=new ViewHolder();
convertView = mInflater.inflate(R.layout.list, null);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.text = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
//把字符串设置在TextView上显示
holder.text.setText((String)mData.get(position).get("text"));
return convertView;
}
}
public void createDataBase() throws IOException{
File dbdir = config.path;
if(!dbdir.exists()){
dbdir.mkdirs();
}
if(!config.f.exists()){
config.f.createNewFile();
SQLiteDatabase.openOrCreateDatabase(config.f, null);
copyDataBase();
}
}
private void copyDataBase() throws IOException {
InputStream myInput;
//复制到的目标路径
String outFileName = config.CR_URL_DBTABLE;
OutputStream myOutput = new FileOutputStream(outFileName);
//获取Assets文件夹中的databae.db
myInput = this.getAssets().open(config.CR_DBNAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myInput.close();
myOutput.close();
}
}