我無法弄清楚我的Android GridView出現了什麼問題。我將數據從SQLite數據庫中提取出來,並將光標返回給適配器,但在渲染時絕對不能正確排列。這是我的。我的Android GridView有什麼問題?
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<LinearLayout
android:id="@+id/top_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:src="@drawable/icon2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:padding="8dp"
android:text="@string/app_name"
android:textColor="@color/app_name"
android:textSize="38sp"
android:textStyle="bold" />
</LinearLayout>
<include layout="@layout/top_right_info_board" />
</LinearLayout>
<GridView
android:id="@+id/row_of_stats"
android:layout_width="match_parent"
android:layout_height="match_parent">
</GridView>
</RelativeLayout>
data_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/id_textview"
android:background="@color/RED"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/data01_textview"
android:background="@color/DARKVIOLET"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/data02_textview"
android:background="@color/YELLOWGREEN"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/data03_textview"
android:background="@color/LIGHTPINK"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/data04_textview"
android:background="@color/MIDNIGHTBLUE"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/data05_textview"
android:background="@color/PALEVIOLETRED"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/data06_textview"
android:background="@color/AQUA"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/data07_textview"
android:background="@color/YELLOWGREEN"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/data08_textview"
android:background="@color/BLUEVIOLET"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity.java
dataCursor = myDBHelper.getDataCursor();
String[] fromColumns = {"_id","data01","data02","data03","data04","data05","data06","data07","data08"};
int[] toViews = {R.id.id_textview, R.id.data01_textview, R.id.data02_textview, R.id.data03_textview, R.id.data04_textview, R.id.data05_textview, R.id.data06_textview, R.id.data07_textview, R.id.data08_textview};
SimpleCursorAdapter mySimpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.data_row, dataCursor, fromColumns, toViews, 0);
GridView myGridView = (GridView) findViewById(R.id.row_of_stats);
myGridView.setAdapter(mySimpleCursorAdapter);
這是什麼樣子:
我試圖用android:numColumns="9"
,但是這之後發生了什麼:
我想要的樣子是這樣的:
我完成了使用滾動型和TableLayout這最後的,有吸引力的,佈局,但它真的缺憾和非常慢,所以我寧願與GridView控件適配器沾。
好吧,這只是絕對美麗...謝謝!我做出了你所建議的改變,現在一切都很好地展現出來。奇怪的是,我一直在尋找一種方法來在Android中創建一個簡單的數據表,並且沒有太多的成功。許多人提出了GridView,但我只是覺得這是不正確的,但我認爲可以這麼說(可以這麼說)。我仍然不確定爲什麼在Web開發中如此簡單的事情在Android中非常困難。再次,謝謝你! – Brian