2011-11-04 63 views
0

我正在使用BaseAdapter for ListView。自定義列表視圖中的每一行都包含一個微調器和一個按鈕。當我點擊列表行中的按鈕時,我必須從同一個列表行中的微調器中獲取selectedItem(),點擊該按鈕。Android將微調器置於自定義列表視圖中

private class MyCustomAdapter extends BaseAdapter { 
    private Activity activity; 
    private String[] estimated, price, image; 
    private LayoutInflater inflater=null; 
    public ViewHolder holder; 
    public ProductImageLoader imageLoader; 
    private ArrayList<String> arraylist4; 
    SQLiteDatabase db; 
    View vi; 

    public MyCustomAdapter(SubMenu subMenu, String[] estimated, String[] price, String[] image) { 
     // TODO Auto-generated constructor stub 
     this.activity = subMenu; 
     this.estimated = estimated; 
     this.price = price; 
     this.image = image; 
     inflater = (LayoutInflater)getParent().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ProductImageLoader(activity.getParent()); 

    } 
@Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return estimated.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
    public class ViewHolder{ 
     public TextView text; 
     public TextView text1; 
     public ImageView image_view; 
     public Button button, add_btn; 
     public Spinner spinner; 
    } 
@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     vi=convertView; 

     if(convertView==null){ 

      vi = inflater.inflate(R.layout.submenu_items, null); 
      holder=new ViewHolder(); 
      holder.text=(TextView)vi.findViewById(R.id.food_name); 
      holder.text1=(TextView)vi.findViewById(R.id.prize); 
      holder.image_view=(ImageView)vi.findViewById(R.id.imageview); 
      holder.button=(Button)vi.findViewById(R.id.detail_btn); 
      holder.add_btn=(Button)vi.findViewById(R.id.addorder_btn); 
      holder.spinner=(Spinner)vi.findViewById(R.id.spinner); 
      vi.setTag(holder); 
     } 
     else 
      holder=(ViewHolder)vi.getTag(); 

     holder.text.setText(estimated[position]); 
     holder.text1.setText(price[position]); 
     holder.image_view.setTag(image[position]); 
     imageLoader.DisplayImage(image[position], activity, holder.image_view); 
     holder.button.setOnClickListener(new OnClickListener() { 
@Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Bundle bundle = new Bundle(); 
     bundle.putString("url", image[position]); 
     bundle.putString("description", description[position]); 
     Log.d("url",""+description[position]); 

     Intent intent = new Intent(SubMenu.this,FoodDetailPage.class); 
     intent.putExtras(bundle); 
     TabGroupActivity parentActivity = (TabGroupActivity)getParent(); 
     parentActivity.startChildActivity("FoodDetailPage", intent); 
     } 

     }); 
     holder.add_btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Log.d("row spinner", ""+holder.spinner.getChildAt(position)); 
       try { 
        db = SQLiteDatabase.openDatabase("data/data/com.android.restaurant1/Restaurant", null, SQLiteDatabase.CREATE_IF_NECESSARY); 
        db.beginTransaction(); 
        db.execSQL("insert into table1(MenuName, Count, Price) values('"+estimated[position]+"', '"+holder.spinner.getSelectedItem()+"', '"+price[position]+"')"); 
        db.setTransactionSuccessful(); 
        db.endTransaction(); 
        db.close(); 
       } 
       catch(SQLException e) { 

       } 
      } 
     }); 
     return vi; 
    } 

} 
+0

http://app-solut.com/2011/03/using-custom-layouts-for-spinner-or-listview-entries-in-android/ 請嘗試此鏈接的微控制器在listview。 – Noman

+0

在該鏈接中,微調器和列表視圖在一個佈局中使用。但我需要的是在自定義列表項中使用微調框。 –

回答

0

創建自定義適配器。裏面的getView()在Button上設置ClickListener,並在此偵聽器中讀取微調器的選定值。

+0

我使用我的代碼編輯了我的帖子...請通過它並告訴我在哪裏創建自定義適配器 –