listview列表视图的使用

无敌的宇宙
无敌的宇宙
擅长邻域:Java,HTML,JavaScript,MySQL,支付,退款,图片上传

分类: Android 标签: listview的使用

2022-08-22 11:32:19 553浏览

listview的使用

1.使用适配器

image.png


package s.jf3q.listview2;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Map<String,Object>> li=new ArrayList<Map<String,Object>>();

        Map<String,Object> m=new HashMap<String,Object>();
        m.put("nickname","六三");
        m.put("faceimg",R.drawable.img1);
        li.add(m);

        Map<String,Object> m2=new HashMap<String,Object>();
        m2.put("nickname","六三2");
        m2.put("faceimg",R.drawable.img2);
        li.add(m2);


        Map<String,Object> m3=new HashMap<String,Object>();
        m3.put("nickname","六三3");
        m3.put("faceimg",R.drawable.img3);
        li.add(m3);

        Map<String,Object> m4=new HashMap<String,Object>();
        m4.put("nickname","六三4");
        m4.put("faceimg",R.drawable.img4);
        li.add(m4);

        Map<String,Object> m5=new HashMap<String,Object>();
        m5.put("nickname","六三5");
        m5.put("faceimg",R.drawable.img5);
        li.add(m5);

        Map<String,Object> m6=new HashMap<String,Object>();
        m6.put("nickname","六三6");
        m6.put("faceimg",R.drawable.img6);
        li.add(m6);

        ListView lv=findViewById(R.id.lv2);
        SimpleAdapter sp=new SimpleAdapter(this,li,R.layout.list,new String []{"nickname","faceimg"},new int[]{R.id.nickname,R.id.faceimg});
        lv.setAdapter(sp);


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String,Object> mm=(Map<String,Object>)parent.getItemAtPosition(position);
                Toast.makeText(MainActivity.this,mm.get("nickname").toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <ListView
            android:id="@+id/lv2"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
    />

</LinearLayout>

list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">


    <ImageView
            android:id="@+id/faceimg"
            android:scaleType="centerCrop"
            android:layout_width="50dp"
            android:layout_height="50dp"/>
    <TextView
            android:id="@+id/nickname"
            android:padding="10dp"
            android:text="123"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

2.简单的数组和数组适配器两种方法

image.png

package s.jf3q.listview;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] tt=new String[]{"1","2","3"};

        ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,tt);

        ListView lv=findViewById(R.id.lv);
        lv.setAdapter(aa);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <ListView
            android:entries="@array/tt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    <ListView
            android:layout_alignParentBottom="true"
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</RelativeLayout>

array.xml放在values目录下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="tt">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
    </string-array>
</resources>

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695