1.0 由于书上内容,已经和实际编程的兼容性已经不太友好,重写了项目,用于进一步学习列表控件ListView。
2.0 新建项目ListViewTest,其中文件目录如下:
3.0 ActivityCollector.java
package com.example.listviewtest; import android.app.Activity; import java.util.ArrayList; import java.util.List; public class ActivityCollector { public static List<Activity> activities = new ArrayList<>(); //添加活动 public static void addActivity(Activity activity){activities.add(activity);} //移除活动 public static void removeActivity(Activity activity){activities.remove(activity);} //销毁所有活动 public static void finishAll(){ for (Activity activity:activities){ if (!activity.isFinishing()){ activity.finish(); } } } }
4.0 BaseActivity.java
package com.example.listviewtest; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("BaseActivity:***",getClass().getSimpleName()); ActivityCollector.addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); } }
5.0 Province.java
package com.example.listviewtest; public class Province { private String name; public Province(String name){ this.name = name; } public String getName() { return name; } }
6.0 ProvinceAdapter.java
package com.example.listviewtest; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.List; public class ProvinceAdapter extends ArrayAdapter<Province> { private int resourceId; public ProvinceAdapter(Context context, int textViewResourceId, List<Province> objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { Province province = getItem(position);//获取当前的Province实例 View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false); TextView provinceName =(TextView) view.findViewById(R.id.province_name); provinceName.setText(province.getName()); return view; } }
7.0 activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context=".MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/guideline" /> <android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.05" /> </android.support.constraint.ConstraintLayout>
8.0 province_item.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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"> <TextView android:id="@+id/province_name" android:layout_width="match_parent" android:layout_height="32dp" android:textSize="18dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
9.0 MainActivity.java
package com.example.listviewtest; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends BaseActivity { private List<Province> provincelist = new ArrayList<>(); private String[] data = { "广西壮族自治区", "内蒙古自治区", "宁夏回族自治区", "西藏藏族自治区", "新疆维吾尔自治区", "香港特别行政区", "澳门特别行政区", "北京市", "天津市", "上海市", "重庆市", "吉林省", "辽宁省", "黑龙江省", "河北省", "河南省", "安徽省", "甘肃省", "山东省", "湖南省", "湖北省", "江苏省", "浙江省", "江西省", "云南省", "广西省", "贵州省", "海南省", "台湾省",}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initProvince(); ProvinceAdapter adapter = new ProvinceAdapter(MainActivity.this, R.layout.province_item, provincelist); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Province province = provincelist.get(position); Toast.makeText(MainActivity.this, province.getName(), Toast.LENGTH_SHORT).show(); } }); } public void initProvince() { for (String dataone : data) { Province province = new Province(dataone); provincelist.add(province); } // for (int i = 0;i<data.length;i++){ // Province province = new Province(data[i]); // provincelist.add(province); // } } }
10.0 运行效果如下:
原文地址:https://www.cnblogs.com/xiaofu007/p/10361635.html
时间: 2024-12-07 03:36:01