Spinner会在很多的场景下面使用,比如选择城市,学校等,Listview的使用场景我也不用多说了,作为一个android菜鸟,我经常使用Listview。后来做项目中的一个功能,在我这个菜鸟来看,使用Spinner和Listview的结合效果还不错,但是貌似这个不是想象中的那么简单,因为Listview中的每个item都要有对应一个Spinner,这里面就出现了两个问题:
1、如何把每个Spinner中的数据放到ListView对应的item中,因为他俩都需要adapter传值
2、Spinner选中了之后,提交数据,如何把Spinner中选择的数据提交
由于这是我的第一次,我也不知道该怎么写,所以废话比较多,现在开始正题。下面是工程目录:
由于这个ListView是需要用adapter传数据进去,一般使用的都是Map向里面传值,Map初始化的时候已经确定了value的类型,Spinner传值需要List,所以如果要让ListView中显示Spinner就要自定义adapter。
我的工程中Text是我自己定义的一个类,title顾名思义标题,content表示内容其实就是存放Spinner数据的数组,id是代表这个Spinner中被选中的数据的数组下标。
<span style="font-size:24px;">package zcrazy.lsh.listviewspinner;
public class Text {
private String title;
private String[] content;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String[] getContent() {
return content;
}
public void setContent(String[] content) {
this.content = content;
}
}</span>
之后是自定义的adapter,在adapter中需要对Spinner进行监听,如果Spinner进行了选择,则将Text类中的id换成被选中数据的数组下标
<span style="font-size:24px;">package zcrazy.lsh.listviewspinner;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class TextAdapter extends BaseAdapter{
private List<Text> texts;
private Integer resource;
private Context context;
private LayoutInflater inflater;
public TextAdapter(Context context, List<Text> text,int resource){
this.texts=text;
this.resource=resource;
this.context=context;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return texts.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return texts.get(arg0);
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1==null){
arg1=inflater.inflate(resource, null);
}
final Text text = texts.get(arg0);
TextView titleView=(TextView)arg1.findViewById(R.id.title);
final Spinner contentView=(Spinner)arg1.findViewById(R.id.content);
contentView.setTag("");
contentView.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
text.setId(arg2);//每次Spinner中的值改变,Text类中的id就要改变
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
titleView.setText(text.getTitle());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_single_choice,text.getContent());
contentView.setAdapter(adapter);
return arg1;
}
}</span>
之后是主函数,这个也没什么好说的,都是基础代码
<span style="font-size:24px;">package zcrazy.lsh.listviewspinner;import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;public class ListView_SpinnerActivity extends Activity {
/* Called when the activity is first created. /private ListView listView; private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button1); show(); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { ListAdapter listAdapter = listView.getAdapter(); for(int i=0;i<listAdapter.getCount();i++){ Text text = (Text) listAdapter.getItem(i); Toast.makeText(ListView_SpinnerActivity.this,text.getTitle()+" "+text.getContent()[text.getId()], 0).show(); } } }); } @SuppressWarnings("static-access") private void show(){ List<Text> texts = new ArrayList<Text>(); for(int i=0;i<10;i++){//自定义的Text类存数据 Text text = new Text(); text.setTitle(String.valueOf(i));//标题数据 String[] temp = new String[10]; for(int j=0;j<10;j++){//Spinner数据<span style="white-space:pre"> temp[j]=String.valueOf(j);
<span style="white-space:pre"> </span>}</span>
<span style="white-space:pre"> </span>text.setId(0);//Spinner的默认选择项
text.setContent(temp);
texts.add(text);
}
TextAdapter textAdapter = new TextAdapter(ListView_SpinnerActivity.this, texts, R.layout.main_item);//向自定义的Adapter中传值
listView=(ListView)findViewById(R.id.mylist);
listView.setAdapter(textAdapter);//传值到ListView中
}
}</span>
上面是主要的文件,接下来是不主要的文件,看一下就好了
<span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout></span></span><span style="font-size:24px;"><span style="font-family:Times New Roman;"><?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="horizontal" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Spinner
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout></span><span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zcrazy.lsh.listviewspinner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ListView_SpinnerActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest></span>效果图如下:
好了我的第一次就这么快,结束了,不知道能不能让大家看得懂,也不知道会有多少人看到,如果有问题可以给我留言。如果转载了请标明出处