关闭

Android入门- listView的实现 SimpleAdapter

标签: androidlistviewimageviewapkxml
236人阅读 评论(0) 收藏 举报
分类:

 listView的实现 SimpleAdapter

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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};
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]);
dataList.add(map);
}
ListView simpleList=(ListView)findViewById(R.id.simpeList);
SimpleAdapter simpleadapter=new SimpleAdapter(MainActivity.this,dataList,R.layout.listitem,new String[]{"image","text"},new int[]{R.id.item_img,R.id.item_tv});
simpleList.setAdapter(simpleadapter);
}
@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;
}
}


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/simpeList"
        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="wrap_content"
    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>


0
0

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