본문 바로가기

PROGRAMING/Android

[widget]android.widget.ListView [2] CustomArrayAdapter --getView

ArrayAdapter를 상속 받은 CustomArrayAdapter를 구현

getView 메소드를  Override하여 View를 임의로 구현함


activity_muisc_list.xml


    <ListView

        android:id="@+id/listView1"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        tools:listitem="@android:layout/simple_list_item_1" >

    </ListView>



custom_list_item.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="vertical" >

<TextView 

    android:id="@+id/text1"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:textAppearance="?android:attr/textAppearanceListItemSmall"

    android:gravity="center_vertical"

    android:paddingStart="?android:attr/listPreferredItemPaddingStart"

    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"

    android:minHeight="?android:attr/listPreferredItemHeightSmall"

/>

</LinearLayout>



Source


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_muisc_list);

ListView listViewTmp = (ListView)findViewById(R.id.listView1);

CustomArrayAdapter  customArrayAdapter = new CustomArrayAdapter(this);

customArrayAdapter.add("BB");

customArrayAdapter.add("CC");

customArrayAdapter.add("DD");

listViewTmp.setAdapter(customArrayAdapter);

}


Source 1


class CustomArrayAdapter extends ArrayAdapter<String>{

public CustomArrayAdapter(Context context) {

super(context,0);

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

LinearLayout view= (LinearLayout)getLayoutInflater().inflate(R.layout.custom_list_item, null);

TextView tv = (TextView)view.findViewById(R.id.text1);

String value = getItem(position);

tv.setText(value);

return view;

}

}