android控件开发之ListView
android控件开发之ListView
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//声明list,且list中的单元是map
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
//声明map,且map中的元素是String键值对
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
//向map中put键值对数据
map1.put("name", "zhangsan");
map1.put("ip", "192.168.1.1");
map2.put("name", "lisi");
map2.put("ip", "192.168.1.2");
map3.put("name", "wangwu");
map3.put("ip", "192.168.1.3");
//将map添加到list中
list.add(map1);
list.add(map2);
list.add(map3);
//生成adapter适配器
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.user, new String[]{"name","ip"},
new int[]{R.id.name,R.id.ip});
setListAdapter(listAdapter);
}
//给list绑定监听器,当按下list中某一个单元时,就会相应此函数
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
System.out.println("id = " + id);
System.out.println("position = " + position);
}
@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;
}
}
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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/listLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"></ListView>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:ignore="UselessParent" >
<TextView
android:id="@+id/name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="20dip"
android:gravity="center_vertical"/>
<TextView
android:id="@+id/ip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"/>
</LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。