Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: BaseAdapter and getLayoutInflater on separate class file
    text
    copied!<p>Right now to populate my GridViews I'm using a extended BaseAdapter class on each of my Android Activities (which are most of them).</p> <p>In order to make it easier to read and maintain, I`m trying to put all the BaseAdapter code in a separate class file.</p> <p>To populate the GridView, I'm using LayoutInflater, and here is where stuff gets tricky...</p> <p>Since the getLayoutInflater() comes from <code>android.Activity</code>, it just won't do the trick. I tried making my Adapter.java(the class to populate the GridViews) a extended Activity class, and then inside create the BaseAdapter class (the way I do it right now), but I haven't been able to make it work properly.</p> <p>Here's how Adapter.java looks:</p> <pre><code>//Adapter.java package com.cimp.matitec; import greendroid.app.GDActivity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class Adapter extends GDActivity{ public class ImageAdapter extends BaseAdapter { Context MyContext; int count; String[] nombre; public ImageAdapter(Context _MyContext, int n, String[] nombre) { MyContext = _MyContext; count = n; this.nombre = nombre; } public int getCount() { /* Set the number of element we want on the grid */ return count; } @Override public View getView(int position, View convertView, ViewGroup parent) { View MyView = convertView; if ( convertView == null ) { /*we define the view that will display on the grid*/ //Inflate the layout LayoutInflater li = getLayoutInflater(); MyView = li.inflate(R.layout.grid_item, null); // Add The Text!!! TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text); tv.setText(nombre[position]+""); // Add The Image!!! ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image); iv.setImageResource(R.drawable.ic_launcher); } return MyView; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } } } </code></pre> <p>To call it from the outside, I do the following:</p> <pre><code>//MainClass.java Adapter MyGridAdapter = new Adapter(); MyGrid = (GridView)findViewById(R.id.grid); MyGrid.setAdapter(MyGridAdapter.new ImageAdapter(this, 6, nombreTema)); </code></pre> <p>The app runs, but when trying to populate, I got a NullPointerException getLayoutInflater().</p> <p>Someone knows what I'm missing, or how to make it work properly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload