I have a little glitch in my program I've been trying to figure out what it is I'm missing. In my .java file, I have a gridview of images where I setOnItemsClickListener on items in the gridview. I have 2 xml files, one for the .java and the other is set as contentview to popup window on click on any of the gridview items. I want it that whenever user clicks on item in gridview, the custom dialog box pops up, to show the image clicked, plus a brief description on the image.
So far, the dialog pops out quite alright, but no details are passed unto this dialog box.
THis is my activity_first.xml code:
<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>
This is my activity_custom_dialog.xml file
<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>
Finally, this is how my Image.Java file looks like:
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
I'd really appreciate it should I get a guide asap on how to get around this lil glitch my lil programmer eyes cannot see. :-)
Thanks in advance.
See Question&Answers more detail:
os