13、Android之ListView学习·

1、XML文件中添加ListView

(1)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/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:entries="@array/names"
        android:divider="#aa0000"
        android:dividerHeight="20dp"
        android:drawSelectorOnTop="false"
        android:scrollbars="horizontal|vertical"
        android:fastScrollEnabled="true"
        android:listSelector="#00ffff"
        >  
    </ListView>
  
</RelativeLayout>

(2)string.xml

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

    <string name="app_name">ListViewDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="names">
        <item>水电工</item>
        <item>定个的</item>
        <item>挨饿高</item>
        <item>反倒是</item>
        <item>改动份</item>
        <item>推荐个</item>
        <item>车厢内</item>
        <item>认同感</item>
        <item>参与奖</item>
        <item>如啼眼</item>
        <item>风格化</item>
        <item>电饭锅</item>
        <item>就快了</item>
        <item>老婆的</item>
        <item>错的人</item>
    </string-array>
</resources>

(3)MainActivity

package com.example.listviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener{

	private ListView lv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv = (ListView) findViewById(R.id.listView);
		lv.setOnItemClickListener(this);
	}

	@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;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		System.out.println("Parent = " + arg0.getClass());
		System.out.println("View = " + arg1);
		TextView tv = (TextView)arg1;
		Toast.makeText(this,tv.getText(), Toast.LENGTH_SHORT).show();
		System.out.println("Position = " + arg2);
		System.out.println("ID = " + arg3);
	}
}

2、使用使用ListActivity和ArrayAdapter

由于ListActivity本身就是一个Activity,所以只有当页面仅存在一个空间,且空间的ID是一定的。
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity2 extends ListActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		String[] names = {"张三","李四","赵武","王六","周琦","养吧","文档",};
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,names);
		
		setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		TextView tv = (TextView)v;
		Toast.makeText(this,tv.getText(), Toast.LENGTH_SHORT).show();
	}
}

3、使用Adapter创建ListView,实现单选和复选

(1)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" >
    	
    <ListView 
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </ListView>

</LinearLayout>

(2)Activity

package com.example.listviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity3 extends Activity implements OnItemClickListener{
	
	private ListView lv;  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main3);
		lv = (ListView) findViewById(R.id.listView1);
		
		String[] citys = {"北京","上海","广州","深圳","东光","中国"};
		
		//单选模式
//		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,citys);
//		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		//多选模式
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,citys);
		lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		lv.setOnItemClickListener(this);//注册事件
		lv.setAdapter(adapter);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
		System.out.println("View = " + arg1);
	}	
}








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