My android learning experience is 34, and my android learning experience is 34.

Source: Internet
Author: User

My android learning experience is 34, and my android learning experience is 34.

Use a class object as the basic data type bound to ArrayAdapter (similar to SimpleAdater)

Generally, the basic data type bound to ArrayAdapter is String. Next we will introduce the class object as the basic data type;

First, create a News class, which serves as the basic data type.

package com.example.news;import android.R.integer;import android.widget.ImageView;public class News {       private String title;       private String content;       private int imageId;              News(String title,String content,int imageId){           this.title=title;           this.content=content;           this.imageId=imageId;       }              public String  getTitle() {        return title;    }       public String  getContent() {           return content;       }       public int  getimageId() {              return imageId;          }      public void setTitle(String title){          this.title=title;      }      public void setContent(String content){          this.content=content;      }}

Next, we will first determine the layout of the listView item, which has a Textview and ImageView;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView         android:id="@+id/news_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <ImageView         android:id="@+id/news_image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>

 

Then, you can customize an adapter NewsAdapter, inherit from ArrayAdapter, and implement two methods. resourceId is the layout id of the item in the listView above;

package com.example.news;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.TextView;public class NewsAdapter extends ArrayAdapter<News>{        private int resourceId;        public NewsAdapter(Context context, int resource, List<News> objects) {        super(context, resource, objects);        // TODO Auto-generated constructor stub        resourceId=resource;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub         News news=getItem(position);        View view ;        if(convertView==null){            view=LayoutInflater.from(getContext()).inflate(resourceId, null);        }else {            view=convertView;        }        TextView news_title=(TextView) view.findViewById(R.id.news_title);        ImageView news_image=(ImageView) view.findViewById(R.id.news_image);                news_title.setText(news.getTitle());        news_image.setImageResource(news.getimageId());        return view;    }}

Then add a listview control to the main layout. This is simple and everyone will certainly

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <ListView         android:id="@+id/list_title"        android:layout_width="wrap_content"        android:layout_height="match_parent"></ListView></RelativeLayout>

Then rewrite the main activity

Import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. widget. listView; public class MainActivity extends Activity {private ListView list_title; private List <News> list = new ArrayList <News> (); private NewsAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); list_title = (ListView) findViewById (R. id. list_title); initList (); adapter = new NewsAdapter (this, R. layout. news_item, list); list_title.setAdapter (adapter);} private void initList () {News news1 = new News ("Title 1", "1", R. drawable. ic_launcher); list. add (news1); News news2 = new News ("title 2", "2", R. drawable. ic_launcher); list. add (news2); News news3 = new News ("Title 3", "3", R. drawable. ic_launcher); list. add (news3); News news4 = new News ("Title 4", "4", R. drawable. ic_launcher); list. add (news4 );}}

Interface Effect

 

Leave a message if you don't know.

 

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.