package com.dream.myapplication; import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter; import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; public class MainActivity extends AppCompatActivity { private List<Map<String, Object>>datalist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ListView listView = (ListView)findViewById(R.id.list_view); // String[] arr_data = {"嘀嘀嘀","嘀嘀嘀","嘀嘀嘀","嘀嘀嘀","嘀嘀嘀","嘀嘀嘀","嘀嘀嘀"}; // ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arr_data); datalist = new ArrayList<Map<String, Object>>(); //ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>(); SimpleAdapter simpleAdapter = new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text}); // listView.setAdapter(arrayAdapter); listView.setAdapter(simpleAdapter); } private List<Map<String, Object>> getData(){ for (int i=0;i<20;i++){ Map<String, Object> map = new HashMap<String, Object>(); map.put("pic",R.drawable.dee); map.put("text","嘿嘿嘿"+i); datalist.add(map); } return datalist; }}
<?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/pic" android:layout_marginLeft="15dp" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/dee"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#009900" android:text="嘀嘀嘀"/> </LinearLayout>
<?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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:orientation="vertical" tools:context="com.dream.myapplication.MainActivity" tools:showIn="@layout/activity_main"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
时间: 2024-10-13 00:00:59