Android GridView empty
I hope to have a little 'more than lucky and find someone who can help me ...
Let me explain my idea of what I do have a list of strings on the left and right of the screen a list of icons that allow you to perform various operations on strings on the left
This is about the idea that I need. I thought of various solutions and I think that is a grid that is more close to what I do, all h a problem ... Refine the list of strings, call the GridView ... and I do not print anything on screen, but the screen remains completely empty ...
enter code public class TabelleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testo);
GridView tabella = new GridView (this);
tabella.setNumColumns(2);
String[] cols = getResources().getStringArray(R.array.countries_array);
ArrayAdap开发者_Go百科ter<String> adapter = new ArrayAdapter<String>(this,R.layout.main, cols);
tabella.setAdapter(adapter);
}
Thank you for the patience
First of all, you never add the GridView to the view displayed on screen. To do so, you must call ViewGroup.addView(View) method. For example, you can give an ID to your base layout and do something like :
// get the base Layout
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
// add the GridView to it
mainLayout.addView(tabella);
Nevertheless, after testing, I reached an IllegalStateException such as :
02-22 09:15:13.140: E/AndroidRuntime(1634): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
Explanation is given by the official documentation of ArrayAdapter:
A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.
So, I need a new resource file to define the item (item.xml). It's like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Finally, I just have to use it in the ArrayAdapter for the GridView:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
GridView tabella = new GridView(this);
tabella.setNumColumns(2);
String cols[] = getResources().getStringArray(R.array.countries_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.item_title, cols);
tabella.setAdapter(adapter);
mainLayout.addView(tabella);
}
I got the expected behaviour, displaying the GridView. Need some adjustment to suit your needs.
精彩评论