Android如何在微调器上为每个列表项添加不同的图像?

时间:2012-07-09 13:42:54

标签: android

以下是地点选择器,我想在此微调器列表视图中添加不同的图标。它适用于我正在努力开发的应用程序。这样我也可以使用相同的其他微调器

public class Addplace extends Activity implements OnItemSelectedListener {

    public String items;
    EditText name;
    Button add;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addpoint);

        // Spinner element
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        name = (EditText) findViewById(R.id.nameplace);
        add = (Button) findViewById(R.id.addplace);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();
        categories.add("Outdoor");
        categories.add("Shop");
        categories.add("Professional & Other place");
        categories.add("College & University");
        categories.add("Food");
        categories.add("Travel");
        categories.add("Residences");
        categories.add("Club");
        categories.add("Bar & Pub");
        categories.add("Services");

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, categories);

        // Drop down layout style - list view with radio button
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);

        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String nameplace = name.getText().toString();

                Toast.makeText(getApplicationContext(), nameplace + items,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        // On selecting a spinner item
        items = parent.getItemAtPosition(position).toString();
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

请善意帮助。

日Thnx

1 个答案:

答案 0 :(得分:1)

您将需要扩展ArrayAdapter,并覆盖其方法并为每行充气所需的xml,请看一下:http://www.coderzheaven.com/2011/07/18/customizing-a-spinner-in-android/