¿Cómo agregar vista a gridview de forma programática, android?

He creado una gridview de 2 coloumns. Necesito tener un botón y una vista de texto que se crean dinámicamente en tiempo de ejecución en cada columna. No puedo escribir su clase baseadapter. ¿Cómo debo inflar mi vista en el gridview.

Esta es mi clase de adaptador

public class Adapter extends BaseAdapter { Context con; Integer[] m; public Adapter(Context c) { con = c; } public Adapter(Integer[] x) { m = x; } @Override public int getCount() { // TODO Auto-generated method stub return m.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return m[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Button btn = new Button(con); TextView textview =new TextView(con); return null; } } 

Usted puede hacer algo como:

 public class Adapter extends BaseAdapter { Context con; Integer[] m; public Adapter(Context c, Integer[] x) { con = c; m = x; } @Override public int getCount() { // TODO Auto-generated method stub return m.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return m[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout layout = new LinearLayout(mContext); layout.setLayoutParams(new GridView.LayoutParams( android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT)); layout.setOrientation(LinearLayout.HORIZONTAL); Button btn = new Button(mContext); btn.setLayoutParams(new LinearLayout.LayoutParams( android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); btn.setText("Btn " + position); TextView textview = new TextView(mContext); textview.setLayoutParams(new LinearLayout.LayoutParams( android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); textview.setText("TV " + position); textview.setTextColor(Color.RED); layout.addView(textview); layout.addView(btn); return layout; } } 

Funcionará:)

  • Center GridView en el diseño relativo
  • Eliminación del relleno adicional en un GridView en android
  • ¿Cómo implementar Galería de imágenes en GridView en android?
  • Android GridView pinch zoom
  • Developer.android.com/training proyecto bitmapfun
  • Cómo agregar CardView dentro de Gridview
  • Android: muestra la implementación de la vista de cabecera de google
  • Cómo hacer que SlidingDrawer sea transparente
  • Mostrar datos en android utilizando gridview desde la base de datos
  • Cómo habilitar arrastrar y soltar en el elemento de GridView onClick ()
  • Registrando UP / CANCEL desde el cuadro de diálogo cuando el evento DOWN fue disparado desde el LongPress de una vista
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.