Android ListView 显示Drawable对象

[url]http://www.cnblogs.com/thu539/archive/2012/02/01/2334455.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的Android ListView电影列表的示例: 1. 创建一个新的Android项目,并在布局文件添加一个ListView组件: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/movie_list" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:dividerHeight="0dp" /> </RelativeLayout> ``` 2. 创建一个Movie类,该类将存储电影的名称,导演和海报图像的资源ID: ``` public class Movie { private String name; private String director; private int poster; public Movie(String name, String director, int poster) { this.name = name; this.director = director; this.poster = poster; } public String getName() { return name; } public String getDirector() { return director; } public int getPoster() { return poster; } } ``` 3. 在MainActivity创建Movie对象列表,并将其添加到ListView: ``` public class MainActivity extends AppCompatActivity { private ListView movieList; private List<Movie> movies; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); movieList = (ListView) findViewById(R.id.movie_list); movies = new ArrayList<>(); movies.add(new Movie("The Shawshank Redemption", "Frank Darabont", R.drawable.shawshank_redemption)); movies.add(new Movie("The Godfather", "Francis Ford Coppola", R.drawable.godfather)); movies.add(new Movie("The Dark Knight", "Christopher Nolan", R.drawable.dark_knight)); movies.add(new Movie("The Lord of the Rings: The Fellowship of the Ring", "Peter Jackson", R.drawable.lord_of_the_rings)); movies.add(new Movie("Forrest Gump", "Robert Zemeckis", R.drawable.forrest_gump)); movies.add(new Movie("Star Wars: Episode IV - A New Hope", "George Lucas", R.drawable.star_wars)); movies.add(new Movie("The Matrix", "The Wachowski Brothers", R.drawable.matrix)); movies.add(new Movie("Goodfellas", "Martin Scorsese", R.drawable.goodfellas)); movies.add(new Movie("Pulp Fiction", "Quentin Tarantino", R.drawable.pulp_fiction)); movies.add(new Movie("Schindler's List", "Steven Spielberg", R.drawable.schindlers_list)); MovieAdapter adapter = new MovieAdapter(this, movies); movieList.setAdapter(adapter); } } ``` 4. 创建一个MovieAdapter类,该类继承自BaseAdapter,并将Movie对象列表的每个电影显示ListView: ``` public class MovieAdapter extends BaseAdapter { private Context context; private List<Movie> movies; public MovieAdapter(Context context, List<Movie> movies) { this.context = context; this.movies = movies; } @Override public int getCount() { return movies.size(); } @Override public Object getItem(int position) { return movies.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.movie_item, null); } ImageView poster = (ImageView) view.findViewById(R.id.poster); TextView name = (TextView) view.findViewById(R.id.name); TextView director = (TextView) view.findViewById(R.id.director); Movie movie = movies.get(position); poster.setImageResource(movie.getPoster()); name.setText(movie.getName()); director.setText(movie.getDirector()); return view; } } ``` 5. 创建一个movie_item.xml布局文件,该布局将在ListView每个电影项的视图重复使用: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/poster" android:layout_width="100dp" android:layout_height="150dp" android:padding="10dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/director" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" /> </LinearLayout> </LinearLayout> ``` 6. 运行应用程序,您将看到一个基本的电影列表,其包含电影的名称,导演和海报图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值