如何使用微调器并从android中的数组填充它

时间:2012-03-19 10:58:44

标签: android android-spinner

我是Android开发的新手,我想实现Spinner。我的问题是我有一个数组,其中有一个关键类别的值为9。我的数组看起来像:

MyArray
[{Category=Things To Do}, 
 {Category=Transportation}, 
 {Category=Food  and  Drink}, 
 {Category=Accommodation}, 
 {Category=Shopping},
 {Category=Money and Costs}, 
 {Category=Business}, 
 {Category=Turkey Tour},
 {Category=Events}]

我希望获得类别键的价值。我需要MyArray for spinner如下:

MyArray
    {
    Things To Do
    Transportation
    Food and Drink
    Accommodation
    Shopping
    ......    
    } 

它类似于iPhone代码。

// iPhone code 
for(int i=0; i<[arrayCategory count]; i++)
    {
    NSString *strSubTitle=[[arrayCategory objectAtIndex:i]objectForKey:@"category"];
    }

有什么想法吗?

请帮帮我。

由于

4 个答案:

答案 0 :(得分:10)

首先在String数组的字符串中创建一个xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
    <string name="planet_prompt">Choose a planet</string>
   <string-array name="planets_array">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
   </string-array>
</resources>

然后在代码中使用上面的xml作为字符串数组,如下所示

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

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.planets_array, android.R.layout.simple_spinner_item);
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     spinner.setAdapter(adapter);
  }

答案 1 :(得分:4)

尝试这种方式,它会正常工作。

public class ArrayAdapterDemo2 extends Activity implements
        AdapterView.OnItemSelectedListener {
    TextView selection;
    String[] category= { "football","cricket" ,"baseball"};

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        selection = (TextView) findViewById(R.id.selection);

        Spinner spin = (Spinner) findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);

        ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item, category);

        aa.setDropDownViewResource(
           android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        selection.setText(items[position]);
    }

    public void onNothingSelected(AdapterView<?> parent) {
        selection.setText("");
    }
}

答案 2 :(得分:0)

如果我正确理解你的问题,这将是sol 1.创建Spinner Spinner微调器;

2.创建数据String[] category={"Cricket","football".....};

3.spinner.setAdapter(new Arrayadapter(this,android.R.layout.simple_spinner_item,category));

4.在微调器上选择特定值以获取值

spinner.setOnItemSelected(new onItemselected(public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
// to get selected Value
Toast.makeText(getApplicationContext(),"selected:"+category[pos],5000).show();
});

答案 3 :(得分:0)

import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

public class AndroidSQLite extends Activity implements OnItemSelectedListener {
 Spinner spinnergame;
 String[] games = { "football","cricket" ,"baseball"};

 @Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
 setContentView(R.layout.creategame);

spinnergame = (Spinner) findViewById(R.id.game);
spinnergame.setOnItemSelectedListener(this);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, games);

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnercity.setAdapter(aa);
public String getSpinnerSelection() {
    return this.selectedspinnervalue;
}

public void setSpinnerSelection(String selection) {
    this.selectedspinnervalue = selection;
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {



}


public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    selection.setText("");

  }
}