Android studio 简单适配器 simpleadapter

文件结构



主视图:


相关代码:

<?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">

    <ListVie
        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" />
</android.support.constraint.ConstraintLayout>

listview:


<?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"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp">

    <TextView
        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" />

    <TextView
        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" />
</android.support.constraint.ConstraintLayout>

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<HashMap<String,String>> arrayList = new ArrayList<>();
        HashMap<String,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;
            }
        });

    }
}

运行效果图:




猜你喜欢

转载自blog.csdn.net/haolangtaiye/article/details/79964446
今日推荐