活动组内自定义列表中的微调器问题

时间:2011-08-25 10:06:49

标签: android spinner

在我的应用程序中,我有一个活动组,其中我有一个带有ImageView,TextView和微调器的自定义ListView。当我点击微调器时出现错误。这是代码..

final ListView list = (ListView)findViewById(R.id.submenu_list);

         MyCustomAdapter adapter = new MyCustomAdapter(this, mylist, 0, null, null);
        list.setAdapter(adapter);
        list.setTextFilterEnabled(true);


private class MyCustomAdapter extends SimpleAdapter {

        public MyCustomAdapter(Context context,
        List<? extends Map<String, ?>> data, int resource,
        String[] from, int[] to) {
            super(context, data, resource, from, to);
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.submenu_items,
             null);

            }

 ((TextView) convertView.findViewById(R.id.food_name))
         .setText(estimated[position]);

 ((TextView) convertView.findViewById(R.id.prize))
 .setText("Price : "+price[position]);

 ((ImageView) convertView.findViewById(R.id.imageview))
         .setImageResource(image[position]);



 Spinner spinner = (Spinner) convertView.findViewById(R.id.spinner);

    ArrayAdapter<CharSequence> sadapter = ArrayAdapter.createFromResource(getParent(), R.array.number, android.R.layout.simple_spinner_item);
    sadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(sadapter);


 return convertView;
}

}

在我的另一个活动内部活动组中,我使用微调器进行简单布局。要生成微调器,我的代码如下:

 public class FoodDetailPage extends Activity {

    Spinner spinner;
    Button back_button,my_order,add_order;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.food_details, null);
        this.setContentView(viewToLoad); 

        back_button = (Button) findViewById(R.id.back_button);

        add_order = (Button) findViewById(R.id.add_order);

        back_button.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Intent intent =new Intent(FoodDetailPage.this,SubMenu.class);
                startActivity(intent);
            }
        });


        this.spinner =(Spinner) findViewById(R.id.spinner);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.number, R.layout.spinnerlayout);
        adapter.setDropDownViewResource(R.layout.spinnerlayout);
        spinner.setAdapter(adapter);

    }

问题是将微调器放在带有Activity组的自定义ListView中时,我的应用程序会挂起。

1 个答案:

答案 0 :(得分:2)

尝试将this.getPrent()传递给MyCustomAdapter,如下所示:

MyCustomAdapter adapter = new MyCustomAdapter(this.getPrent(), mylist, 0, null, null);

并在MyCustomAdapter中创建一个Context变量

private class MyCustomAdapter extends SimpleAdapter {
        Context mContext;
        public MyCustomAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
        {
            super(context, data, resource, from, to);
            mContext = context;
        }
        //getView implementation
}

并将mContext传递给ArrayAdapter

内的getView()
ArrayAdapter.createFromResource(mContext, R.array.number, android.R.layout.simple_spinner_item);