3

我的程序有一个小故障,我一直在试图弄清楚我错过了什么。在我的 .java 文件中,我有一个图像网格视图,我在网格视图中的项目上设置了 OnItemsClickListener。我有 2 个 xml 文件,一个用于 .java,另一个设置为 contentview 以在单击任何 gridview 项目时弹出窗口。我希望每当用户单击 gridview 中的项目时,都会弹出自定义对话框,以显示单击的图像以及图像的简要说明。

到目前为止,对话框弹出得很好,但没有向该对话框传递任何细节。

这是我的 activity_first.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>

这是我的 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>

最后,这就是我的 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

如果我能尽快获得有关如何解决我的 lil 程序员眼睛看不到的 lil 故障的指南,我将非常感激。:-)

提前致谢。

4

0 回答 0