关闭

android 基础-自定义listView的实现 adapter 方法解析

标签: androidlistview布局xml
262人阅读 评论(0) 收藏 举报
分类:
*BaseAdapter  自定义adapter适配listview
 *getCount()方法 设置ListView显示条目个数
 *getView()方法 设置指定位置的条目视图
 *string 资源文件的使用

 *News类——属性的封装

public class MainActivity extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.list);
String[]data={"的萨芬的","阿萨德发送","阿瓦尔维权","违法打算","怀仁堂噶","七4人","圣达菲尔","阿凡达岁的","犹太人","阿尔","温柔的","抢 ","高峰时段低点",};
int[]imges={R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8,R.drawable.image9,R.drawable.image10,R.drawable.image11,R.drawable.image12,R.drawable.image13};
Resources resources=getResources();
String[]title=resources.getStringArray(R.array.titles);
ArrayList<News> dataList=new ArrayList<News>();

for(int i=0;i<data.length;i++){
News info=new News();
info.setData(data[i]);
info.setImages(imges[i]);
info.setTitle(title[i]);
dataList.add(info);
}
MyAdapter adapter=new MyAdapter(MainActivity.this,dataList);
list.setAdapter(adapter);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


public class MyAdapter extends BaseAdapter{
int i=0;
LayoutInflater inflater;
LinearLayout layout;
ArrayList<News> arrayList;
public MyAdapter(Context context,ArrayList<News> arrayList){
this.inflater = LayoutInflater.from(context);
this.i=arrayList.size();
this.arrayList=arrayList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return i;
}


@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//1、得到视图对象
//找到当前布局组件
//得到布局加载器

//inflater.inflate(R.layout.listitem, null)得到布局 第二个参数一般为空;
LinearLayout layout=(LinearLayout)inflater.inflate(R.layout.listitem, null);
ImageView image=(ImageView)layout.findViewById(R.id.item_img);
TextView tv=(TextView)layout.findViewById(R.id.item_tv);
TextView title=(TextView)layout.findViewById(R.id.item_title);
//2、设置数据到视图对象上

// News news=(News)arrayList.get(position);
image.setImageResource(arrayList.get(position).getImages());
tv.setText(arrayList.get(position).getData());
title.setText(arrayList.get(position).getTitle());
//3、添加到ListView

return layout;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}


@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}


public class News {
private  String title,data;
private  int images;
public  String getTitle() {
return title;
}
public  void setTitle(String title) {
this.title = title;
}
public  String getData() {
return data;
}
public  void setData(String data) {
this.data = data;
}
public  int getImages() {
return images;
}
public  void setImages(int images) {
this.images = images;
}
}


activity_main.xml

<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/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


   <ListView
       android:id="@+id/list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
/>

</RelativeLayout>



listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<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/item_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <ImageView
            android:id="@+id/item_img"
            android:layout_width="75dp"
            android:layout_height="75dp" />


        <TextView
            android:id="@+id/item_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />
    </LinearLayout>


</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">DalyWork4_Demo02(ListView BaseAdapter)</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
<string-array name="titles">
   <item>1</item>
   <item>2</item>
   <item>3</item>
   <item>4</item>
   <item>5</item>
   <item>6</item>
   <item>7</item>
   <item>8</item>
   <item>9</item>
   <item>0</item>
   <item>-</item>
   <item>1</item>
   <item>2</item>
</string-array>
</resources>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25216次
    • 积分:640
    • 等级:
    • 排名:千里之外
    • 原创:45篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条