android之listview

首先建立res/layout/data_list.xml;

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView 
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3px"/>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:gravity="left">
        <TextView 
            android:id="@+id/title"
            android:padding="3px"
            android:textSize="20px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView 
            android:id="@+id/author"
            android:padding="3px"
            android:textSize="15px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left">
        <TextView 
            android:id="@+id/type"
            android:padding="3px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView 
            android:id="@+id/score"
            android:padding="3px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

  然后编写res/layout/activity_main.xml;

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
	<TextView 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:textSize="25px"
	    android:gravity="center_horizontal"
	    android:text="西北农林科技大学视频列表"/>
	<ListView 
	    android:id="@+id/datalist"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"/>
</LinearLayout>

  最后定义MainActivity类;

代码如下:

package com.example.myporject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class MainActivity extends Activity {
	private int[] pic = new int[]{
			R.drawable.pic1,R.drawable.pic2,
			R.drawable.pic3,R.drawable.pic4
	};
	private String data[][] = new String[][]{
			{"编号1","白彩荣"},{"编号2","曹琦"},
			{"编号3","曾德星"},{"编号","李海涛"}
	};
	private List<Map<String,String>> list = new ArrayList<Map<String,String>>();
	private ListView datalist;
	private SimpleAdapter simpleAdapter = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.datalist = (ListView)super.findViewById(R.id.datalist);
        for(int x = 0;x<this.data.length;x++){
        	Map<String,String> map = new HashMap<String,String>();
        	map.put("pic", String.valueOf(this.pic[x]));
        	map.put("title", this.data[x][0]);
        	map.put("author", this.data[x][1]);
        	map.put("type", "免费");
        	map.put("score", String.valueOf(R.drawable.star));
        	this.list.add(map);
        }
        this.simpleAdapter = new SimpleAdapter(this,this.list,R.layout.data_list,new String[]{"pic","title","author","type","score"},new int[]{R.id.pic,R.id.title,R.id.author,R.id.type,R.id.score});
        this.datalist.setAdapter(this.simpleAdapter);	
    }
}

  注:需要在res/drawable文件中添加相应图片。

相应效果:

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。