babyQ
spinner没有图片显示

我自定义了一个下拉列表样式如下

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<ImageView

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_centerVertical="true"

android:layout_marginLeft="20dp"

android:src="@drawable/ic_launcher" />

<TextView

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_marginLeft="25dp"

android:layout_toRightOf="@+id/image"

android:text="None" />

</RelativeLayout>

main.xml是这个

<?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="vertical" >

<TextView

android:id="@+id/textview"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textColor="#ff0000"

android:textSize="25sp"

/>

<Spinner

android:id="@+id/spinner"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

</LinearLayout>

最后是

package com.example.spinner;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.support.v7.app.ActionBarActivity;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Adapter;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.SimpleAdapter;

import android.widget.Spinner;

import android.widget.TextView;

public class MainActivity extends Activity implements OnItemSelectedListener{

private TextView tv;

private Spinner sp;

private List<Map<String, Object>>list;

private SimpleAdapter adapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tv=(TextView) findViewById(R.id.textview);

sp=(Spinner) findViewById(R.id.spinner);

list= new ArrayList<Map<String,Object>>();

getdata();

adapter=new SimpleAdapter(this, list, R.layout.item, new String[]{"inmage","text"},

new int[]{R.id.image,R.id.text});

adapter.setDropDownViewResource(R.layout.item);

sp.setAdapter(adapter);

sp.setOnItemSelectedListener(this);

}

private List<? extends Map<String, ?>> getdata() {

// TODO Auto-generated method stub

Map<String, Object>map=new HashMap<String, Object>();

map.put("image", R.drawable.ic_launcher);

map.put("text", "北京");

Map<String, Object>map2=new HashMap<String, Object>();

map2.put("image", R.drawable.ic_launcher);

map2.put("text", "上海");

Map<String, Object>map3=new HashMap<String, Object>();

map3.put("image", R.drawable.ic_launcher);

map3.put("text", "杭州");

Map<String, Object>map4=new HashMap<String, Object>();

map4.put("image", R.drawable.ic_launcher);

map4.put("text", "深圳");

list.add(map);

list.add(map2);

list.add(map3);

list.add(map4);

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);

}

@Override

public void onItemSelected(AdapterView<?> parent, View view, int position,

long id) {

// TODO Auto-generated method stub

tv.setText("你所选的城市是"+adapter.getItem(position));

}

@Override

public void onNothingSelected(AdapterView<?> parent) {

// TODO Auto-generated method stub

tv.setText("None");

}

}