In my application, i have activity group in which i have a custom ListView with ImageView, TextView and a spinner. Error comes when i click on the spinner. here is the code..
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;
}
}
In my another activity inside activity group, i have simple layout with spinner. To generate the spinner i have my code as below:
public class FoodDetailPage extends Activity {
Spinner spinner;
Button back_button,my_order,add_order;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceSt开发者_高级运维ate);
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);
}
Problem is when placing a spinner in custom ListView with Activity group, my application hangs.
Try passing this.getPrent()
to the MyCustomAdapter
like this:
MyCustomAdapter adapter = new MyCustomAdapter(this.getPrent(), mylist, 0, null, null);
and make a Context variable in the MyCustomAdapter
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
}
and pass that mContext
to the ArrayAdapter
inside getView()
ArrayAdapter.createFromResource(mContext, R.array.number, android.R.layout.simple_spinner_item);
精彩评论