关闭

android 基础-自定义listView的实现

标签: androidlistview布局xml
243人阅读 评论(0) 收藏 举报
分类:
/**
 * @author Administrator
 *BaseAdapter  自定义adapter适配listview
 *getCount()方法 设置ListView显示条目个数
 *getView()方法 设置指定位置的条目视图
 *string资源文件的使用
 *
 */
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<HashMap<String,Object>> dataList=new ArrayList<HashMap<String,Object>>();
for(int i=0;i<data.length;i++){
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("image",imges[i]);
map.put("text",data[i]);
map.put("titles", title[i]);
dataList.add(map);
}

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;
String []data,titles;
int[]images;
public MyAdapter(Context context,ArrayList<HashMap<String,Object>> arrayList){
this.inflater = LayoutInflater.from(context);
this.i=arrayList.size();
images=new int[i];
data=new String[i];
titles=new String[i];
for(int j=0;j<arrayList.size();j++)
{
data[j]=(String)arrayList.get(j).get((Object)("text"));
images[j]=Integer.parseInt(arrayList.get(j).get((Object)"image").toString());
titles[j]=(String)arrayList.get(j).get((Object)("titles"));
}

}
@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.title);
//2、设置数据到视图对象上

image.setImageResource(images[position]);
tv.setText(data[position]);
title.setText(titles[position]);
//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;
}
}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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" >
    </ListView>


</LinearLayout>


listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <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"
            android:src="@drawable/ic_launcher" />


        <TextView
            android:id="@+id/item_tv"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="kjdflka"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>


0
0

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