Reputation: 5551
I have a ListView with a custom ArrayAdapter and 2 Custom Layouts.
List View (1 x X) 'single_row.xml' Grid View (3 x X) 'grid_row.xml'
I have two RadioButtons, inside a RadioGroup with ID's 'button1' & 'button2'.
Ideally, I would like my ListView to be default 'single_row.xml' and set onClickListeners to both RadioButtons.
Pseudocode:
If button1 is clicked, adapter layout = 'single_row.xml' If button2 is clicked, adapter layout = 'grid_row.xml'
The Setup:
list = (ListView) findViewById(R.id.listView);
myAdapter adapter = new myAdapter(this, memeTitles, images, memeDescriptions);
list.setAdapter(adapter);
The 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;
}
}
EDIT: Targeted Question - I can't seem to pull in the myAdapter variable. Even by declaring it globally in the class with 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);"?
}
});
Thanks in advance!
Upvotes: 0
Views: 3228
Reputation: 2295
what you need to do is use the View Types (Tutorial) and get set the type based on the Radio buttton.
however that being said you're going to run into an issue due to how your adapter is currently working, you need to have an object (rather then String) backing your Adapter so that when the radio button is changed it will set it on that object and later when your view gets recycled (read about that here) you wont lose that state.
so in your case once you get the view types figured out you're going to want to do base the view type off the saved radio button state, then when get view gets called you inflate that view instead, you'll also have to refresh the view on radio button change.
Upvotes: 1