グリッドビューにボタンを1つ追加したいのですが、ボタンをドラッグアンドドロップしてプロジェクトを実行すると、ボタンがグリッドビューの各フィールドにあることがわかります...2つの線形レイアウトを作成するなどのソリューションを試し、ボタンを配置しました別のレイアウトですが、変更はありません! コードは次のとおりです。
public class gridview extends Activity{
GridView grid_main;
Context context;
TextView tv;
EditText tx;
ImageAdapter grid;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.gridview);
grid_main = (GridView)findViewById(R.id.gridview);
grid_main.setAdapter(new ImageAdapter(this));
grid= new ImageAdapter(context);
// grid_main.setOnItemClickListener(new OnItemClickListener() {
// public void onItemClick(AdapterView parent, View v, int position, long id) {
// grid .getCount();
// grid .getItem(position);
// }
// });
}
public class ImageAdapter extends BaseAdapter{
Context mContext;
public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
v= inflater.inflate(R.layout.list_item,parent, false);
tv = (TextView)v.findViewById(R.id.textView1);
tx = (EditText)v.findViewById(R.id.editText1);
tv.setText(""+R.drawable.download);
ImageView iv = (ImageView)v.findViewById(R.id.imageView1);
iv.setImageResource(mThumbIds[position]);
}
else
{
v = convertView;
}
return v;
}
private Integer[] mThumbIds = {
R.drawable.download,
R.drawable.download,
R.drawable.french,
R.drawable.download,
R.drawable.download,
R.drawable.french,
R.drawable.download,
R.drawable.download,
R.drawable.french,
R.drawable.french
};
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
}
}
xml ファイル:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="366dp"
android:orientation="vertical" >
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/download" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textview" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
08-27 15:26:24.067: E/AndroidRuntime(26165): FATAL EXCEPTION: main
08-27 15:26:24.067: E/AndroidRuntime(26165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finalproject3/com.example.finalproject3.gridview}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
08-27 15:26:24.067: E/AndroidRuntime(26165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
08-27 15:26:24.067: E/AndroidRuntime(26165): at dalvik.system.NativeStart.main(Native Method)
08-27 15:26:24.067: E/AndroidRuntime(26165): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.widget.AdapterView.addView(AdapterView.java:477)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-27 15:26:24.067: E/AndroidRuntime(26165): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:365)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.app.Activity.setContentView(Activity.java:1912)
08-27 15:26:24.067: E/AndroidRuntime(26165): at com.example.finalproject3.gridview.onCreate(gridview.java:24)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.app.Activity.performCreate(Activity.java:5066)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-27 15:26:24.067: E/AndroidRuntime(26165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
08-27 15:26:24.067: E/AndroidRuntime(26165): ... 11 more