Android中Spinner绑定实体对象

2017/6/8 11:18:47 人评论 次浏览 分类:项目实例

Activity代码如下:

package com.example.spinner;


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

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	private Spinner luxian_sp;
	private ArrayAdapter<XunJianSp> adapterxunjian;
	private List<XunJianSp> mitemsxj = new ArrayList<XunJianSp>();
	private TextView textView1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		xunjianshujv();
		textView1=(TextView)findViewById(R.id.textView1);
		luxian_sp=(Spinner)findViewById(R.id.luxian_sp);
		adapterxunjian = new ArrayAdapter<XunJianSp>(MainActivity.this, android.R.layout.simple_spinner_item,
				mitemsxj);
		adapterxunjian.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		luxian_sp.setAdapter(adapterxunjian);
		luxian_sp.setSelection(0);
		luxian_sp.setOnItemSelectedListener(new OnItemSelectedListener() {
			
			@Override
			public void onItemSelected(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
			int	xunjianid = mitemsxj.get(paramInt).getId();
			textView1.setText(mitemsxj.get(paramInt).getName());
	    	
			}

			@Override
			public void onNothingSelected(AdapterView<?> paramAdapterView) {
				// TODO Auto-generated method stub
			}
		});
	}

	@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);
	}
	
	private  void xunjianshujv(){
		XunJianSp one=new XunJianSp();
		one.setId(1);;
		one.setName("选项 1");
		mitemsxj.add(one);

		XunJianSp two=new XunJianSp();
		two.setId(2);;
		two.setName("选项 2");
		mitemsxj.add(two);

		XunJianSp three=new XunJianSp();
		three.setId(3);;
		three.setName("选项 3");
		mitemsxj.add(three);
		
	}

}

 

 

实体对象

package com.example.spinner;

public class XunJianSp {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
	
		return name;
	}

}

 

 

 

布局文件如下

<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="com.example.spinner.MainActivity" >


  <RelativeLayout
                         android:layout_width="match_parent"
                         android:layout_height="wrap_content" >
                                <TextView
                         android:id="@+id/textView1"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_alignBottom="@+id/relativeLayout1"
                         android:layout_alignParentLeft="true"
                         android:text="TextView" />

                     <Spinner
                         android:id="@+id/luxian_sp"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_below="@+id/textView1"
                         android:layout_toRightOf="@+id/relativeLayout1" />
                     </RelativeLayout>
</RelativeLayout>


附件下载

相关资源

    暂无相关的资源...

共有访客发表了评论 网友评论

验证码: 看不清楚?