2015-12-29 57 views
0

我無法弄清楚我的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); 

這是什麼樣子:

enter image description here

我試圖用android:numColumns="9",但是這之後發生了什麼:

enter image description here

我想要的樣子是這樣的:

enter image description here

我完成了使用滾動型和TableLayout這最後的,有吸引力的,佈局,但它真的缺憾和非常慢,所以我寧願與GridView控件適配器沾。

回答

1

這是一個數據表;你不想要GridView。當每個細胞(通常)是相同類型時使用。如果你有一堆縮略圖,那麼網格是否有兩列或四列是有意義的。

在你的情況下,每列表示與它旁邊的列不同的東西,所以GridView在這裏沒有意義。

您可以將GridView更改爲ListView。你的數據行佈局已經處於水平方向,你有一個CursorAdapter做正確的事情。只需將GridView更改爲ListView即可讓您朝正確的方向前進。

然後,您將要開始播放行佈局。一個好的開始將是給你的TextView「細胞」的所有相同的重量以及minWidth價值。

最後一個問題是當表格比設備屏幕更寬時,甚至在將行壓縮到最小可讀寬度後,該如何處理。一個超級簡單的解決方案是有左/右按鈕。當用戶按下按鈕向右移動時,您將適配器設置爲在佈局中的第一個TextView上放置visibility="gone",這會使表格跳到左側。如果用戶正確再按一次,您將第 TextView s到visibility="gone"。你明白了。

我正在處理一個數據表的項目,其中每個列表項目視圖都是HorizontalScrollView,其中包含水平LinearLayoutTextView單元格,就像您一樣。訣竅是我捕獲了一行的水平滾動事件,然後將它們傳播到所有行,所以行將全部同步地水平滾動,從而產生雙向滾動的效果。當然,由於每個行的回收,(垂直)滾動性能非常好。如果你有興趣看到這些,請告訴我。

+0

好吧,這只是絕對美麗...謝謝!我做出了你所建議的改變,現在一切都很好地展現出來。奇怪的是,我一直在尋找一種方法來在Android中創建一個簡單的數據表,並且沒有太多的成功。許多人提出了GridView,但我只是覺得這是不正確的,但我認爲可以這麼說(可以這麼說)。我仍然不確定爲什麼在Web開發中如此簡單的事情在Android中非常困難。再次,謝謝你! – Brian