A sentimental robot

ListView 본문

Android Studio

ListView

GOD03219 2018. 1. 29. 16:45

// 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