Customized ListView with TextView, TextView, RadioGroup in Android

放肆的年华 提交于 2019-12-02 00:53:31

This is a sample example of custom adapter for listview. Modify it as your need: XML layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Header -->

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black" >

        <TextView
            android:id="@+id/item2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:height="30dip"
            android:text="Latest News - Your Healing Place"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF"
            android:width="100dip" />

        <TextView
            android:id="@+id/item1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:height="30dip"
            android:width="20dip" />

    </LinearLayout>

    <View android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="?android:attr/listDivider" />

    <LinearLayout android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">

        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/black" >

        </ListView>

    </LinearLayout>
</LinearLayout>

news.java :

List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String[] from = new String[] {"details", "date"};
int[] to = new int[] { R.id.item1, R.id.item2};
ListView lv= (ListView)findViewById(R.id.listview);
HashMap<String, String> map = new HashMap<String, String>();
map.put("details", "This is a sample message");
map.put("date", "24-07-2003");
fillMaps.add(map);

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);

You can use a class inheriting from ArrayAdapter, and overriding its getView() method.

public class StudentAdapter extends ArrayAdapter<Student> {

protected LayoutInflater inflater;

    public StudentAdapter(final Context context) {
        super(context, 0);
        inflater = (LayoutInflater) ((Context) context)
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

        // Note: You should optimize here with re-using convertView

        View rowView = inflater.inflate(R.layout.user_address_row_layout,
                parent, false);
        TextView sNo = (TextView) rowView.findViewById(R.id.sNo);
        sNo.setText(getItem(position).number);
            // same for every field of the row
            // ...

        return rowView;
    }

 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!