在galleryview中的每个图像下面的Textview

时间:2012-11-22 05:44:21

标签: android android-layout android-intent galleryview

在我的应用程序中,我使用5个galleryview显示图像,正确完成,我可以正确填充图像。现在我想在每个图像下面添加textview。

我想要这样的画廊视图:

image1    Image2    Image3  image4
                                             -------->Galleryview1

text1     text2      text3   text4

------------------------------------------
image1    image2    image3    image4

                                             --------->Galleryview2
text1     text2      text3    text4

我的代码:

适配器代码:

public class GalleryviewAdapter extends BaseAdapter 
{      

    static final String URL="http://aaaa/home.xml";


    public static GalleryviewAdapter instance=new GalleryviewAdapter();
    public static GalleryviewAdapter getInstance()
       {
           return instance;
       }
       Context context;        
       GalleryviewAdapter()
       {
           System.out.println("Inside cons");
           getelement();
//         getelementindia();
       }
//     String[] itemsArray = 
//         {
//       "SUN","MON", "TUS", "WED", "THU", "FRI", "SAT"
//         };

//     MyAdapter(Context c)
//     {
//      context = c;
//     }

       private Activity activity;  
       private  LayoutInflater inflater=null; 
       public void ImageAdapter(Activity a) 
       {   
           activity = a;  
                inflater = (LayoutInflater)activity.getSystemService
                        (Context.LAYOUT_INFLATER_SERVICE);         
        } 

    String imageurl[]= {};//new String[5];
//    String imageurlindia[]={};
    public void getelement()
    {
        System.out.println("Inside getelement");
//     String[] itemsarray={};
//     ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
//     ArrayList<String> menuItems = new ArrayList<String>();
       TaplistingParser parser = new TaplistingParser();
       String xml= parser.getXmlFromUrl(URL);
       Document doc=parser.getDomElement(xml);        
//       System.out.println("sssss="+doc);
       NodeList nl=doc.getElementsByTagName("article");
       imageurl = new String[nl.getLength()];
       System.out.println("len="+nl.getLength());
       for(int i=0; i < nl.getLength(); i++ )
       {
//         System.out.println("Inside for");
//          HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
//          map.put("SectionName", parser.getValue(e, "sectionname"));
//          System.out.println("b4 items="+parser.getValue(e, "sectionname"));
//          itemsarray[i] = parser.getValue(e, "sectionname");
//          System.out.println("items="+itemsarray[i]);
        imageurl[i]=parser.getValue(e, "kickerimage");
//          menuItems.add(parser.getValue(e, "sectionname"));
//      menuItems.add(parser.getValue(e, "sectionname"));
//       System.out.println("menu="+menuItems);

       }    
//      String[] itemsarray = menuItems.toArray(new String[menuItems.size()]); 

//       String[] itemsarray = new String[menuItems.size()];
//       itemsarray=menuItems.toArray(itemsarray);
////       for(int j= 0;j < itemsarray.length;j++ )
////       {
////           Log.d("string is",(itemsarray[j]));
////       }
//      return itemsarray;
    }





      public int getCount() 
      {
       // TODO Auto-generated method stub
       return imageurl.length;
      }


      public Object getItem(int position) 
      {
       // TODO Auto-generated method stub
       return imageurl[position];
      }


      public long getItemId(int position) 
      {

       return position;
      }

      public View getView(int position, View convertView, ViewGroup parent) 
      {

//        System.out.println("sssssssss="+imageurl[position]);
//          View vi=convertView;   
//          ViewHolder holder;   
//          if(convertView==null)
//          {  
//              vi = inflater.inflate(R.layout.main, null); 
//              holder=new ViewHolder();  
//              holder.text=(TextView)vi.findViewById(R.id.Txt01);  
//              holder.image=(ImageView)vi.findViewById(R.id.imageview1);   
//              vi.setTag(holder);   
//          }

       Bitmap bitmap=DownloadImage(imageurl[position]);
       View rowView = LayoutInflater
         .from(parent.getContext())
         .inflate(R.layout.main, null);
//     
       ImageView imgview=(ImageView)rowView.findViewById(R.id.imageview1);     
       imgview.setImageBitmap(bitmap);


//     
//     ImageView imgviewindia=(ImageView)rowView.findViewById(R.id.imageviewindia);
//     imgviewindia.setImageBitmap(bitmap);


//     TextView listTextView = (TextView)rowView.findViewById(R.id.);
//     listTextView.setText(getelement()[position]);           
       return rowView;
      } 

