Domanda

I've downloaded the Navigation Drawer example from the developers guide, I try to implement a GridView into an Activity of one of the options of the Drawer, but in execution time the GridView doesn't appears, the IDE doesn't show me any errors and in an example apart I can do it de GridView successfuly, How do I correct this problem?

My code is here:

In the MainActivity I use the same like the example with

Fragment fragment;
FragmentManager fragmentManager;
fragment = new inicioFragment(R.layout.inicio);
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

InicioActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
public class InicioActivity extends Activity {
    TextView tv1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inicio);    
        tv1=(TextView)findViewById(R.id.tv1);
        GridView gv = (GridView)findViewById(R.id.gridView1); 
        gv.setAdapter(new ImageAdapter(this.getApplicationContext()));
        gv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id){              
                String g=Integer.toString(position);
                tv1.setText(g);
            }
        });
    }
}

ImageAdapter.java

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

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;
    }
    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(13, 13, 13, 13);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    private Integer[] mThumbIds = {
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7,
            R.drawable.image_0, R.drawable.image_1,
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7,
            R.drawable.image_0, R.drawable.image_1,
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7
    };
}

inicio.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Aqui van las notificaciones push de facebook" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="156dp"
            android:layout_height="192dp"
            android:numColumns="2" >
        </GridView>

        <TextView
            android:id="@+id/tv1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:gravity="center_horizontal|center_vertical"
            android:text="Información" />
    </LinearLayout>
</RelativeLayout>

Thanks for your collaboration!!

È stato utile?

Soluzione 2

The following worked for me. In Android Studio:

1) Create a new Navigation Drawer Activity from the list of activity templates.

2) Note that several layout files will be created for you, including a content_main.xml. Replace the XML in the content_main.xml file with your <GridView> XML.

3) Create the relevant XML files or Java classes for the GridView, eg. the ImageAdapter class and an XML file for the GridView item layout.

Altri suggerimenti

Try to change InicioActivity into a fragment.
And check this question. I think you are facing the same problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top