listView的小demo

主Acticity:

package com.example.listviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ListView list ;
	private MyAdapter adapter ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		list = (ListView) findViewById(R.id.mylist);
		
		String[] data= new String[20];
		int length = data.length;
		for(int i = 0 ; i < length ; i++ ){
			data[i] = i + "" ;
		}
		
		adapter = new MyAdapter(getApplicationContext(),data);
		
		list.setAdapter(adapter);
		
	}

}

自定义adapter:

package com.example.listviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

	private String[] txt1 ;
	private Context mContext ;
	private LayoutInflater inflater;
	
	public MyAdapter(Context ctx  ,String[] str1 ){
		this.mContext = ctx ;
		this.txt1 = str1;
		this.inflater = LayoutInflater.from(mContext) ;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return txt1.length;
	}

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

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		convertView = inflater.inflate(R.layout.item, null);
		TextView text = (TextView)convertView.findViewById(R.id.txt1);
		text.setText(txt1[position]);
		return convertView;
	}

}

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"
    >

    <ListView 
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

item.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="wrap_content"
    >

    <TextView 
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        />
    
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_centerHorizontal="true"
        />
    <TextView 
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/gudingneirong"
        />

</RelativeLayout>

最后 在string.xml加上 <string name="gudingneirong">固定内容</string>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值