在values文件下新建一个arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <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> <item>李四</item> </string-array> </resources>
也可以在MainActivity.java文件中创建一个String[]name
在布局文件activity_mian.xml中添加ListView控件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="com.zsh.demo35.MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
修改MainActivity.java文件:
package com.bzu.test035listview;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private ListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list=(ListView) findViewById(R.id.listView1); //1.将可选内容与数据原链接起来,参数1:当前.参数2:数据源.参数3:样式1 // ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.names, // android.R.layout.simple_gallery_item); //样式2 // ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.names, // android.R.layout.simple_list_item_multiple_choice); //样式3 // ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, // //带选项的显示样式 // android.R.layout.simple_list_item_checked,name); //绑定数据源,并设置样式4 ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.names, android.R.layout.simple_list_item_single_choice); list.setAdapter(adapter); list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//设为单选,多选是CHOICE_MODE_MULTIPLE list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "您点击了"+(position+1)+"行,内容:" +list.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show(); } }); } @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 boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
运行效果图
ListView带自定义样式布局的使用
运行效果
新建一个文件,activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
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="com.zsh.demo036.MainActivity" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22px"/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px"/>
</LinearLayout>
</LinearLayout>
修改MainActivity.java文件,注意继承ListActivity:
package com.zsh.demo036;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter=new SimpleAdapter(this, getData(),R.layout.activity_main ,
new String[]{"title","info","img"},
new int[]{R.id.title,R.id.info,R.id.img});
setListAdapter(adapter);
}
private List<Map<String,Object>> getData(){
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
Map<String,Object> map=new HashMap<String, Object>();
map.put("title", "标题1");
map.put("info", "内容一...");
map.put("img",R.drawable.a1);
list.add(map);
map=new HashMap<String, Object>();
map.put("title", "标题二");
map.put("info", "内容二...");
map.put("img",R.drawable.a2);
list.add(map);
map=new HashMap<String, Object>();
map.put("title", "标题三");
map.put("info", "内容三...");
map.put("img",R.drawable.a3);
list.add(map);
map=new HashMap<String, Object>();
map.put("title", "标题四");
map.put("info", "内容四...");
map.put("img",R.drawable.a1);
list.add(map);
map=new HashMap<String, Object>();
map.put("title", "标题五");
map.put("info", "内容五...");
map.put("img",R.drawable.a1);
list.add(map);
map=new HashMap<String, Object>();
map.put("title", "标题六");
map.put("info", "内容六...");
map.put("img",R.drawable.a1);
list.add(map);
return list;
}
@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
个人参考Android API笔记,以待更新信息笔记.