“ 매주 목요일마다 당신이 항상 하던대로 신발끈을 묶으면 신발이 폭발한다고 생각해보라.
컴퓨터를 사용할 때는 이런 일이 항상 일어나는데도 아무도 불평할 생각을 안 한다. ”- Jef Raskin
맥의 아버지 - 애플컴퓨터의 매킨토시 프로젝트를 주도
반응형
안드로이드스튜디오,모바일,앱개발,어플리케이션,아이폰,출시,신제품,Spinner,GridView,Source,초보
스피너와 그리드뷰 활용하기
과제 일시 : 2020 - 05 - 12
그리드뷰에 이미지를 넣어 앱을 만들어보았습니다. 아직 완벽한 모습은 아니지만 그리드뷰의 속성들에 대해서 알아보는 시간이 되었습니다. 그리고,
ImageAdapter에서 이미지 배열을 만들고 이미지의 속성들을 설정을 해보았습니다. getView나 getCount의 메소드를 다시 재정의했습니다.
스피너는 String.xml에 미리 정의를 하여, 사용 할 것을 준비를 했습니다. ArrayAdapter.createFromResource() 메소드를 이용하여 한다는 것을 배웠습니다.
이 글을 올리고 한번 소스를 더 보면서 이해해야 할 것 같습니다 :D
- GridView Test App Source
* activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
* MainActivity.java
package com.example.gridviewtest02;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
* ImageAdapter.java
package com.example.gridviewtest02;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mcontext;
ImageAdapter(Context c){
this.mcontext = c;
}
private int[] imageIds = {
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4
};
@Override
public int getCount() {
return imageIds.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv;
if(convertView == null){
iv = new ImageView(mcontext);
iv.setLayoutParams(new GridView.LayoutParams(85,85));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8,8,8,8);
} else {
iv = (ImageView)convertView;
}
iv.setImageResource(imageIds[position]);
return iv;
}
}
- Spinner APP Source
* MainActivity.java
package com.example.spinnertest02;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.spinnertest02.R;
public class MainActivity extends AppCompatActivity {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "선택된 행성은" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
* strings.xml
<resources>
<string name="app_name">SpinnerTest02</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!!</string>
<string name="planet_prompt">행성을 선택하시오.</string>
<string-array name="planets_array">
<item>수성</item>
<item>금성</item>
<item>지구</item>
<item>화성</item>
<item>목성</item>
<item>토성</item>
<item>천왕성</item>
<item>해왕성</item>
</string-array>
</resources>
*activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/planet_prompt" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/planet_prompt"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
- 실행화면 ( Program execution )