美文网首页
简单适配器结合ListView控件的使用

简单适配器结合ListView控件的使用

作者: 昨天剩下的一杯冷茶 | 来源:发表于2018-10-30 15:12 被阅读1次

//布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hzx.mylistview1.MainActivity">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/pname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:textSize="15sp"
            android:layout_weight="1"
            android:text="产品名称"/>

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:textSize="15sp"
            android:layout_weight="1"
            android:text="产品价格"/>

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:textSize="15sp"
            android:layout_weight="1"
            android:text="产品产地"/>
    </LinearLayout>


    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

</LinearLayout>


//数据源

public class MyDataSource {
    public MyDataSource(){

    }

    public static List<Map<String,String>>getMaps(){
        List<Map<String,String>> listMaps = new ArrayList<Map<String,String>>();
        Map<String,String> map1 = new HashMap<String,String>();
        map1.put("pname","西瓜");
        map1.put("price","$2.30");
        map1.put("address","海南");


        Map<String,String> map2 = new HashMap<String,String>();
        map2.put("pname","香蕉");
        map2.put("price","$4.30");
        map2.put("address","广西");

        Map<String,String> map3 = new HashMap<String,String>();
        map3.put("pname","南瓜");
        map3.put("price","$1.30");
        map3.put("address","河北");


        listMaps.add(map1);
        listMaps.add(map2);
        listMaps.add(map3);
        return listMaps;
    }
}


//activity

public class MainActivity extends AppCompatActivity {

    private ListView listView;

    private SimpleAdapter adapter;

    private List<Map<String, String>> data = null;

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

        listView = (ListView) findViewById(R.id.listview);
        data = MyDataSource.getMaps();
        adapter = new SimpleAdapter(MainActivity.this, data, R.layout.activity_main, new String[]{"pname", "price", "address"}, new int[]{R.id.pname, R.id.price, R.id.address});

        listView.setAdapter(adapter);
    }
}


image.png

相关文章

网友评论

      本文标题:简单适配器结合ListView控件的使用

      本文链接:https://www.haomeiwen.com/subject/rcugtqtx.html