用类对象作为ArrayAdapter绑定的基本数据类型(和SimpleAdater效果类似)

一般ArrayAdapter绑定的基本数据类型是String,接下来介绍一下类对象作为基本数据类型;

首先,新建一个类News,这个类作为基本的数据类型

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;
      }
}

接下来先把listView的item的布局确定下来,有一个Textview和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>

然后自定义一个适配器NewsAdapter,继承ArrayAdapter,并且实现其中的两个方法,resourceId也就是上面的listView的item的布局的id;

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;
    }
}

然后在主布局中添加一个listview控件,这个简单,大家肯定都会

<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>

然后重写主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("标题1", "1", R.drawable.ic_launcher);
        list.add(news1);
        News news2=new News("标题2", "2", R.drawable.ic_launcher);
        list.add(news2);
        News news3=new News("标题3", "3", R.drawable.ic_launcher);
        list.add(news3);
        News news4=new News("标题4", "4", R.drawable.ic_launcher);
        list.add(news4);
    }

}

界面效果

不懂得可以留言

我的android学习经历34的更多相关文章

  1. 我的android学习经历

    我为什么选择android? 我基本上前一年的时间都是在学习java的语法和线程之类的,没有注意java的分类,所以到现在慢慢接触到深处的时候我了解到,java的优势主要在web,而我不是特别喜欢网页 ...

  2. 我的android学习经历36

    最近把android的基础知识都学的差不多了,也写了许多demo,就想自己写一个app,可是写到后面的时候发现很混乱,所以还是得写一些文档,用xml语言写一下基础的类以及一些其他的东西.所以要想写一个 ...

  3. 我的android学习经历27

    前几天忙着学校的互联网+项目比赛,没有时间学习android和发一些东西,主要是这两天太累了,我是项目组长,好多东西去弄,今天已经交稿去进行初赛. 马上收拾收拾心情,继续我的andorid菜鸟之路 加 ...

  4. 我的android学习经历23

    学习fragment时遇到的问题 这几天学习fragment静态加载时遇到这样的问题: java.lang.RuntimeException: Unable to start activity Com ...

  5. 我的android学习经历9

    给android的activity添加背景图片 1.你可以在网上下载android的图片,也可以制作自己的图片,图片的后缀为.png,也就是png格式的图片(注意图片的大小要适合你的手机屏幕或者AVD ...

  6. 我的android学习经历7

    android签名后报错的问题 Duplicate id @+id/imageView, already defined earlier in this layout,android生成报错 这个是项 ...

  7. 我的android学习经历5

    android在strings.xml文件中,写string对象时,如何加入空格 <string name="password">密    码:</string& ...

  8. 我的android学习经历40

    为listview设置背景,并且不随拖动改变 <ListView android:id="@+id/list_view" android:layout_width=" ...

  9. 我的android学习经历39

    关于像第一次进qq一样的那个渐变企鹅图的制作 渐变动画 package com.moonweather.app.activity; import com.moonweather.app.R; impo ...

随机推荐

  1. classmethod一个用处是创建可选类构造器

    Definition and Introduction通常来说, descriptor 是一种绑定着特殊行为属性的对象, 在访问它时行为被descriptor协议定义的方法所重载.这些方法是__get ...

  2. EmguCV 如何从数组中创建出IntPtr

    需要添加引用:System.Runtime.InteropServices 举例如下: float[] priors={1,10}; IntPtr intPtrSet = new IntPtr(); ...

  3. SQL优化 1

    SQL_ID:fvdwtfv18yy0m 先看看sql的预估执行计划 select * from table(dbms_xplan.display_awr('fvdwtfv18yy0m')); sql ...

  4. Apache Spark技术实战之3 -- Spark Cassandra Connector的安装和使用

    欢迎转载,转载请注明出处,徽沪一郎. 概要 前提 假设当前已经安装好如下软件 jdk sbt git scala 安装cassandra 以archlinux为例,使用如下指令来安装cassandra ...

  5. Given a compiled machine-language program, which statements in the source language cause the execution of the most machine-language instructions and what is the execution time of these instr

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A  variety  of  studi ...

  6. deleteRow

    如果是删除某一行的话,直接delete就可以,行数要在删除之前剪掉,否则会崩溃. 但是,如果section要减一的话,是不能删掉section的 Terminating app due to unca ...

  7. 从零开始的Android新项目1 - 架构搭建篇

    记录一下新项目的搭建. 试想一下,如果没有历史负担,没有KPI压力,去新搭建一个项目,你会怎么设计和实现呢? 本系列文章不是教你怎么从0开始学Android,从0开始怎么建一个项目,而定位于零负担的情 ...

  8. vim - Highlight unwanted spaces

    http://vim.wikia.com/wiki/VimTip396 precondition: set hlsearch" Show all tabs:/\t" Show tr ...

  9. NSArray倒序

    NSArray倒序 NSArray *tmparr = [[upLoadImageArr reverseObjectEnumerator] allObjects]; ios 正则表达式替换 . 不可变 ...

  10. Struts 404 The requested resource is not available

    出现这种错误一般是struts.xml配置错误,重点针对<action> 的class属性的全名 因为struts感觉应该是先加载所有的配置文件,如果配置文件有错误的话,所有的资源就都无效 ...