文件结构



主视图:


相关代码:

<?xml version="1.0" encoding="utf-8"?>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">            android:id="@+id/listView"        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_marginBottom="8dp"        android:layout_marginEnd="8dp"        android:layout_marginLeft="8dp"        android:layout_marginRight="8dp"        android:layout_marginStart="8dp"        android:layout_marginTop="8dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />

listview:


<?xml version="1.0" encoding="utf-8"?>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:layout_marginBottom="5dp"    android:layout_marginTop="5dp">            android:id="@+id/textView_city"        android:layout_width="0dp"        android:layout_height="47dp"        android:layout_marginTop="10dp"        android:background="#40acff"        app:layout_constraintBottom_toTopOf="@+id/textView_province"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.0"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />            android:id="@+id/textView_province"        android:layout_width="0dp"        android:layout_height="21dp"        android:background="#faf24e"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.0"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/textView_city" />

java程序:

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ListView listView = findViewById(R.id.listView);        final ArrayList,String>> arrayList = new ArrayList<>();        HashMap,String> hashMap = new HashMap<>();        hashMap.put("city","chengdu");        hashMap.put("province","sichuan");        arrayList.add(hashMap);        hashMap = new HashMap<>();        hashMap.put("city","beijing");        hashMap.put("province","zhixia");        arrayList.add(hashMap);        hashMap = new HashMap<>();        hashMap.put("city","xian");        hashMap.put("province","shanxi");        arrayList.add(hashMap);        hashMap = new HashMap<>();        hashMap.put("city","hangzhou");        hashMap.put("province","zhejiang");        arrayList.add(hashMap);        final SimpleAdapter simpleAdapter = new SimpleAdapter(this,arrayList,R.layout.list,new String[]{"city","province"},new int[]{R.id.textView_city,R.id.textView_province});        listView.setAdapter(simpleAdapter);        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Toast.makeText(MainActivity.this,"you choice " + arrayList.get(position),Toast.LENGTH_SHORT).show();            }        });        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {            @Override            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {                arrayList.remove(position);                simpleAdapter.notifyDataSetChanged();                return true;            }        });    }}

运行效果图:




更多相关文章

  1. android 如何判断程序是否在前台运行
  2. Android(java.lang.NullPointerException)
  3. android include 使用
  4. Android(安卓)drawable 渐变色
  5. android上方显示进度的进度条
  6. 想抢先体验Android操作系统的魅力吗?那就使用Android(安卓)LiveCD
  7. 实现类似android:clipChildren="false"的效果,在使用android:clip
  8. Android应用程序框架层和系统运行库层日志系统源代码分析
  9. Android上运行QT项目Necessitas的基本原理

随机推荐

  1. Android RIL源码分析(2)
  2. Android 3D引擎之CatCake----编译hello_c
  3. Andriod 开发之微信分享接口
  4. Android中的Intent详解
  5. 【Android】Android背景选择器selector用
  6. Java/Android引用类型及其使用分析
  7. Android Activity切换动画overridePendin
  8. org.json.JSONException: End of input a
  9. Android之使用Pull解析Xml数据
  10. android短信服务