      private Bitmap DownloadImage(String URL)
        {        
//          System.out.println("image inside="+URL);
            Bitmap bitmap = null;
            InputStream in = null;        
            try
            {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in);
                in.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
//          System.out.println("image last");
            return bitmap;                
        }
        private InputStream OpenHttpConnection(String urlString)
                throws IOException
                {
                    InputStream in = null;
                    int response = -1;

                    URL url = new URL(urlString);
                    URLConnection conn = url.openConnection();

                    if (!(conn instanceof HttpURLConnection))                    
                        throw new IOException("Not an HTTP connection");                   
                    try{
                        HttpURLConnection httpConn = (HttpURLConnection) conn;
                        httpConn.setAllowUserInteraction(false);
                        httpConn.setInstanceFollowRedirects(true);
                        httpConn.setRequestMethod("GET");
                        httpConn.connect();

                        response = httpConn.getResponseCode();                
                        if (response == HttpURLConnection.HTTP_OK) 
                        {
                            in = httpConn.getInputStream();                                
                        }                    
                    }
                    catch (Exception ex)
                    {
                        throw new IOException("Error connecting");            
                    }
                    return in;    
        }
      }
//  }

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/imageview1"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="-20dp"
        android:layout_width="120dp"
        android:layout_height="100dp"        
        android:paddingLeft="10dp"
        android:paddingRight="10dp" 
        />
      <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Txt01" />

</LinearLayout>

我的主要活动代码:

public class NewspapperActivity extends Activity 
{
    /** Called when the activity is first created. */
    Context ctx;

    static final String URLHeading = "http://aaaaaa.in/cccccc.xml";
    String[] headingurl=new String[20];
//    ListViewwithimageAdapter adapter;
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galleryview);        
//        ArrayList<String>item = new ArrayList<String>();

        Heading parser = new Heading();       
        String xmldata = parser.getXmlFromUrl(URLHeading);

        Document domelement = parser.getDomElement(xmldata);

        NodeList node = domelement.getElementsByTagName("item");

//        int i ;

        TextView txt1 = (TextView)findViewById(R.id.Text01);
        TextView txt2 = (TextView)findViewById(R.id.Text02);
        TextView txt3 = (TextView)findViewById(R.id.Text03);
        TextView txt4 = (TextView)findViewById(R.id.Text04);
        TextView txt5 = (TextView)findViewById(R.id.Text05);


//        glly.setAdapter(adapter);
//        System.out.println("prakash4");
//        for(i=0 ; i < node.getLength();i++)
//        {
            Element e0 = (Element) node.item(0);
            txt1.setText(parser.getValue(e0, "sectionname"));

            Element e1 = (Element) node.item(1);
            txt2.setText(parser.getValue(e1, "sectionname"));

            Element e2 = (Element) node.item(2);
            txt3.setText(parser.getValue(e2, "sectionname"));

            Element e3 = (Element) node.item(3);
            txt4.setText(parser.getValue(e3, "sectionname"));

            Element e4 = (Element) node.item(4);
            txt5.setText(parser.getValue(e4, "sectionname"));

            Gallery glly= (Gallery)findViewById(R.id.Gallery01);
            Gallery glly2= (Gallery)findViewById(R.id.Gallery02);
            Gallery glly3= (Gallery)findViewById(R.id.Gallery03);
            Gallery glly4= (Gallery)findViewById(R.id.Gallery04);
            Gallery glly5= (Gallery)findViewById(R.id.Gallery05);
//          GridView gv = (GridView)findViewById(R.id.grid);

//          ListViewwithimageAdapter adapter = ListViewwithimageAdapter.getInstance();
            GalleryviewAdapter adapter=GalleryviewAdapter.getInstance();
            glly.setAdapter(adapter);    
            glly.setSelection(1);
            glly2.setAdapter(adapter);
            glly2.setSelection(1);
            glly3.setAdapter(adapter);
            glly3.setSelection(1);
            glly4.setAdapter(adapter);
            glly4.setSelection(1);
            glly5.setAdapter(adapter);
            glly5.setSelection(1);
//          item.add(parser.getValue(e, "sectionname"));
//        }
//        gv.setAdapter(new GridviewImageAdapter(this));

    }

}

我是新手。请帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

使用下面的xml代码而不是xml代码,它将解决您的问题,如果您有任何问题,请告诉我。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageview1"
            android:layout_width="120dp"
            android:layout_height="100dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="-20dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" 
            android:src="@drawable/ic_launcher"/>

        <TextView
            android:id="@+id/Txt01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageview1"
            android:layout_centerHorizontal="true"
            android:text="Dipak" />
    </RelativeLayout>

</RelativeLayout>