当前位置: 首页 > 工具软件 > PopMenu > 使用案例 >

综合实验三-----listView、popMenu

湛功
2023-12-01
package com.example.lenovo.testtwo;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MainActivity extends AppCompatActivity {

    private EditText ed;
    private EditText ed1;
    private EditText ed2;
    private ListView lv;
    private Button btn;
    List<shu> list=new ArrayList<>();
//   private  ArrayAdapter<String> arrayAdapter;
    private myAdpater arrayAdapter;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       initView();
    }
    public  void  initView()
    {
          ed=(EditText)findViewById(R.id.editText);
        ed1=(EditText)findViewById(R.id.editText1);
        ed2=(EditText)findViewById(R.id.editText2);
        lv=(ListView)findViewById(R.id.lv);
        btn=(Button)findViewById(R.id.button);
        arrayAdapter=new myAdpater();
//        arrayAdapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,list);
        lv.setAdapter(arrayAdapter);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shu s=new shu();
                s.name=ed.getText().toString();
                s.age=ed1.getText().toString();
                s.tel=ed2.getText().toString();
                list.add(s);
                arrayAdapter.setList(list);
            }
        });
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {//这里先是listview的--pop的时间
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
                //view 看清楚
                PopupMenu popupMenu=new PopupMenu(MainActivity.this,view);
                //需要创建菜单,将样式用在menu
                popupMenu.getMenuInflater().inflate(R.menu.menu1,popupMenu.getMenu());
                popupMenu.show();
                //当菜单出现点击事件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch (menuItem.getItemId())
                        {
                            case R.id.i1:
                                //记得这里如果不是 类的base则用 notify直接在此调用
                                list.remove(i);
                                arrayAdapter.setList(list);
                        }
                        return false;
                    }
                });
                return false;
            }
        });

    }
    class myAdpater extends BaseAdapter{

        List<shu> list=new ArrayList<>();
        //此是新的方法
        public void setList(List list)
        {
            this.list=list;
            notifyDataSetChanged();//看见没!!!!小心
        }
        public int getCount()
        {
            return list.size();
        }

        @Override
        public Object getItem(int i)
        {
            return list.get(i);
        }

        @Override
        public long getItemId(int i)
        {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
          Holder holder;
          if(view==null)
          {
              //相当于给一个布局给menu
              holder=new Holder();
              view= LayoutInflater.from(MainActivity.this).inflate(R.layout.list1,null);
              holder.n=view.findViewById(R.id.textView);
              holder.t=view.findViewById(R.id.textView1);
              holder.a=view.findViewById(R.id.textView2);
              view.setTag(holder);
          }
          else
          {
              holder=(Holder) view.getTag();
          }
          //向布局加东西
            holder.n.setText(list.get(i).name);
            holder.t.setText(list.get(i).tel);
            holder.a.setText(list.get(i).age);
            return view;
        }
    }
    class Holder{
        TextView n;
        TextView t;
        TextView a;
    }
    class shu {
      String name;
      String tel;
      String age;记得加布局!!--加一个menu布局、一个textview布局,notify记得写呀
    }

}

 类似资料: