如何使用网格视图滚动和按钮输出

时间:2013-06-06 04:20:47

标签: android xml gridview scroll

我正在尝试在我的项目上实现一个包含xml文件的文件:textview,gridview和一个后退按钮,但这里是代码:

<?xml version="1.0" encoding="UTF-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mask="http://schemas.android.com/apk/res/br.com.example.sgm"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/Black"
    android:screenOrientation="portrait" >



    <TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="10dp"
    android:text="Código                 Data"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/LSGray" />






    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="410dp"
    android:onClick="bvoltar"
    android:text="     Voltar     "
    android:textColor="@color/Snow" />

    <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="350dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="345dp"
    android:numColumns="1" >

    </GridView>

    </LinearLayout>
    ScrollView>

</RelativeLayout>

这应该是一个小文本在顶部,一个网格将显示来自一个数据库表的字段,一个按钮退出,在网格内滚动....但是当我运行项目并转到此屏幕时,滚动不起作用,网格加载并显示entrys,但如果尝试移动滚动这关闭我的apk,任何人都知道如何解决它?

这里是适配器:

public class DataAdapter extends BaseAdapter

{

  Context mContext;


//      
//      private String [] codigo = new String[100];
//      private String [] data = new String[100];

  private LayoutInflater mInflater;

  public int cont;

  public DataAdapter(Context c)

  {

         mContext=c;

         mInflater = LayoutInflater.from(c);

  }

  public int getCount()

  {
      PersistenceHelper per = PersistenceHelper.getInstance(mContext);
        List<Ocorrencia> lOco;
            lOco = per.listarOcorrencia();
        return lOco.size();

  }

  public Object getItem(int position)

  {

         return position;

  }

  public long getItemId(int position)

  {

         return position;

  }

  public View getView(int position, View convertView, ViewGroup parent)

  {

         ViewHolder holder=null;

         if(convertView==null)

         {

                convertView = mInflater.inflate(R.layout.customgrid,
                                                               parent,false);

                holder = new ViewHolder();

                holder.txtCodigo=(TextView)convertView.findViewById(R.id.txtCodigo);

                holder.txtCodigo.setPadding(10, 10, 10 , 10);

                holder.txtData=(TextView)convertView.findViewById(R.id.txtData);

                holder.txtData.setPadding(10, 10, 10, 10);

                if(position==0)

                {                             

                      convertView.setTag(holder);

                }

         }

         else

         {

                holder = (ViewHolder) convertView.getTag();

         }
         PersistenceHelper per = PersistenceHelper.getInstance(mContext);

         List<Ocorrencia> lOco = per.listarOcorrencia();
         setCont(lOco.size());

         String [] codigo = new String[getCont()];
         String [] data = new String[getCont()];

         for(int x=0; x<getCont(); x++){
             codigo[x] = lOco.get(x).getCodigo();
             data[x] = lOco.get(x).getData();  
             }  

         holder.txtCodigo.setText(codigo[position]);

         holder.txtData.setText(data[position]);

         getCount();

         return convertView;


  }

  static class ViewHolder

  {        

         TextView txtCodigo;        

         TextView txtData;               

  }

public int getCont() {
    return cont;
}

public void setCont(int cont) {
    this.cont = cont;
}



}

如果有必要......

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

<TableLayout android:id="@+id/TableLayout01"

          android:layout_height="wrap_content"

          android:layout_width="fill_parent">


   <TableRow
       android:id="@+id/TableRow01"
       android:layout_width="wrap_content"
       android:layout_height="fill_parent" >




    <TextView
        android:id="@+id/txtCodigo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Codigo"
        android:textColor="@color/LSGray" />


    <TextView
        android:id="@+id/txtData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="                  Data"
        android:textColor="@color/Snow" />

   </TableRow>

</TableLayout>

</LinearLayout>

在logcat上没有错误导致这个代码我改变了一些东西,但是问题是因为没有接近但是,这只是显示了来自db的一小部分,并没有显示所有的东西,并且我不能改变gridview来填充父级的宽度和高度,我怎么能把网格增加每个新的大小?

UPDATE 1
当我通过MM-BB答案删除scrollview时 logcat错误:

06-06 05:14:17.287: E/AndroidRuntime(828): FATAL EXCEPTION: main
06-06 05:14:17.287: E/AndroidRuntime(828): java.lang.NullPointerException
06-06 05:14:17.287: E/AndroidRuntime(828):  at com.example.sgm.DataAdapter.getView(DataAdapter.java:125)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.widget.AbsListView.obtainView(AbsListView.java:2255)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.widget.GridView.makeAndAddView(GridView.java:1331)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.widget.GridView.makeRow(GridView.java:331)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.widget.GridView.fillDown(GridView.java:283)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.widget.GridView.fillGap(GridView.java:243)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5040)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3197)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3471)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.View.dispatchTouchEvent(View.java:7127)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
06-06 05:14:17.287: E/AndroidRuntime(828):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)

1 个答案:

答案 0 :(得分:0)

1-Gridview不需要scrollView这是当你在scrollview中使用gridview时eclipse显示的警告

The vertically scrolling ScrollView should not contain another vertically scrolling widget (GridView)

2 - 将ScrollView>更改为</ScrollView>