Niestandardowe okna dialogowe do wyświetlania obrazu, tekstu i zestawu

Mam mały błąd w moim programie, który próbowałem dowiedzieć się, czego mi brakuje. W moim pliku .java mam gridview obrazów, gdzie I setOnItemsClickListener na elementach w gridview. Mam 2 pliki xml, jeden dla .java, a drugi jako contentview do wyskakującego okna po kliknięciu dowolnego z elementów gridview. Chcę, aby za każdym razem, gdy użytkownik kliknie element w widoku siatki, pojawi się niestandardowe okno dialogowe, aby pokazać kliknięty obraz, a także krótki opis obrazu.

Jak dotąd okno dialogowe jest całkiem w porządku, ale żadne szczegóły nie są przekazywane do tego okna dialogowego.

To jest mój kod_podstawowy.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="3" 
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:gravity="center" >

</GridView>

</LinearLayout>

To jest mój plik activity_custom_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background">
    <TableLayout    
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <!-- 2 columns -->
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp" />
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </TableRow>

        <!-- Button span 2 column -->
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip" >

    <Button
                android:id="@+id/button1"
                android:text="Ok" />
        </TableRow>
    </TableLayout>

</LinearLayout>

W końcu tak wygląda mój plik Image.Java:

package com.example.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class ImageActivity extends Activity {
    // Images to display
    Integer[] imageIDs = {
            R.drawable.firefighter,     R.drawable.hydrant,
            R.drawable.fire,            R.drawable.hose,
            R.drawable.truck,           R.drawable.ladder,
            R.drawable.safety_clothing, R.drawable.gloves,
    };
    /**
     * onCreate
     *
     * Called when the activity is first created. 
     * This is where you should do all of your normal static set up: create views, bind data to lists, etc. 
     * This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
     * 
     * Always followed by onStart().
     *
     * @param savedInstanceState Bundle
     */

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView (R.layout.activity_first);
        setTitleFromActivityLabel (R.id.title_text);

        GridView gridview = (GridView) findViewById(R.id.gridView1);
           gridview.setAdapter(new ImageAdapter(this));

           gridview.setOnItemClickListener(new OnItemClickListener()
           {
               public void onItemClick(AdapterView<?> parent, View v, int position, long id)
               {
                    final Dialog dialog = new Dialog(context);
                    dialog.setContentView(R.layout.activity_custom_dialog);

                   switch(v.getId())
                   {
                   case R.drawable.firefighter:
                        dialog.setTitle("FireFighter");                    
                        TextView text = (TextView) dialog.findViewById(R.id.textView1);
                        text.setText("To insert text...");
                        ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);

                        image.setImageResource(R.drawable.firefighter);
                   break;

                   case R.drawable.hydrant:
                        dialog.setTitle("Hydrant");                    
                        TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
                        text1.setText("To insert text...");
                        ImageView image1 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image1.setImageResource(R.drawable.hydrant);
                   break;
                   case R.drawable.fire:
                        dialog.setTitle("Fire");                       
                        TextView text2 = (TextView) dialog.findViewById(R.id.textView1);
                        text2.setText("To insert text...");
                        ImageView image2 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image2.setImageResource(R.drawable.fire);
                   break;
                   case R.drawable.hose:
                        dialog.setTitle("Hose");                       
                        TextView text3 = (TextView) dialog.findViewById(R.id.textView1);
                        text3.setText("To insert text...");
                        ImageView image3 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image3.setImageResource(R.drawable.hose);
                   break;
                   case R.drawable.truck:
                        dialog.setTitle("Truck");                      
                        TextView text4 = (TextView) dialog.findViewById(R.id.textView1);
                        text4.setText("To insert text...");
                        ImageView image4 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image4.setImageResource(R.drawable.truck);
                   break;
                   case R.drawable.ladder:
                        dialog.setTitle("Ladder");                     
                        TextView text5 = (TextView) dialog.findViewById(R.id.textView1);
                        text5.setText("To insert text...");
                        ImageView image5 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image5.setImageResource(R.drawable.ladder);
                   break;
                   case R.drawable.gloves:
                        dialog.setTitle("Gloves");                     
                        TextView text6 = (TextView) dialog.findViewById(R.id.textView1);
                        text6.setText("To insert text...");
                        ImageView image6 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image6.setImageResource(R.drawable.gloves);
                   break;
                   }

                    // set the custom dialog components - text, image and button

                    Button dialogButton = (Button) dialog.findViewById(R.id.button1);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });

                    dialog.show();
           /*                      Toast.makeText(getBaseContext(), "Pic "+(position+1)+
                                  " selected", Toast.LENGTH_SHORT).show();
*/             }
           });
    }

    public class ImageAdapter extends BaseAdapter
    {
        private Context context;

        public ImageAdapter(Context c)
        {
            context = c;
        }


       // Returns the number of images
        public int getCount(){
         return imageIDs.length;
        }

        // Returns the ID of an item
        public Object getItem(int position) {
            return position;
        }

        // Returns the ID of an item
        public long getItemId(int position){
            return position;
        }

        // Returns an ImageView View
        public View getView(int position, View convertView, ViewGroup parent){
            ImageView imageView;
            if(convertView == null){
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(100,100));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(5, 5, 5, 5);
            } 
            else{
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(imageIDs[position]);
            return imageView;
        }

    }

} // end class

Naprawdę bym to docenił powinienem otrzymać poradnik jak poradzić sobie z tą usterką lil, której oczy programisty lil nie widzą. :-)

Z góry dziękuję.

questionAnswers(0)

yourAnswerToTheQuestion