listview使用BaseAdapter显示图片和文字
2016-04-27 18:04
453 查看
package com.example.listview_5; 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.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private List<Animal> animalList; private ListView animalListView; private BaseAdapter animalAdapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); animalListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO 自动生成的方法存根 Toast.makeText(MainActivity.this, "您按的是第"+String.valueOf(position)+"个view", Toast.LENGTH_SHORT).show(); } }); }///onCreate private void init() { // TODO 自动生成的方法存根 animalList = new ArrayList<Animal>(); int[] imageRes = {R.drawable.monkey,R.drawable.rabbit,R.drawable.rat}; String[] names=getResources().getStringArray(R.array.animals); for(int i=0;i<imageRes.length;i++){ Animal animal = new Animal(imageRes[i],names[i]); animalList.add(animal); } animalListView=(ListView)findViewById(R.id.animalListView); animalAdapter = new animalAdapter(); animalListView.setAdapter(animalAdapter); }//init public class animalAdapter extends BaseAdapter { @Override public int getCount() { // TODO 自动生成的方法存根 return animalList.size(); } @Override public Object getItem(int position) { // TODO 自动生成的方法存根 return animalList.get(position); } @Override public long getItemId(int position) { // TODO 自动生成的方法存根 return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO 自动生成的方法存根 View layout = View.inflate(MainActivity.this, R.layout.activity_item_anmials, null); // ImageView image =(ImageView)findViewById(R.id.image); // TextView name = (TextView)findViewById(R.id.name); ImageView image = (ImageView)layout.findViewById(R.id.image);//// 一定别忘了是layout.findViewById() TextView name = (TextView)layout.findViewById(R.id.name); Animal animal = animalList.get(position); image.setImageResource(animal.getImageSrc()); name.setText(animal.getName()); return layout; }//getView }///animalAdapter }
</pre><pre code_snippet_id="1664200" snippet_file_name="blog_20160427_4_4592303" name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">package com.example.listview_5;</span>
public class Animal { int imageSrc; String name; public Animal(int imageSrc, String name) { super(); this.imageSrc = imageSrc; this.name = name; } public int getImageSrc() { return imageSrc; } public void setImageSrc(int imageSrc) { this.imageSrc = imageSrc; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/animalListView" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#ccc" android:dividerHeight="1dp" > </ListView> </LinearLayout>
<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" android:paddingBottom="@dimen/vertical_margin" android:paddingLeft="@dimen/horizontal_margin" android:paddingRight="@dimen/horizontal_margin" android:paddingTop="@dimen/vertical_margin" > <ImageView android:id="@+id/image" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/rabbit"/> <TextView android:id="@+id/name" android:layout_toRightOf="@id/image" android:layout_width="wrap_content" android:layout_height="80dp" android:text="兔子" android:layout_marginLeft="25dp" android:textSize="20sp" android:gravity="center_vertical" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">ListView_5</string> <string name="hello_world">Hello world!</string> <string-array name="animals"> <item>猴子</item> <item>兔子</item> <item>老鼠</item> </string-array> </resources>
相关文章推荐
- 完美实现Android ListView中的TextView的跑马灯效果
- android上改变listView的选中颜色
- Delphi7中Listview的常用功能汇总
- Delphi控件ListView的属性及使用方法详解
- android中ListView数据刷新时的同步方法
- Android提高之ListView实现自适应表格的方法
- Android中实现水平滑动(横向滑动)ListView示例
- C#实现ListView选中项向上或向下移动的方法
- Listview加载的性能优化是如何实现的
- C#实现listview Group收缩扩展的方法
- C# listview添加combobox到单元格的实现代码
- ListView 百分比进度条(delphi版)
- Android listview多视图嵌套多视图
- ListView Adapter优化 实例
- Android用ListView显示SDCard文件列表的小例子
- Adapter实现ListView带多选框等状态的自定义控件的注意事项
- asp.net ListView 数据绑定
- Android之ScrollView嵌套ListView和GridView冲突的解决方法
- android ListView和ProgressBar(进度条控件)的使用方法
- Android实现ListView异步加载图片的方法