Android (Android) ListView display picture text _android

Source: Internet
Author: User

I. Code IMPLEMENTATION

1. "Activity_11\src\yan\activity_11\mainactivity.java"

Package yan.activity_11; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import Android.content.Context; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.BaseAdapter; 
Import Android.widget.ImageView; 
Import Android.widget.ListView; 
 
Import Android.widget.TextView; 
  public class Mainactivity extends activity {ListView ListView; 
  String [] titles={"Heading 1", "Heading 2", "Heading 3", "Heading 4"}; 
  String [] texts={"Textual content a", "text content B", "Text content C", "Text content D"}; 
   
  int [] Resids={r.drawable.icon,r.drawable.icon,r.drawable.icon,r.drawable.icon}; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.main); 
    This.settitle ("Baseadapter for ListView"); 
    listview= (ListView) This.findviewbyid (R.id.mylistview); 
  Listview.setadapter (New Listviewadapter (Titles,texts,resids)); } public class Listviewadapter extends baseadapter{ViEW [] itemviews; Public Listviewadapter (String [] itemtitles, String [] itemtexts, int [] itemimageres) {itemviews = new Vi 
       
      Ew[itemtitles.length]; 
            for (int i=0; i<itemviews.length; ++i) {Itemviews[i] = Makeitemview (itemtitles[i), Itemtexts[i], 
      Itemimageres[i]); 
    } public int GetCount () {return itemviews.length; 
    Public View getitem (int position) {return itemviews[position]; 
    public long getitemid (int position) {return position; Private View Makeitemview (String strtitle, string strText, int resid) {Layoutinflater Inflater = (Lay 
       
      Outinflater) mainactivity.this. Getsystemservice (Context.layout_inflater_service); 
       
      Object Itemview with view is associated with r.layout.item view Itemview = inflater.inflate (R.layout.listview_item, NULL); Through the Findviewbyid () method instance r.layout.item the components within the TextView titLe = (TextView) Itemview.findviewbyid (r.id.itemtitle); 
      Title.settext (strtitle); 
      TextView Text = (TextView) Itemview.findviewbyid (R.id.itemtext); 
      Text.settext (StrText); 
      ImageView image = (ImageView) Itemview.findviewbyid (r.id.itemimage); 
       
      Image.setimageresource (RESID); 
    return itemview; 
        Public View GetView (int position, View Convertview, ViewGroup parent) {if (Convertview = null) 
      return itemviews[position]; 
    return convertview; 
 } 
  } 
}

2.package yan.activity_11;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.ListView;

Import Android.widget.TextView;
 public class Mainactivity extends activity {ListView ListView;
 String [] titles={"Heading 1", "Heading 2", "Heading 3", "Heading 4"};
 String [] texts={"Textual content a", "text content B", "Text content C", "Text content D"};
 
 int [] Resids={r.drawable.icon,r.drawable.icon,r.drawable.icon,r.drawable.icon};
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main);
 This.settitle ("Baseadapter for ListView");
 listview= (ListView) This.findviewbyid (R.id.mylistview);
 Listview.setadapter (New Listviewadapter (Titles,texts,resids));
 
 public class Listviewadapter extends baseadapter{View [] itemviews; Public Listviewadapter (String [] itemtitles, string [] Itemtexts, int [] itemimageres) {itemviews = new view[itemtitles.length]; 
  for (int i=0; i<itemviews.length; ++i) {Itemviews[i] = Makeitemview (itemtitles[i), Itemtexts[i], itemimageres[i]);
 } public int GetCount () {return itemviews.length;
 Public View getitem (int position) {return itemviews[position];
 public long getitemid (int position) {return position; Private View Makeitemview (String strtitle, string strText, int resid) {Layoutinflater inflater = (layoutinflater) M
  
  Ainactivity.this. Getsystemservice (Context.layout_inflater_service);
  
  Object Itemview with view is associated with r.layout.item view Itemview = inflater.inflate (R.layout.listview_item, NULL);
  TextView title = (TextView) Itemview.findviewbyid (r.id.itemtitle) of each component in the R.layout.item by Findviewbyid () method;
  Title.settext (strtitle);
  TextView Text = (TextView) Itemview.findviewbyid (R.id.itemtext);
  Text.settext (StrText);
 ImageView image = (ImageView) Itemview.findviewbyid (r.id.itemimage); Image.setimageresource (RESID);
 return itemview; Public View GetView (int position, View Convertview, ViewGroup parent) {if (Convertview = null) return itemviews
  [position];
 return convertview; }
 }
}

two. " Activity_11\res\layout\main.xml "

 <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "android:id=" LinearLayout01 "android:layout_width=" fill_parent "android:layout_height=" fill_parent "xmlns:android=" HT Tp://schemas.android.com/apk/res/android "> <listview android:layout_width=" wrap_content "and roid:layout_height= "Wrap_content" android:id= "@+id/mylistview" > </ListView> </linearlayout& 

Gt <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:id= "@+id/linearlayout01" Android:layout_w Idth= "Fill_parent" android:layout_height= "fill_parent" xmlns:android= "Http://schemas.android.com/apk/res/android" 
         "> <listview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:id= "@+id/mylistview" > </ListView> </LinearLayout> 

three. " Activity_11\res\layout\listview_item.xml "

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout android:layout_width= "Fill_parent" xml Ns:android= "Http://schemas.android.com/apk/res/android" android:layout_height= "Wrap_content" Android:paddi Ngbottom= "4dip" android:paddingleft= "12dip" > <imageview android:layout_width= "Wrap_conten   
     T "android:id=" @+id/itemimage "android:layout_height=" fill_parent "> </ImageView> <textview android:text= "TextView01" android:layout_height= "Wrap_content" android:l 
        Ayout_width= "Fill_parent" android:id= "@+id/itemtitle" android:layout_torightof= "@+id/itemimage"   
        Android:textsize= "20dip" > </TextView> <textview android:text= "TextView02" android:layout_height= "Wrap_content" android:layout_width= "fill_parent" android:id= "@+id/itemTe" XT "Android:layout_torightof= "@+id/itemimage" android:layout_below= "@+id/itemtitle" > </TextView> </relati  Velayout>
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout "android:layout_width=" xmlns: Android= "Http://schemas.android.com/apk/res/android" android:layout_height= "Wrap_content" Android:paddingbot  
        Tom= "4dip" android:paddingleft= "12dip" > <imageview android:layout_width= "wrap_content" Android:id= "@+id/itemimage" android:layout_height= "fill_parent" > </ImageView> <text View android:text= "TextView01" android:layout_height= "wrap_content" android:layout_width= "fi Ll_parent "android:id=" @+id/itemtitle "android:layout_torightof=" @+id/itemimage "Android:textsiz" E= "20dip" > </TextView> <textview android:text= "TextView02" android:layout_he ight= "Wrap_content" android:layout_width= "fill_parent" android:id= "@+id/itemtext" Android:layo Ut_torightof= "@+id/itemiMage "android:layout_below=" @+id/itemtitle "> </TextView> </RelativeLayout> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.