2015年10月9日 星期五

List View and Adapter

The theory of adapter and listview:

We have 3 Raw data

0.Mary
1.Peter
2.Tom

Adapter will get there raw data. It can use the raw data to create the view that display in the ListView. However it will not make immediately.

First , ListView ask the adpater how many ite have. Adapter told it three. When the ListView want to show the zero dta,it ask adapter to get the zero view. Than, Adapter make the zero view (Mary), than return to ListView. ListView want ot show the first view, it repeat the same proce.

normal use 0dp height
and the weight is 1
This can prevent to cover other


First create a view bu xml.e.g. list_itemforecast.xml


<?xml version="1.0" encoding="utf-8"?>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_forecast_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Todat-sunny-88/63"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"/>



Second,Create Array Adapter


aa = new ArrayAdapter<String>(getActivity(), R.layout.list_itemforecast, R.id.list_item_forecast_textview, weekForcast);


R.id.list_item_forecast_textview is a iteam in the list_itemforecast.xml to show the information


Third,give the adapter to the List View


ListView listview_forecast = (ListView) rootView.findViewById(R.id.listview_forecast);
        listview_forecast.setAdapter(aa);
        listview_forecast.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getActivity(),aa.getItem(position),Toast.LENGTH_LONG).show();}})
            ;