일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- #binary
- html id
- #android activity
- relative path
- html youtube
- #다차원포인터
- #성적관리프로그램
- html object
- mac terminal command
- #2차원배열
- hyperledger transaction
- html code
- #JAVASCRIPT
- docker example
- #bubbleSort
- #CallByAddress
- html multimedia
- html charset
- 하이퍼레저패브릭
- #C++ 연산자함수오버로딩
- #C++ has~a
- 토큰경제
- border-box
- #자바상속#자바이즈어#is~a
- git flow
- html5 new tag
- html video
- #3차원배열
- html plug-in
- #1차원배열
Archives
- Today
- Total
A sentimental robot
ListView 본문
// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:background="#aaaaaa"
android:orientation="vertical"
tools:context="com.example.i310_54.myapplication01.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#ffd000"
/>
</LinearLayout>
// milk.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button 2 " />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
// MainActivity.java
class Milk{
String name;
int age;
public Milk(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class MilkAdapter extends ArrayAdapter<Milk>{
Context context;
int resource;
public MilkAdapter(@NonNull Context context, int resource, @NonNull List<Milk> objects) {
super(context, resource, objects);
this.context=context;
this.resource=resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(resource,null);
//여기서부터 코드 수정
Button button=convertView.findViewById(R.id.button2);
button.setText(getItem(position).getName());
TextView textView=convertView.findViewById(R.id.textView2);
textView.setText(getItem(position).getAge()+"");
//끝
return convertView;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinkedList<Milk>li=new LinkedList<Milk>();
li.add(new Milk("jo",23));
li.add(new Milk("SORA",23));
li.add(new Milk("hyeon",23));
li.add(new Milk("yum",22));
li.add(new Milk("jeong",28));
li.add(new Milk("min",23));
li.add(new Milk("papa",58));
MilkAdapter milkAdapter=new MilkAdapter(this,R.layout.milk,li);
ListView listView=findViewById(R.id.listView);
listView.setAdapter(milkAdapter);
}
}
'Android Studio' 카테고리의 다른 글
Activity (0) | 2018.01.31 |
---|---|
Activity transition/intent (0) | 2018.01.30 |
Spinner (0) | 2018.01.29 |
LinearLayout (0) | 2018.01.12 |
메소드를 공유하는 위젯 (0) | 2018.01.12 |