开发者

Grid view in Android

开发者 https://www.devze.com 2023-03-05 07:23 出处:网络
Hi I want to display a table of data in the mobile. in rows and columns I was trying to achive as below

Hi I want to display a table of data in the mobile. in rows and columns I was trying to achive as below in the getview method, i created a tablerow开发者_StackOverflow and added three textview to this and retruned the tablerow. This displays only the first textview added to tablerow

can anyone help me in this as i am new to Android. I need to get the dispaly something like thsi.

Colval11 colval12 colval13 colval21 colval22 colval23


go through below example: will help you hope so ..no thanks main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"  
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp" 
android:numColumns="auto_fit"  
android:verticalSpacing="10dp" 
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth" 
android:gravity="center"/>

//main activity

package com.test2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class test2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.temp);




          GridView gridview = (GridView) findViewById(R.id.gridview);  
          gridview.setAdapter(new ImageAdapter(this)); 
          gridview.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
                  Toast.makeText(test2.this, "" + position, Toast.LENGTH_SHORT).show();   
                  }    });}

}

///baseadapter

package com.test2;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter { 
private Context mContext;
public ImageAdapter(Context c) { 
    mContext = c;   
    }    public int getCount() {   
        return mThumbIds.length;    
        }    public Object getItem(int position) {
            return null;  
            } 
        public long getItemId(int position) {     
            return 0; 
            }    
        // create a new ImageView for each item referenced by the Adapter  
        public View getView(int position, View convertView, ViewGroup parent) {   
            ImageView imageView;  
            if (convertView == null) { 
                // if it's not recycled, initialize some attributes      
                imageView = new ImageView(mContext);     
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));    
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
                imageView.setPadding(8, 8, 8, 8);     
                } else {   
                    imageView = (ImageView) convertView;     
                    }     
            imageView.setImageResource(mThumbIds[position]);    
            return imageView;    }  
        // references to our images 
        private Integer[] mThumbIds = {     
                R.drawable.sample_2, R.drawable.sample_3,  
                R.drawable.sample_4, R.drawable.sample_5, 
                R.drawable.sample_6, R.drawable.sample_7,  
                R.drawable.sample_0, R.drawable.sample_1, 
                R.drawable.sample_2, R.drawable.sample_3, 
                R.drawable.sample_4, R.drawable.sample_5, 
                R.drawable.sample_6, R.drawable.sample_7, 
                R.drawable.sample_0, R.drawable.sample_1, 
                R.drawable.sample_2, R.drawable.sample_3, 
                R.drawable.sample_4, R.drawable.sample_5, 
                R.drawable.sample_6, R.drawable.sample_7  
 };
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号