gridview help

Put your problem here if it does not fit any of the other categories.

gridview help

Postby flo » Tue Dec 15, 2009 5:03 pm

Hy!
i'm reading the book begining android and i don't understand some things.
first in the code I'll post below you override the class ArrayaAdapter which you'll be using for the GridView.
when you override getView you return a textview,but how is that textview used in the gridview.every cell is a new textview ?



public class GridDemo extends Activity
implements AdapterView.OnItemSelectedListener {
TextView selection;
String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
"ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue", "purus"};

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);

GridView g=(GridView) findViewById(R.id.grid);
g.setAdapter(new FunnyLookingAdapter(this,
android.R.layout.simple_list_item_1,
items));
g.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
selection.setText(items[position]);
}

public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}

private class FunnyLookingAdapter extends ArrayAdapter {
Context ctxt;

FunnyLookingAdapter(Context ctxt, int resource,
String[] items) {
super(ctxt, resource, items);

this.ctxt=ctxt;
}

public View getView(int position, View convertView,
ViewGroup parent) {
TextView label=(TextView)convertView;

if (convertView==null) {
convertView=new TextView(ctxt);
label=(TextView)convertView;
}

label.setText(items[position]);

return(convertView);
}
}
}
flo
Freshman
Freshman
 
Posts: 2
Joined: Tue Dec 15, 2009 4:28 pm

Top

Solution of gridview

Postby pranav09 » Wed Dec 16, 2009 5:04 am

Hi flo,

As per the your problem, you should go through the different example, those are available in the Android SDK.

Just go through it. You will find the solution by yourself.
Good Luck.
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 27 guests