我有一个带有自定义ArrayAdapter和2个自定义布局的ListView。
列表视图(1 x X)'single_row.xml' 网格视图(3 x X)'grid_row.xml'
我有两个RadioButton,在RadioGroup内,ID为'button1'& 'BUTTON2'。
理想情况下,我希望我的ListView默认为'single_row.xml',并将onClickListeners设置为两个RadioButtons。
伪代码:
如果单击button1,则适配器布局='single_row.xml' 如果单击button2,则适配器布局='grid_row.xml'
设置:
list = (ListView) findViewById(R.id.listView);
myAdapter adapter = new myAdapter(this, memeTitles, images, memeDescriptions);
list.setAdapter(adapter);
适配器:
class myAdapter extends ArrayAdapter<String> {
int size = 1;
Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
myAdapter(Context c, String[] titles, int imgs[], String[] desc) {
super(c, R.layout.grid_row, R.id.textView, titles);
this.context = c;
this.images = imgs;
this.titleArray = titles;
this.descriptionArray = desc;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.grid_row, parent, false);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
myImage.setImageResource(images[position]);
TextView myTitle = (TextView) row.findViewById(R.id.textView);
myTitle.setText(titleArray[position]);
TextView myDescription = (TextView) row.findViewById(R.id.textView2);
myDescription.setText(descriptionArray[position]);
return row;
}
}
编辑:有针对性的问题 - 我似乎无法拉入myAdapter变量。甚至通过myAdapter adapter1;
在类中全局声明它button1.setOnClickListener(new View.OnClickListener() {
myAdapter adapter1;
public void onClick(View v) {
// how do I change "View row = inflater.inflate(R.layout.grid_row, parent, false);"?
}
});
提前致谢!