• 十六、ListView


    一.、使用 setAdapter 方法进行 内容的填充

    二、 创建  MyAdapter 类继承 BaseAdapter ;对 BaseAdapter 的方法 getView 进行重写,进行信息的展示

    三、代码示例

    1.设置页面

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    

      

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TextView
            android:id="@+id/tv"
            android:textSize="30sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

      2.后台

    package com.example.myapplication;
    
    public class Bean {
        String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    

      

    package com.example.myapplication;
    
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private List<Bean> data = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            for(int i=0 ; i<100 ; i++){
                Bean bean=new Bean();
                bean.setName("乡学"+i);
                data.add(bean);
            }
            Context context=this;
            ListView listView=findViewById(R.id.lv);
            listView.setAdapter(new MyAdapter(data,context));
    
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Log.e("leo", "onItemClick: "+i );
    
                    //显示对话框
                    AlertDialog.Builder builder=new AlertDialog.Builder(context);
                    builder.setIcon(R.mipmap.ic_launcher)
                            .setTitle("标题")
                            .setMessage("文本提示信息:"+data.get(i).getName())
                            .create().show();
                }
            });
    
        }
    }
    

      

    package com.example.myapplication;
    
    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import org.w3c.dom.Text;
    
    import java.util.List;
    
    public class MyAdapter extends BaseAdapter {
    
        private List<Bean> data;
        private Context context;
    
        public MyAdapter(List<Bean> data2,Context context2){
            this.data=data2;
            this.context=context2;
        }
    
        @Override
        public int getCount() {
            return data.size();
        }
    
        @Override
        public Object getItem(int i) {
            return null;
        }
    
        @Override
        public long getItemId(int i) {
            return i;
        }
    
        /**
         * 返回item条目
         * */
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
    
            //当view等于null时进行创建
            if (view==null) {
                viewHolder=new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);
                viewHolder.textView = view.findViewById(R.id.tv);
                view.setTag(viewHolder);
            }
            else{
                viewHolder=(ViewHolder)view.getTag();
            }
    
            //显示信息打印并返回
            viewHolder.textView.setText(data.get(i).getName());
            Log.e("leo", "getView:打印 " + i);
            return view;
        }
    
        private  final  class  ViewHolder{
            TextView textView;
        }
    
    
    }
    

      

  • 相关阅读:
    Windows下使用WSRM限制MongoDB内存
    java基础知识总结
    zabbix3.4自定义监控
    Java中的static关键字
    N!阶乘
    Jxnu Group Programming Ladder Tournament 2017
    大数加减法
    Codeforces 755D(思维+线段树)
    最小生成树(模板)
    引力波(matplotlib绘制)
  • 原文地址:https://www.cnblogs.com/wangshunyun/p/16043768.html
Copyright © 2020-2023  润新知