Cogonto
08/09/12, 18:23:58
Buenas a todos,
Estoy aprendiendo a programar Android, así que cualquier ayuda es buena! La idea es que programé una aplicación con dos fragments, uno desplegara texto y otro es un gridview que desplegara una lista de fotos. El texto del primer frament ira asociado a cada una de las fotos del gridview (eso lo haré cuando haya solucionado el problema que os comentaré a continuación). Pues bien, me quedé bloqueado a la hora de usar el Adapter para meterlo en el Fragment y a pesar de haber visitado muchas páginas acerca de ello no encuentro solucion. A continuación os muestro el código para que, con vuestra ayuda, alguien me eche un cable o me oriente. Muchas gracias de antemano por vuestra ayuda! No he puesto los Fragments 1 y 2 .java por que el error esta en como uso el adapter. Muchas gracias de antemano por vuestra ayuda!
Ahi van los xml:
Main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:..."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="horizontal" >
<fragment
android:name="com.cogonto.fragments.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<fragment
android:name="com.cogonto.fragments.Fragment2"
android:id="@+id/fragment2"
android:layout_weight="2"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
Fragment1 y Fragment2
<LinearLayout
xmlns:android="..."
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
>
<TextView
android:id="@+id/lblFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
---
<LinearLayout
xmlns:android="..."
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFE00"
>
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit" >
</GridView>
</LinearLayout>
Y el adapater y el main:
public class FragmentsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} }
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_ CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.foto1,
R.drawable.foto2,
R.drawable.foto3,
R.drawable.foto4,
R.drawable.foto5,
};
}
Muchas gracias y vaya todo mu bien!
Estoy aprendiendo a programar Android, así que cualquier ayuda es buena! La idea es que programé una aplicación con dos fragments, uno desplegara texto y otro es un gridview que desplegara una lista de fotos. El texto del primer frament ira asociado a cada una de las fotos del gridview (eso lo haré cuando haya solucionado el problema que os comentaré a continuación). Pues bien, me quedé bloqueado a la hora de usar el Adapter para meterlo en el Fragment y a pesar de haber visitado muchas páginas acerca de ello no encuentro solucion. A continuación os muestro el código para que, con vuestra ayuda, alguien me eche un cable o me oriente. Muchas gracias de antemano por vuestra ayuda! No he puesto los Fragments 1 y 2 .java por que el error esta en como uso el adapter. Muchas gracias de antemano por vuestra ayuda!
Ahi van los xml:
Main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:..."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="horizontal" >
<fragment
android:name="com.cogonto.fragments.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<fragment
android:name="com.cogonto.fragments.Fragment2"
android:id="@+id/fragment2"
android:layout_weight="2"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
Fragment1 y Fragment2
<LinearLayout
xmlns:android="..."
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
>
<TextView
android:id="@+id/lblFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
---
<LinearLayout
xmlns:android="..."
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFE00"
>
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit" >
</GridView>
</LinearLayout>
Y el adapater y el main:
public class FragmentsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} }
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_ CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.foto1,
R.drawable.foto2,
R.drawable.foto3,
R.drawable.foto4,
R.drawable.foto5,
};
}
Muchas gracias y vaya todo mu bien!