我在android studio中的java新手。我不知道现在该做什么,我有一个微调器“亩”,我把物品放在“mu”与AarrayAdapter(makanan_pokok)里面,我的问题是我如何为旋转器内的每个物品赋予一个值并将其放入mapo?感谢...
package stmikbumigoramataram.simulasipemodelan;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String[] makanan_pokok = {"...","Nasi Putih 175 kal/200gr","Lontong 38 kal/200gr","Roti Tawar 145 kal/60gr","Bubur 44 kal/200gr"};
public int mapo,lapak,samur;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tmblhitung = (Button)findViewById(R.id.bhitung);
Spinner mu = (Spinner)findViewById(R.id.makanan_utama);
ArrayAdapter Arr_mu = new ArrayAdapter(this,android.R.layout.simple_spinner_item,makanan_pokok);
Arr_mu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mu.setAdapter(Arr_mu);
}
public void hitung_kalori(){
switch (...){
case 1:
mapo = 0;
case 2:
mapo = 175;
case 3:
mapo = 38;
case 4:
mapo = 145;
}
}
}
答案 0 :(得分:0)
声明自定义类
public class Item
{
String name;
int value;
public item(){}
public item(String name,int value){
this.name=name;
this.value=value;
}
@override
public String toString(){
return name;
}
}
现在更新现有代码
Item[] makanan_pokok =new Item[5];
makanan_pokok[0]=new Item("",0);
makanan_pokok[0]=new Item("Nasi Putih 175 kal/200gr",175);
// ...rest of items...
然后创建适配器
ArrayAdapter<Item> Arr_mu =
new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1,
items);
Arr_mu.setDropDownViewR
esource(android.R.layout.simple_spinner_dropdown_item);
mu.setAdapter(Arr_mu);
获取所选项目实现.listener
AdapterView.OnItemSelectedListener
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long id) {
Toast.makeText(getApplicationContext(), animalList[position],
Toast.LENGTH_LONG).show();
}