如何获得,如果我选择了微调的项目,然后出现指定的列表项自定义列表视图(多列)? [英] How to get if i selected spinner item, then appear specified list item with custom list view (multicolumn)?

查看:133
本文介绍了如何获得,如果我选择了微调的项目,然后出现指定的列表项自定义列表视图(多列)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示列表视图值。我有一个下拉spinner.if我选择了微调的项目,指定列表项的值将显示在自定义列表视图。

how to display the listview value. I have one dropdown spinner.if i selected the spinner item, the specified list item value will be displayed in the custom listview.

// SampActivity.java

//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

//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"};
}

在此先感谢

推荐答案

看看这个:

    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中

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类:

CustomAdapter Class:

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:

and 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>

这篇关于如何获得,如果我选择了微调的项目,然后出现指定的列表项自定义列表视图(多列)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