Spinner用法与ListView用法

时间:2023-01-30 21:46:52

参考:

http://blog.csdn.net/u012960536/article/details/46732421

------------------------------------------------Spinner的简单用法(ArrayAdapter)--------------------------------------

1.布局文件定义一个是spinner

activity_main.xml

<LinearLayout 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:orientation
="vertical"
android:padding
="5dp"
tools:context
=".MainActivity">

<TextView
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="选择您的排位分段"
android:textColor
="#44BDED"
android:textSize
="18sp" />

<Spinner
android:id
="@+id/spin_one"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:entries
="@array/data"
android:spinnerMode
="dialog" />


</LinearLayout>

 2.Activity初始化Spinner

package com.example.spinnerdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner spinner
=(Spinner) findViewById(R.id.spin_one);
List
<String> list = new ArrayList<String>();
list.add(
"选项1");
list.add(
"选项3");
list.add(
"选项2");
ArrayAdapter
<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
spinner.setAdapter(adapter);


}

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

}

 

 3.效果:

Spinner用法与ListView用法

 

 

Spinner用法与ListView用法

 

 

 

如果页面中不初始化list。

Spinner用法与ListView用法

定义一个数组文件:

myarrays.xml

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

<string-array name="data">
<item>英勇青铜</item>
<item>不屈白银</item>
<item>荣耀黄金</item>
<item>华贵铂金</item>
<item>璀璨钻石</item>
<item>超凡大师</item>
<item>最强王者</item>
</string-array>

</resources>

 

页面效果:

Spinner用法与ListView用法

 

 

 

---------------------------ListView的简单用法(SimpleAdapter)--------------------------------

1.布局文件:

android:spinnerMode="dialog"可以修改打开方式
<LinearLayout 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:orientation
="vertical"
android:padding
="5dp"
tools:context
=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="选择您的排位分段"
android:textColor
="#44BDED"
android:textSize
="18sp" />

<Spinner
android:id="@+id/spin_one"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:entries
="@array/data"
android:spinnerMode
="dialog" />


<!-- 添加一个ListView控件 -->
<ListView
android:id="@+id/lv"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
/>


</LinearLayout>

 

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height
="fill_parent"
android:layout_width
="fill_parent">
<ImageView
android:layout_alignParentRight="true"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:id
="@+id/ItemImage"
/>
<TextView
android:id="@+id/ItemTitle"
android:layout_height
="wrap_content"
android:layout_width
="fill_parent"
android:textSize
="20sp"
/>
<TextView
android:id="@+id/ItemText"
android:layout_height
="wrap_content"
android:layout_width
="fill_parent"
android:layout_below
="@+id/ItemTitle"
/>
</RelativeLayout>

 

 

 

 

2.Activity代码:(图片要放在目录)

package com.example.spinnerdemo;

import java.util.ArrayList;
import java.util.HashMap;

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

public class MainActivity extends Activity {

private ListView lv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lv
= (ListView) findViewById(R.id.lv);
/*定义一个动态数组*/
ArrayList
<HashMap<String, Object>> listItem = new ArrayList<HashMap<String,Object>>();
/*在数组中存放数据*/
for(int i=0;i<5;i++)
{
HashMap
<String, Object> map = new HashMap<String, Object>();
map.put(
"ItemImage", R.drawable.ic_launcher);//加入图片
map.put("ItemTitle", "第"+i+"行");
map.put(
"ItemText", "这是第"+i+"行");
listItem.add(map);
}

SimpleAdapter mSimpleAdapter
= new SimpleAdapter(this,listItem,//需要绑定的数据
R.layout.item,//每一行的布局
//动态数组中的数据源的键对应到定义布局的View中
new String[] {"ItemImage","ItemTitle", "ItemText"},new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});

lv.setAdapter(mSimpleAdapter);
//为ListView绑定适配器

lv.setOnItemClickListener(
new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle(
"你点击了第"+arg2+"行");//设置标题栏显示点击的行

}
});
}
}

 

 

 

3.效果:

Spinner用法与ListView用法