Android studio 简单适配器 simpleadapter

文件结构

Android studio 简单适配器 simpleadapter_第1张图片


主视图:

Android studio 简单适配器 simpleadapter_第2张图片

相关代码:

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:

Android studio 简单适配器 simpleadapter_第3张图片

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;
            }
        });

    }
}

运行效果图:

Android studio 简单适配器 simpleadapter_第4张图片



你可能感兴趣的:(Android,studio)