凌云志轩 2015-04-16 06:31 采纳率: 89%
浏览 3364
已采纳

android studio gridview如何实现

我实现的老是黑屏退出
mainactivity.java

 private LayoutInflater inflater;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        inflater = LayoutInflater.from(getApplicationContext());                
        GridView dev_listview =(GridView)findViewById(R.id.dev_listview);
        MyAdapter gAdapter = new MyAdapter (this);          
        dev_listview.setAdapter(gAdapter);
        setContentView(R.id.dev_listview);
    }

MyAdapter.java

 public class MyAdapter extends BaseAdapter {
      private LayoutInflater inflater;
    private Context mcontext;
    private Integer[] mThumbnails = {R.drawable.unpress9};
    public MyAdapter( Context c) 
    { 
       mcontext = c;
    } 
     public int getCount() {
            return mThumbnails.length;
     }
     public Object getItem(int position) {
            return null;
        }

      public long getItemId(int position) {
            return 0;
        }


      @SuppressLint("InflateParams")
    public View getView(int position, View grid, ViewGroup parent) 
      {
          inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(grid != null)
     {
        inflater = LayoutInflater.from(mcontext);
        grid =inflater.inflate(R.layout.item_9,parent);
     }

     return grid;

     }

} 

item_9.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal">
    <LinearLayout
          android:orientation="horizontal">
        <LinearLayout
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text1"/>
            <ImageView
                android:id="@+id/img3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img4" />
            <TextView
                android:id="@+id/text2"/>
            <ProgressBar
                android:id="@+id/bar"  />
            <TextView
                android:id="@+id/text3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img" />
            <TextView
                android:id="@+id/text4" />
            <ProgressBar
                android:id="@+id/br" />
            <TextView
                android:id="@+id/text5"/>
        </LinearLayout>
                <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img94"/>
            <TextView
                android:id="@+id/text6"/>
            <ProgressBar
                android:id="@+id/r" />
            <TextView
                android:id="@+id/text7"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img93" />
            <TextView
                android:id="@+id/text8"/>
            <ProgressBar
                android:id="@+id/ar" />
            <TextView
                android:id="@+id/text9"/>
        </LinearLayout>
        <LinearLayout
           android:orientation="horizontal">
            <ImageView
                android:id="@+id/img22" />
            <TextView
                android:id="@+id/text10"/>
            <ProgressBar
                android:id="@+id/ar13" />
            <TextView
                android:id="@+id/text11"/>
        </LinearLayout>
                <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img6"/>
            <TextView
                android:id="@+id/text12" />
            <ProgressBar
                android:id="@+id/r17" />
            <TextView
                android:id="@+id/text13"/>
        </LinearLayout>       
         <LinearLayout
            android:orientation="horizontal" >
            <LinearLayout
                android:orientation="vertical" >
                <LinearLayout
                   android:orientation="horizontal" >
                    <TextView
                        android:id="@+id/text17"/>
                    <ImageView
                        android:id="@+id/mg"/>
                    <ImageView
                        android:id="@+id/mg2"/>
                    <ImageView
                        android:id="@+id/dev_ctrl_stat_img3"/>
                    <ImageView
                        android:id="@+id/k"/>
                    <ImageView
                        android:id="@+id/img90"/>
                    <ImageView
                        android:id="@+id/img91"/>
                    <TextView
                        android:id="@+id/text19"/>
                                                <TextView
                        android:id="@+id/text20" />                  
                </LinearLayout>
                <LinearLayout
                    android:gravity="center_vertical" />
                    <ImageView                      
                        android:src="@drawable/img9" />
                    <TextView
                        android:gravity="center_vertical" />
                    <TextView
                        android:layout_width="wrap_content"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_weight="4.5" >
                <ImageView
                    android:id="@+id/qry"/>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

想实现的功能是:item.xml全部写进gridview的一个item中,这个item里有9行,我下个item调用令一个5行的item5.xml,要保障不同item每行的高度都一样,如何实现呢,求大神指教!

  • 写回答

3条回答

  • danielinbiti 2015-04-16 06:36
    关注
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 
    
            inflater = LayoutInflater.from(getApplicationContext());                
            GridView dev_listview =(GridView)findViewById(R.id.dev_listview);
            MyAdapter gAdapter = new MyAdapter (this);          
            dev_listview.setAdapter(gAdapter);
            setContentView(R.id.dev_listview);//怎么又set布局了,去掉
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站