안드로이드

 

 

class MainActivity

package org.androidtown.mylist;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    ListView listview;
    SingerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       listview=(ListView)findViewById(R.id.listview1);
       adapter=new SingerAdapter();
       listview.setAdapter(adapter);
    }


    class SingerAdapter extends BaseAdapter{
        String[] names ={"소녀시대", "걸스데이", "씨스타", "포미닛"};
        String[]  ages ={"21", "20", "23", "21"};
        int[]  imgs={R.drawable.a1, R.drawable.a2 , R.drawable.a3, R.drawable.a4};


        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          /*  TextView view =new TextView(getApplicationContext());
            view.setText(names[position]);
            view.setTextSize(50.0f);
            view.setTextColor(Color.BLACK);
            if(position%2==0){
                view.setBackgroundColor(Color.rgb(239,226,15 ) );
            }else{
                view.setBackgroundColor(Color.rgb(240,236,171) );
            }*/

            SingerItemView view =new SingerItemView(getApplicationContext());
            view.setName(names[position]);
            view.setAge(ages[position]);
            view.setImage(imgs[position]);
            return view;
        }
    }


}

 

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.androidtown.mylist.MainActivity">

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:id="@+id/listview1" />
</RelativeLayout>

 

class SingerItemView

package org.androidtown.mylist;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by choi on 2017-04-08.
 */

public class SingerItemView extends LinearLayout {

    TextView nameTextView, ageTextView;
    ImageView image;

    public SingerItemView(Context context) {
        super(context);
        init(context);
    }

    public SingerItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    private void init(Context context){
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        //메모리에 올린다
        inflater.inflate(R.layout.singer_itmes, this, true);

        image=(ImageView)findViewById(R.id.image1);
        nameTextView=(TextView)findViewById(R.id.nameTextView);
        ageTextView=(TextView)findViewById(R.id.ageTextView);

    }


    public void setName(String name){
        nameTextView.setText(name);
    }

    public void setAge(String age){
        ageTextView.setText(age);
    }

    public void setImage(int img){
        image.setImageResource(img);
    }



}

 

R.layout.singer_itmes

<?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"
    >

    <ImageView
        android:id="@+id/image1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/a1"
        android:layout_gravity="center_vertical"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        >

        <TextView
            android:id="@+id/nameTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="30dp"
            android:textColor="#ff000000"
            />

        <TextView
            android:id="@+id/ageTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="20dp"
            android:textColor="#128930"
            />



    </LinearLayout>




</LinearLayout>

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

용덕(龍德)을 갖춘 자로서 정당한 지위에 있는 자는 위대한 인물, 즉 군덕(君德)이 있는 사람을 만나는 것이 좋다. -역경

댓글 ( 4)

댓글 남기기

작성