Java – Android: a simple GridView that displays text in the grid
I followed this example in the Android tutorial on GridView, but I want to use textview to simply display some text instead of images The result seems more difficult than I thought This seems completely unnecessary. It doesn't have a valid use case, but I'm trying to familiarize myself with the SDK
So my code and http://developer.android.com/guide/tutorials/views/hello-gridview.html The GridView example in is very similar, but instead of using the imageadapter, I created a virtual adapter, as shown below:
public class MyAdapter extends BaseAdapter {
private Context context;
private String[] texts = {"aaa","bbb","ccc","ddd","eee","fff","hhh","iii"};
public MyAdapter(Context context) {
this.context = context;
}
public int getCount() {
return 9;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position,View convertView,ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = new TextView(context);
tv.setLayoutParams(new GridView.LayoutParams(85,85));
}
else {
tv = (TextView) convertView;
}
tv.setText(texts[position]);
return tv;
}
}
This seems to work for me, but running it has nothing on the screen And there is no error message If you click them, there will be some selectable / clickable (invisible) blocks, but the text is obviously not displayed I want to know that my layout does not have Android: does the text cause this problem? Or something?
Any feedback will be greatly appreciated. Thank you for your help!
Solution
I'm not sure what caused your problem I set up "Hello, GridView" according to the step-by-step instructions on the page you link to, use your code and be able to see the text
The only thing I changed was not to create a class for imageadapter that uses myadapter In the active hellogridview In Java oncreate, I use "myadapter" instead of "imageadapter" I haven't changed the layout at all
alt text http://i41.tinypic.com/2rcwkmw.jpg
This is a screenshot obtained when running the code
