1

How can i customize a cell of Grid view with two textview, as i want to show 2 labels in each cell.

In my task, i am looking for the Grid View as a Table and it contains two various Values in each cell. i have Tried the Customized Grid view for this as follow.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/selectBtn"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Select" android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:minWidth="200px" />
<GridView android:id="@+id/PhoneGrid"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:numColumns="auto_fit" android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp" android:columnWidth="90dp"
    android:stretchMode="columnWidth" android:gravity="center"
    android:layout_above="@id/selectBtn" />
</RelativeLayout>

and the Custom layout is,..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/Name" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_centerInParent="true" />
<TextView android:id="@+id/Number" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />
</RelativeLayout>

In this i need to display the name and phone number in a single Gridview..

Help me do get the Solution..

Thanks in Advance..

Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
gowri
  • 681
  • 9
  • 27
  • What do you want to ask ? You mean how to set them? You need to write a custom adapter for this. – yahya Nov 09 '12 at 06:19

3 Answers3

3

You want to create an Adapter class that extends BaseAdapter to pass into the GridView in code. Everything you need to do is already shelled out for you in the methods you need to override. Android has a decent tutorial that should help you get started. If you need help understanding what each part does just look it up on here. There are plenty of people who have went through the same thing you are going through now, so resources are in the plenty :)

Cheers.

Andy
  • 10,553
  • 21
  • 75
  • 125
  • 1
    I'm using database. Can i use this Adapter for the Mutiple or n no.of data?? – gowri Nov 09 '12 at 06:28
  • Yes. `CursorAdapter` has a method called `swapCursor` that you can use in conjunction with a CursorLoader. You'll need to have your Class implement `LoaderManager.LoaderCallbacks`. You'll get a few methods to override that you can use to have it all work together. Do some research on CursorLoaders. A few of my question on here actually has been answered on this very subject so I know you'll get some good stuff. – Andy Nov 09 '12 at 06:33
  • 1
    Could you Explain the Concept.. Andy – gowri Nov 09 '12 at 06:38
  • 1
    Sure. What you'll need is 2 classes: Your main Activity or whatever you are using to run it, a custom `CursorAdapter` which you need to create. But your Activity (or Fragment) which is running the layout with the GridView must implement `LoaderManager.LoaderCallbacks` This brings some convenience methods that allow you set the GridView with an empty adapter, and in the background query the database, and give your GridView the Cursor after its done. Your adapter will handle everything that you tell it once you create it. – Andy Nov 09 '12 at 06:42
  • Do you Have Any Examples?? Andy – MGR Nov 09 '12 at 06:44
  • 2
    @ShaftShankar (interesting name btw), this is the link to one of my questions on the matter: http://stackoverflow.com/questions/11150527/how-does-cursorloader-with-loadermanager-know-to-send-the-cursor-to-a-cursoradap hopefully its helpful to anyone since it really helped me understand how the whole process works. – Andy Nov 09 '12 at 06:50
  • I too have tried with this Concept. But i couldn't get the Output. So i dropped,.. Now this Question Bring me to Here.. Andy.. – MGR Nov 09 '12 at 06:55
  • Well @ShaftShankar check out that link, and look through that. The guy that helped me understand the concept is really good at explaining it. Actually, if you go to his website, he has a few posts on CursorLoaders. Unfortunately by the time he created them they weren't useful to me as he had already helped me, but I am sure they will be great for you. http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html – Andy Nov 09 '12 at 06:58
  • @Andy: Mail me at shaftshankar@gmail.com – MGR Nov 09 '12 at 07:05
  • More info on CursorLoaders: http://developer.android.com/guide/components/loaders.html by the way, you should also accept the answer that helped you. – Andy Nov 09 '12 at 21:55
2

You can follow this link and can replace the place of imageview, you can use the Text, what you need..

Cusom GridView

Thank you

Arivazhagan
  • 101
  • 8
  • Its a good tutorial, but if you are using a database, a `BaseAdapter` would not work. You could do some extra work and parse the Cursor you get back after the query and then send in the information to the `BaseAdapter` custom class, but why complicate it when `CursorAdapter` does it all for you. At the same time, if you use CursorAdapter, you need to make sure you do the query in the background. CursorLoader does this for you automatically. Keep in mind it only does this for the query and not update, delete, insert, etc. – Andy Nov 09 '12 at 06:45
  • Check out the links I posted on my answer @ShaftShankar but if those still don't help, let me know and I will create example code for you guys on github. – Andy Nov 09 '12 at 07:01
1

Simply i have used a String with Multiline Processing as "\n"..

Like you can subsitude the String as follows

String header=first_String_Variable+"\n"+second_String_Variable;

and set grid by using Adapter..

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, header);
gridHeader.setAdapter(adapter);
gowri
  • 681
  • 9
  • 27
MGR
  • 382
  • 2
  • 7
  • 21