リストビューの値を表示する方法。ドロップダウン スピナーが 1 つあります。スピナー アイテムを選択すると、指定したリスト アイテムの値がカスタム リストビューに表示されます。
//SampActivity.java
class GlobalClass extends Application {
public static List<String> myval=new ArrayList<String>();
}
public class SampActivity extends Activity {
String[] Category={"----SELECT---- ", "BEVERAGES","BREAKFAST","LUNCH","DINNER","DESSERTS","APPETIZERS & SIDES"};
Spinner spinner;
ListView l1;
String[] item;
int myid;
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner) findViewById(R.id.spinnerCategory);
l1.setAdapter(new EfficientAdapter(this));
l1=(ListView) findViewById (R.id.list);
b1=(Button)findViewById(R.id.button3);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button1);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, Category);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
String Text = parent.getSelectedItem().toString();
if(Text.equals("----SELECT----")) {
}
else if(Text.equals("BEVERAGES")){
l1.setAdapter(bever);
return;
}
else if(Text.equals("BREAKFAST")){
l1.setAdapter(Breakf);
return;
}
else if(Text.equals("LUNCH")){
l1.setAdapter(lunc);
return;
}
else if(Text.equals("DINNER")){
l1.setAdapter(Dinn);
return;
}
else if(Text.equals("DESSERTS")){
l1.setAdapter(Dessert);
return;
}
else if(Text.equals("APPETIZERS & SIDES")){
l1.setAdapter(Appet);
return;
}
}
public void onNothingSelected(AdapterView<?> arg0){
}
});
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "SELECTED :: " +((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
String st1 =(String) parent.getItemAtPosition(position);
GlobalClass.myval.add(st1);
}
});
}
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return StrActivity.Beverage.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView
.findViewById(R.id.TextView02);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(StrActivity.Beverage[position]);
holder.text2.setText(StrActivity.Beveragecos[position]);
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
}
}
}
//StrActivity.java
package com.tru.samp;
public class StrActivity {
public static String[] Beverage={"PEPSI","COKE","LASSI","FALOODA","BUTTER MILK","GREEN TEA","BADAM MILK","MASALA CHAI" };
public static String[] Beveragecos={"$ 0.5","$ 0.5","$ 2.0","$ 3.0","$ 1.5","$ 0.7","$ 0.8","$ 1.0"};
public static String[] Breakfast={"SIRLOIN & EGG","CFS STEAK & EGG","T-BONE & EGG","TWO EGGS BREAKFAST","2EGG W/MEAT","BEL WAFFLE","BEL WAFFLE W/MEAT","BLUEBERRY CAKE","CAKES","FABULOUS FRENCH TST","MOONS","FRENCH TST"};
public static String[] Breakfastcos={"$ 2.5","$ 3.0","$ 3.5","$ 2.7","$ 4.7","$ 3.0","$ 5.6","$ 3.0","$ 2.5","$ 3.2","$ 2.0","$ 1.0"};
public static String[] Lunch={" CRAB CAKE SANDWICH","TUSCAN GRILLED CHICKEN PANINI","SOUTHWEST TURKEY CLUB"," LOBSTER ROLL","TUNA MELT"," FRENCH DIP","YOUR OWN SANDWICH"};
public static String[] Lunchcos={"$ 4.0","$ 4.5","$ 7.0","$ 3.5","$ 5.5","$ 6.3","$ 5.0"};
public static String[] Dinner={"DUBLIN BAY PRAWN","CRAB FROM BRITTANY","WHITE ASPARAGUS","BLEWIT MUSHROOM","JOHN DORY FISH","MONKFISH","VEAL SWEET BREAD","LAMB","PIGEON FRY"};
public static String[] Dinnercos={"$ 3.5","$ 4.5","$ 3.3","$ 2.6","$ 4.3","$ 3.7","$ 2.5","$ 3.0","$ 4.0"};
public static String[] Desserts={"ECHOURGNAC CHEESE","HAZELNUT","GARIGUETTE STRAWBERRY","MOUSSE & ZEST","APRICOT","CHOCOLATE"};
public static String[] Dessertscos={"$ 1.0","$ 1.5","$ 2.7","$ 4.0","$ 2.9","$ 1.5"};
public static String[] Appetizers={"Greek Artichoke Spinach","Seafood Appetizers","Tapenade Flatbread","Cranberry Blue Cheese","Fig and Blue Cheese","Sun-Dried Tomato","Tropical Crab Rangoon"};
public static String[] Appertizerscos={"$ 1.3","$ 2.7","$ 1.5","$ 2.0","$ 2.5","$ 2.3","$ 3.0"};
}
前もって感謝します