I am trying to add to this example http://metanit.com/java/android/5.6.php GridView
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="100dp" android:numColumns="2" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center"> <ImageView android:id="@+id/flag" android:layout_marginLeft="8dip" android:layout_marginBottom="3dip" android:layout_marginTop="3dip" android:layout_width="73dip" android:layout_height="48dip" /> <LinearLayout android:orientation="vertical" android:paddingRight="8dip" android:paddingLeft="16dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:text="Название" android:textAppearance="@android:style/TextAppearance.Medium" /> <TextView android:id="@+id/capital" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:text="Столица" android:textAppearance="@android:style/TextAppearance.Small" /> </LinearLayout> </GridView> </LinearLayout> MainActivity:
public class MainActivity extends ListActivity { private State[] states = { new State ("Бразилия", "Бразилиа", R.drawable.brasil), new State ("Аргентина", "Буэнос-Айрес", R.drawable.argentina), new State ("Колумбия", "Богота", R.drawable.columbia), new State ("Уругвай", "Монтевидео", R.drawable.uruguay), new State ("Чили", "Сантьяго", R.drawable.chilie), new State ("Бразилия", "Бразилиа", R.drawable.brasil), new State ("Аргентина", "Буэнос-Айрес", R.drawable.argentina), new State ("Колумбия", "Богота", R.drawable.columbia), new State ("Уругвай", "Монтевидео", R.drawable.uruguay), new State ("Чили", "Сантьяго", R.drawable.chilie), new State ("Бразилия", "Бразилиа", R.drawable.brasil), new State ("Аргентина", "Буэнос-Айрес", R.drawable.argentina), new State ("Колумбия", "Богота", R.drawable.columbia), new State ("Уругвай", "Монтевидео", R.drawable.uruguay), new State ("Чили", "Сантьяго", R.drawable.chilie), }; @Override public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setListAdapter(new StateAdapter(states)); AdapterView.OnItemClickListener itemListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // получаем выбранный пункт State selectedState = (State)parent.getItemAtPosition(position); Toast.makeText(getApplicationContext(), "Был выбран пункт " + selectedState.getName(), Toast.LENGTH_SHORT).show(); } }; getListView().setOnItemClickListener(itemListener); } private State getModel(int position) { return(((StateAdapter)getListAdapter()).getItem(position)); } class StateAdapter extends ArrayAdapter<State> { private LayoutInflater mInflater; StateAdapter(State[] list) { super(MainActivity.this,R.layout.activity_main, list); mInflater = LayoutInflater.from(MainActivity.this); } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; View row=convertView; if(row==null){ row = mInflater.inflate(R.layout.activity_main, parent, false); holder = new ViewHolder(); holder.imageView = (ImageView) row.findViewById(R.id.flag); holder.nameView = (TextView) row.findViewById(R.id.name); holder.capitalView = (TextView) row.findViewById(R.id.capital); row.setTag(holder); } else{ holder = (ViewHolder)row.getTag(); } State state = getModel(position); holder.imageView.setImageResource((state.getFlagResource())); holder.nameView.setText(state.getName()); holder.capitalView.setText(state.getCapital()); return row; } class ViewHolder { public ImageView imageView; public TextView nameView, capitalView; } } } When I run on the emulator, I get the error:
FATAL EXCEPTION: main
java.langUnsupportedOperationException: addView (View, LayoutParams) is not supported in AdapterView
swears at this line:
row = mInflater.inflate(R.layout.activity_main, parent, false); explain how to remake the original example under the GridView?