如何显示listview值。我有一个下拉式spinner。如果我选择了微调项目,指定的列表项值将显示在自定义列表视图中。
// 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"};
}
提前致谢
答案 0 :(得分:1)
看看这个:
listView=(ListView) findViewById(R.id.list);
spinner=(Spinner) findViewById(R.id.spin);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(HotelActivity.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 arg3) {
String Text = parent.getSelectedItem().toString();
if(Text.equals("----SELECT----")) {
}
else if(Text.equals("BEVERAGES")){
CustomAdapter bever=new CustomAdapter(HotelActivity.this,pos,Beverage.length);
listView.setAdapter(bever);
return;
}
else if(Text.equals("BREAKFAST")){
CustomAdapter breakfast=new CustomAdapter(HotelActivity.this,pos,Breakfast.length);
listView.setAdapter(breakfast);
return;
}
else if(Text.equals("LUNCH")){
CustomAdapter lunch=new CustomAdapter(HotelActivity.this,pos,Lunch.length);
listView.setAdapter(lunch);
return;
}
else if(Text.equals("DINNER")){
CustomAdapter dinner=new CustomAdapter(HotelActivity.this,pos,Dinner.length);
listView.setAdapter(dinner);
return;
}
else if(Text.equals("DESSERTS")){
CustomAdapter dessert=new CustomAdapter(HotelActivity.this,pos,Desserts.length);
listView.setAdapter(dessert);
return;
}
else if(Text.equals("APPETIZERS & SIDES")){
CustomAdapter app=new CustomAdapter(HotelActivity.this,pos,Appetizers.length);
listView.setAdapter(app);
return;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/spin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
CustomAdapter类:
public class CustomAdapter extends BaseAdapter{
Context context;
int pos;
int length;
LayoutInflater layoutInflater;
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"};
public CustomAdapter(Context context, int pos, int length) {
super();
this.context=context;
this.pos=pos;
this.length=length;
}
public int getCount() {
return length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
DataHolder holder=null;
if(convertView==null){
holder=new DataHolder();
layoutInflater=((Activity)context).getLayoutInflater();
convertView=layoutInflater.inflate(R.layout.list_item, parent, false);
holder.title=(TextView) convertView.findViewById(R.id.title);
holder.price=(TextView) convertView.findViewById(R.id.price);
convertView.setTag(holder);
}
else
{
holder=(DataHolder) convertView.getTag();
}
if(pos==1){
holder.title.setText(Beverage[position]);
holder.price.setText(Beveragecos[position]);
}
else if(pos==2){
holder.title.setText(Breakfast[position]);
holder.price.setText(Breakfastcos[position]);
}
else if(pos==3){
holder.title.setText(Lunch[position]);
holder.price.setText(Lunchcos[position]);
}
else if(pos==4){
holder.title.setText(Dinner[position]);
holder.price.setText(Dinnercos[position]);
}
else if(pos==5){
holder.title.setText(Desserts[position]);
holder.price.setText(Dessertscos[position]);
}
else if(pos==6){
holder.title.setText(Appetizers[position]);
holder.price.setText(Appertizerscos[position]);
}
return convertView;
}
static class DataHolder{
TextView title,price;
}
}
和list_item.xml:
<?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="30dp"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
答案 1 :(得分:1)
你可以通过applicationcontroller类来实现....只需要创建一个类ApplicationController,如下所示
public class ApplicationController extends Application {
// write your getters and setters here....
}
在Applicationcontroller类中,放入getter和setter方法。您可以在一个活动中通过set方法设置数据,并通过应用程序的其他活动中的get方法获取该数据。此类将数据存储在enitre application..Moreoevr中,您必须在manifest.xml文件中声明该calss
android:name=".your package name.ApplicationController"
上面应该在manifest.xml文件的应用标签中声明.....