我有一个带有自定义 ArrayAdapter 和 2 个自定义布局的 ListView。
列表视图 (1 x X) 'single_row.xml' 网格视图 (3 x X) 'grid_row.xml'
我有两个 RadioButton,在 ID 为“button1”和“button2”的 RadioGroup 内。
理想情况下,我希望我的 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);"?
}
});
提前致谢!