各省備案網(wǎng)站怎么用模板做網(wǎng)站
第四次作業(yè)-寶寶相冊
題目
用Listview建立寶寶相冊,相冊內(nèi)容及圖片可自行設(shè)定,也可在資料文件中獲取。給出模擬器仿真界面及代碼截圖。 (參考例4-8)
創(chuàng)建工程項目
創(chuàng)建名為baby
的項目工程,最后的工程目錄結(jié)構(gòu)如下圖所示:

res/drawable文件中的i1、i2、i3、i4、i5、i6均為圖片,即寶寶相冊圖片,網(wǎng)上自行選取照片即可。
res/layout為文件布局文件,activity_main.xml
為自動生成的自定義布局文件,list_item.xml
為自定義布局文件
布局文件
-
創(chuàng)建自定義布局文件
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="horizontal"><ImageViewandroid:id="@+id/news_thumb"android:layout_width="100dp"android:layout_height="100dp"android:layout_margin="5dp"/><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"><TextViewandroid:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="16sp" /><TextViewandroid:id="@+id/news_info"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="14sp"android:layout_marginTop="5dp"/></LinearLayout></LinearLayout>
-
修改MainActivity.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingLeft="16dp"android:paddingRight="16dp"><ListViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>
MainActivity文件
package com.example.baby;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;public class MainActivity extends AppCompatActivity {private ListView listView;private SimpleAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 假設(shè)有一個包含數(shù)據(jù)的ListList<Map<String, String>> data = new ArrayList<>();Map<String, String> item1 = new HashMap<>();item1.put("news_thumb", String.valueOf(R.drawable.i1)); //R.drawable.i1引用照片資源文件item1.put("news_title", "氈帽系列");item1.put("news_info", "此系列服裝有點cute,像不像小車夫。");data.add(item1);Map<String, String> item2 = new HashMap<>();item2.put("news_thumb", String.valueOf(R.drawable.i2)); //R.drawable.i2引用照片資源文件item2.put("news_title", "蝸牛系列");item2.put("news_info", "寶寶變成了小蝸牛,爬啊爬啊爬啊。");data.add(item2);Map<String, String> item3 = new HashMap<>();item3.put("news_thumb", String.valueOf(R.drawable.i3));item3.put("news_title", "小蜜蜂系列");item3.put("news_info", "小蜜蜂,嗡嗡嗡,飛到西,飛到東。");data.add(item3);Map<String, String> item4 = new HashMap<>();item4.put("news_thumb", String.valueOf(R.drawable.i4));item4.put("news_title", "氈帽系列");item4.put("news_info", "此系列服裝有點cute,像不像小車夫。");data.add(item4);Map<String, String> item5 = new HashMap<>();item5.put("news_thumb", String.valueOf(R.drawable.i5));item5.put("news_title", "蝸牛系列");item5.put("news_info", "寶寶變成了小蝸牛,爬啊爬啊爬啊。");data.add(item5);Map<String, String> item6 = new HashMap<>();item6.put("news_thumb", String.valueOf(R.drawable.i6));item6.put("news_title", "小蜜蜂系列");item6.put("news_info", "小蜜蜂,嗡嗡嗡,飛到西,飛到東。");data.add(item6);// 定義數(shù)據(jù)的鍵與布局文件中組件的映射String[] from = {"news_thumb", "news_title", "news_info"};int[] to = {R.id.news_thumb, R.id.news_title, R.id.news_info};// 創(chuàng)建SimpleAdapteradapter = new SimpleAdapter(this, data, R.layout.list_item, from, to);// 關(guān)聯(lián)SimpleAdapter與ListViewlistView = findViewById(R.id.list);listView.setAdapter(adapter);// 為ListView添加一個項目點擊監(jiān)聽器,當(dāng)點擊項目時顯示對話框listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {// 獲取點擊項目的數(shù)據(jù)Map<String, String> itemData = (Map<String, String>) parent.getItemAtPosition(position);// 從點擊項目的數(shù)據(jù)中提取文本信息以供對話框使用String title = itemData.get("news_title");String info = itemData.get("news_info");// 創(chuàng)建并顯示一個自定義對話框AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setTitle(title).setMessage(info).setPositiveButton("確定", null); // 沒有操作的確定按鈕AlertDialog dialog = builder.create();dialog.show();}});}
}
修改AndroidManifest.xml文件
<activityandroid:name=".MainActivity"android:exported="true"android:label="SimpleAdapterDemo"> <!--修改導(dǎo)航欄名稱--><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter>
</activity>