【Android】内嵌数据库IDE(可视化操作类)

时间:2014-04-30 14:57:28   收藏:0   阅读:578

Android开发的朋友应该对数据库内容的管理深有体会,想看一下放入数据库的内容都不是很方便,要么用root的设备导出来看或用第三方的手机版的ide。但是都要求root之后。最近一直在想android方便快捷的方法,今天刚好弄到了数据库这块。就写了一个Activity专门用来看数据库的,功能就是看对应数据库的表及表中的数据库。

 

效果图

mamicode.com,码迷

 

 

mamicode.com,码迷

 

刚写还没来得及美化,后面在使用过程中再时行完善。

 

DBIDEActivity.java

mamicode.com,码迷
import java.util.ArrayList;
import java.util.List;
import net.sqlcipher.database.SQLiteDatabase;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 数据库图形界面。
 * @author Harlan Song
 * @email 651193340@qq.com
 * @date 2014年4月29日
 */
public class DBIDEActivity extends Activity implements OnItemClickListener {
    private boolean table = true;
    List<String> tables = new ArrayList<String>();
    SQLiteDatabase db = null;
    ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("tables");
        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
        horizontalScrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        listview = new ListView(this);
        horizontalScrollView.addView(listview,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        setContentView(horizontalScrollView);
        listview.setOnItemClickListener(this);
        db = SQLiteHelper.getInstanceDB(this);
        loadTable();
    }
    
    
    /**
     * 加载所有表。
     */
    private void loadTable(){
        String sql = "SELECT name FROM sqlite_master WHERE type=‘table‘;";
        Cursor cursor = db.rawQuery(sql, null);
        while(cursor.moveToNext()){
            tables.add(cursor.getString(0));
        }
        cursor.close();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, tables); 
        listview.setAdapter(adapter);
    }
    

    
    private class TableAdapter extends BaseAdapter{
        private ArrayList<String[]> datas;
        public TableAdapter(ArrayList<String[]> datas){
            this.datas = datas;
        }
        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout layout_line = new LinearLayout(DBIDEActivity.this);
            layout_line.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            String[] data = datas.get(position);
            for (int i = 0; i < data.length; i++) {
                TextView tvName = new TextView(DBIDEActivity.this);
                tvName.setLayoutParams(new LayoutParams(200, LayoutParams.WRAP_CONTENT));
                tvName.setText(data[i]);
                layout_line.addView(tvName);
                if(position == 0 ){
                    tvName.setPadding(0, 5, 0, 5);
                    tvName.setTextSize(15);
                    tvName.setBackgroundColor(Color.GRAY);
                    //tvName.setGravity(Gravity.CENTER_HORIZONTAL);
                }else{
                    tvName.setTextSize(10);
                }
                    
            }
            return layout_line;
        }
    }
    
    @Override
    public void onBackPressed() {
        if(table ==false){
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, tables); 
            listview.setAdapter(adapter);
            table = true;
            setTitle("tables");
        }else{
            super.onBackPressed();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        if(table){
            String tableName = tables.get(position);
            loadTableData(tableName);
        }
    }
    
    /**
     * 加载表数据。
     * @param tableName
     */
    private void loadTableData(String tableName){
        setTitle(tableName);
        String sql = "select * from " + tableName;
        Cursor cursor =db.rawQuery(sql, null);
        int columnCount = cursor.getColumnCount();
        ArrayList<String[]> datas = new ArrayList<String[]>();
        String[]  names = new String[columnCount];
        for (int i = 0; i < columnCount; i++) {
            names[i] = cursor.getColumnName(i);
        }
        datas.add(names);
        while(cursor.moveToNext()){
            String[]  data = new String[columnCount];
            for (int i = 0; i < columnCount; i++) {
                data[i] = cursor.getString(i);
            }
            datas.add(data);
        }
        cursor.close();
        table = false;
        listview.setAdapter(new TableAdapter(datas));
    }
}
View Code

 

 

就一个Activity,对于一个经常操作数据库的项目来说多个类没什么的。里面的SQLiteDatabase换成系统自带的就可以了。

 

 

 

【Android】内嵌数据库IDE(可视化操作类),码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!