[英]Is there a way of displaying all sqlite tables and contents by using listview?
我制作了一個用於在運行時從數據庫獲取所有信息的小工具。 這是我的代碼:
包com.example.helix.whiffs;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FragmentDatabase extends Fragment{
private final String LOG_TAG = "FRAGMENTDATABASE";
DatabaseListener listener;
int rowHeight = 40;
String text;
Button select;
TextView counter;
LinearLayout titleBar;
LinearLayout dataList;
PopupMenu popup;
String[] fields;
int[] colWidths;
List<String> data;
HashMap<Integer, String[]> dataItems;
HashMap<Integer, String> menuItems;
DBAdapter db;
public interface DatabaseListener{
void onload(String table);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_database, container, false);
select = (Button)v.findViewById(R.id.select);
counter = (TextView)v.findViewById(R.id.count);
titleBar = (LinearLayout)v.findViewById(R.id.titles);
dataList = (LinearLayout) v.findViewById(R.id.list);
data = new ArrayList<>();
menuItems = new HashMap<>();
dataItems = new HashMap<>();
db = new DBAdapter(getActivity().getApplicationContext());
data = db.allTables();
popup = new PopupMenu(getActivity().getApplicationContext(), select);
for(int i = 0; i < data.size(); i++) {
popup.getMenu().add(Menu.NONE, i, i, data.get(i));
menuItems.put(i, data.get(i));
}
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
dataList.removeAllViews();
titleBar.removeAllViews();
dataItems.clear();
int itemId = menuItem.getItemId();
String table = menuItems.get(itemId);
fields = db.allFields(table);
TextView idField = new TextView(getActivity().getApplicationContext());
idField.setText(fields[0]);
titleBar.addView(idField);
colWidths = new int[fields.length];
colWidths[0] = 80;
for(int i = 1; i < fields.length; i++){
colWidths[i] = 200;
TextView f = new TextView(getActivity().getApplicationContext());
String text = fields[i];
f.setText(text);
titleBar.addView(f);
}
dataItems = db.allValues(table);
counter.setText(String.valueOf(dataItems.size()));
for(int i = 0; i < dataItems.size(); i++){
LinearLayout row = new LinearLayout(getActivity().getApplicationContext());
row.setOrientation(LinearLayout.HORIZONTAL);
String[] rowData = dataItems.get(i);
TextView idf = new TextView(getActivity().getApplicationContext());
if(rowData.length != 0) idf.setText(rowData[0]);
row.addView(idf);
for(int j = 1; j < rowData.length; j++){
TextView idf2 = new TextView(getActivity().getApplicationContext());
text = rowData[j];
row.addView(idf2);
if(text != null && text.length() > 9){
int length = 18 * text.length();
if(colWidths[j] < length) colWidths[j] = length;
}
idf2.setText(text);
}
dataList.addView(row);
}
for(int j = 0; j < colWidths.length; j++){
TextView tv = (TextView) titleBar.getChildAt(j);
ViewGroup.LayoutParams vlp = tv.getLayoutParams();
vlp.width = colWidths[j];
vlp.height = rowHeight;
tv.setLayoutParams(vlp);
}
for(int i = 0; i < dataItems.size(); i++){
LinearLayout rowLayout = (LinearLayout) dataList.getChildAt(i);
for(int j = 0; j < colWidths.length; j++){
TextView tv = (TextView) rowLayout.getChildAt(j);
ViewGroup.LayoutParams vlp = tv.getLayoutParams();
vlp.width = colWidths[j];
vlp.height = rowHeight;
tv.setLayoutParams(vlp);
}
}
return true;
}
});
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popup.show();
}
});
return v;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentDatabase.DatabaseListener) {
listener = (FragmentDatabase.DatabaseListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentListener");
}
}
@Override
public void onDetach() {
listener = null;
super.onDetach();
}
}
這是我的數據庫適配器的重要部分:
myDbHelper myhelper;
public List<String> allTables(){
List<String> result = new ArrayList<>();
SQLiteDatabase db = myhelper.getWritableDatabase();
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type = 'table'",null);
if(c.moveToFirst()){
while (!c.isAfterLast()){
result.add(c.getString(0));
c.moveToNext();
}
}
return result;
}
public String[] allFields(String table){
List<String> result = new ArrayList<>();
SQLiteDatabase db = myhelper.getWritableDatabase();
Cursor c = db.query(table, null, null, null, null, null, null);
return c.getColumnNames();
}
public HashMap<Integer, String[]> allValues(String table) {
HashMap<Integer, String[]> result = new HashMap<>();
String[] row;
SQLiteDatabase db = myhelper.getWritableDatabase();
Cursor c = db.rawQuery("SELECT * FROM "+table, null);
int columns = c.getColumnCount();
if(c.moveToFirst()){
int rowCount = 0;
while(!c.isAfterLast()){
row = new String[columns];
for(int i = 0; i < columns; i++){
row[i] = c.getString(i);
Log.i(LOG_TAG, "Row add "+String.valueOf(c.getString(i)));
}
result.put(rowCount, row);
rowCount ++;
c.moveToNext();
}
}
return result;
}
static class myDbHelper extends SQLiteOpenHelper
{
private Context context;
public myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.context=context;
onCreate(this.getWritableDatabase());
}
}
}
最后是片段的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/top_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#888888"
android:orientation="horizontal"
>
<Button
android:id="@+id/select"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="table"/>
<TextView
android:id="@+id/count"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="total"
android:textSize="24sp"
android:gravity="center_horizontal"
android:layout_marginTop="5dp"/>
</LinearLayout>
<HorizontalScrollView
android:layout_below="@+id/top_bar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/titles"
android:background="#777777"
android:layout_width="match_parent"
android:layout_height="36dp"
android:orientation="horizontal">
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
</ScrollView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
所有這些都是手動完成的(使用嵌套的LinearLayouts)。 我的華為p8的性能非常好。 如果沒有list-or recyclerview選項,則此代碼可能對某人有用。
答案是肯定的:-
我有一個稱為SQLiteInfoAssistant的工具,該工具被設計為模塊,並且在存在多個數據庫時將顯示信息和基礎數據。
該信息顯示在3 ListView中。 頂部並排顯示2個,它們最初在左上方顯示數據庫,而在右上方顯示所選數據庫的表。
在此初始視圖中,各種數據庫信息都顯示在底部視圖中。 例如,路徑以及通過編譯指示獲得的各種其他信息。
單擊表后,數據庫的表將顯示在左上方視圖中,而列則顯示在右上方視圖中。 下部視圖顯示表信息名稱,用於創建表的SQL,列數和主鍵中的列數。
在以上任一視圖中,長按一個表將顯示實際數據(斑點被截斷並顯示為十六進制表示)。
以下是一些屏幕截圖:
調用可以像包含模塊一樣簡單,創建一個傳遞上下文的SQLiteInformationAssistan
實例,然后使用show
方法。 但是,可以進行許多自定義,如以下調用代碼中的注釋所示:
private void tryitout() {
// Get an SQLiteInformationAssistant instance
// Note! the show method invokes the Activity
SQLiteInformationAssistant SIA = new SQLiteInformationAssistant(this);
// Instance customisation i.e set Display attributes
//SIA.setBaseBackgroundColour(0xFFFFFFFF);
//SIA.setHeadingTextColour(0xFFFFFF00);
//SIA.setDatabaseListHeadingTextColour(0xFFFFFF00);
//SIA.setTableListHeadingTextColour(0xFF0077FF);
//SIA.setColumnListHeadingTextColour(0xFF77FF00);
//SIA.setDatabaseListTextColour(0xFFFFFFFF);
//SIA.setDatabaseInfoTextColour(0xFFFF0000);
//SIA.setDatabaseListBackgroundColour(0xFFFFFF00);
//SIA.setDatabaseInfoBackgroundColour(0xFFFFFF00);
//SIA.setDatabaseInfoHeadingTextColour(0xFFFF0000);
//SIA.setTableListTextColour(0xFFFF00FF);
//SIA.setTableInfoTextcolour(0xFF0000FF);
//SIA.setTableListBackgroundColour(0xFFFFFF00);
//SIA.setTableInfoBackgroundColour(0xFFFFFF00);
//SIA.setTableInfoHeadingTextColour(0xFFFF00FF);
//SIA.setColumnListTextColour(0xFFFFFF00);
//SIA.setColumnInfoTextColour(0xFF000000);
//SIA.setColumnListBackgroundColour(0xFFFFFF00);
//SIA.setColumnInfoBackgroundColour(0xFFFFFF00);
//SIA.setColumnInfoHeadingTextColour(0xFF00FF00);
//SIA.setStringCellBackgroundColour(0xFFEEEEEE);
//SIA.setStringCellTextColour(0xFF0000FF);
//SIA.setIntegerCellBackgroundColour(0xFFFF0000);
//SIA.setIntegerCelltextColour(0XFFFFFFFF);
//SIA.setDoubleCellBackgroundColour(0xFF00FFFF);
//SIA.setDoubleCelltextColour(0XFF5555FF);
//SIA.setBlobCellBackgroundColour(0xFF44FF55);
//SIA.setBlobCelltextColour(0xFFFFFF00);
//SIA.setUnkownCelltextColour(0xFFFFFF00);
//SIA.setUnknownBackgroundCellColour(0xFFFF0000);
// Note
//SIA.setBytesToShowInBlob(128);
SIA.show();
}
我實際上還沒有發布它或使其可用,但是如果有人感興趣,我可以提供代碼,這主要是因為我自己很少使用它,因為將數據庫復制到一個免費的SQLite工具(Navicat)中非常容易。 ,用於Sqlite的數據庫瀏覽器,SQLite管理器等)。 完整的代碼可能太大,無法在此處發布。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.