10.18考试

题目
在这里插入图片描述

  1. 数据自拟,完成学生列表的显示,对listview进行优化
  2. 编写json数据,将学生的所有信息存储在json中,使用gosn处理json
  3. 设置item的单击监听,同时弹出对应的窗口,见中间的效果图
  4. 点击“修改”无需做修改操作,直接让窗口消失
  5. 点击“删除”弹出对应的dialog,见最右侧效果图
  6. 当点击“确定”时,删除该条目,同时刷新列表

main

public class MainActivity extends BaseActivity {
ListView main_lv;
ItemAdapter adapter;
    List<Bean.StudentListBean> studentList=new ArrayList<>();
    String json;
    protected static final int CONTEXTMENU_DELETEITEM = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        json = getJson("info.txt", this);
        main_lv=findViewById(R.id.main_lv);
        adapter=new ItemAdapter(this,studentList);
        main_lv.setAdapter(adapter);

        Gson gson=new Gson();
        Bean stubean=gson.fromJson(json, Bean.class);
        List<Bean.StudentListBean> feedlist=stubean.getStudentList();
        studentList.addAll(feedlist);

        adapter.notifyDataSetChanged();
        //listview的长按弹出菜单
        main_lv.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.add(0, 0, 0, "删除");
                menu.add(0, 1, 1, "修改");
            }
        });

        }
    private void initListView() {
        /* Loads the items to the ListView. */
        //refreshFavListItems();
         /* / *将上下文菜单侦听器添加到ListView。* /*/
        main_lv.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {

            public void onCreateContextMenu(ContextMenu conMenu, View view , ContextMenu.ContextMenuInfo info) {
                conMenu.setHeaderTitle("ContextMenu");
                conMenu.add(0, 0, 0, "Delete this favorite!");
/* / *添加任意数量的上下文菜单选项。* /*/
                /* Add as many context-menu-options as you want to. */
            }
        });

        main_lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Toast.makeText(MainActivity.this, "111111111111", Toast.LENGTH_SHORT).show() ;
            }
        });
    }
    @Override
    public boolean onContextItemSelected(MenuItem aItem) {
        ContextMenu.ContextMenuInfo menuInfo = (ContextMenu.ContextMenuInfo) aItem.getMenuInfo();

        /* / *打开项目的ID,以获取用户选择的内容。* /*/
        switch (aItem.getItemId()) {
            //CONTEXTMENU_DELETEITEM     赋值为0  代表菜单中第一个被点击
            case CONTEXTMENU_DELETEITEM:
              /*   / *按位置将所选项目从适配器中取出。* /*/
                //Bean.StudentListBean favContexted = (Bean.StudentListBean) main_lv.getAdapter().getItem(0);
               /* / *从列表中将其删除。* /*/
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                // 设置提示框的标题
                builder.setTitle("提示标题").
                        // 设置要显示的信息
                                setMessage("文本的提示信息:真的要删除吗?").
                        // 设置确定按钮
                                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Bean.StudentListBean favContexted2 = (Bean.StudentListBean) main_lv.getAdapter().getItem(0);
                                studentList.remove(favContexted2);
                                adapter.notifyDataSetChanged();
                            }
                        }).show();

                //refreshFavListItems();
                return true; /* true means: "we handled the event". */
            //代表菜单中第2个被点击
            case 1:
                /*   / *按位置将所选项目从适配器中取出。* /*/
                Bean.StudentListBean favContexted2 = (Bean.StudentListBean) main_lv.getAdapter().getItem(0);
                /* / *从列表中将其删除。* /*/
                Toast.makeText(this,"111111",Toast.LENGTH_SHORT).show();
               /* studentList.remove(favContexted2);
                adapter.notifyDataSetChanged();*/
                //refreshFavListItems();
                return true; /* true means: "we handled the event". */
        }
        return false;
    }
    public static String getJson(String fileName, Context context) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


}

ItemAdapter

public class ItemAdapter extends BaseAdapter {
    Context context;
    List<Bean.StudentListBean> studentList;

    public ItemAdapter(Context context, List<Bean.StudentListBean> studentList) {
        this.context = context;
        this.studentList = studentList;
    }

    @Override
    public int getCount() {
        return studentList.size();
    }

    @Override
    public Object getItem(int position) {
        return studentList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_main_lv,null);
            holder=new ViewHolder();
            holder.item_name=convertView.findViewById(R.id.item_name);
            holder.item_banji=convertView.findViewById(R.id.item_banji);
            holder.item_class=convertView.findViewById(R.id.item_class);
            holder.item_sex=convertView.findViewById(R.id.item_sex);
            holder.item_image=convertView.findViewById(R.id.item_image);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        Bean.StudentListBean stuBean=studentList.get(position);
        Bean.StudentListBean dataBean=stuBean;
        holder.item_sex.setText(dataBean.getSex());
        holder.item_class.setText(dataBean.getClassroom());
        holder.item_banji.setText(dataBean.getGrade());
        holder.item_name.setText(dataBean.getName());
        return convertView;
    }
    class ViewHolder{
        ImageView item_image;
        TextView item_name,item_sex,item_class,item_banji;
    }
}

public class Bean {

**public class Bean {**

    private List<StudentListBean> studentList;

    public List<StudentListBean> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<StudentListBean> studentList) {
        this.studentList = studentList;
    }

    public static class StudentListBean {
        /**
         * id : 1
         * name : 张三
         * sex : 男
         * classroom : 1209C班
         * grade : 3210教室
         * icon : 1
         */

        private int id;
        private String name;
        private String sex;
        private String classroom;
        private String grade;
        private int icon;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public String getClassroom() {
            return classroom;
        }

        public void setClassroom(String classroom) {
            this.classroom = classroom;
        }

        public String getGrade() {
            return grade;
        }

        public void setGrade(String grade) {
            this.grade = grade;
        }

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:text="学生列表"
        android:background="#25A7EC"
        android:padding="10dp"
        android:gravity="center_horizontal"/>
<ListView
    android:id="@+id/main_lv"
    android:layout_width="match_parent"

    android:layout_height="match_parent"></ListView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:id="@+id/item_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@mipmap/ic_launcher"/>
    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="zhangsan"
        android:textSize="30sp"
        android:layout_marginLeft="7dp"
        android:layout_toRightOf="@id/item_image"
        />
    <TextView
        android:id="@+id/item_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男"
        android:textSize="30sp"
        android:layout_below="@id/item_name"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@id/item_image"
        />
    <TextView
        android:id="@+id/item_class"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="32109教室"
        android:textSize="30sp"
        android:layout_toRightOf="@id/item_name"
        android:layout_marginLeft="20dp"
        />
    <TextView
        android:id="@+id/item_banji"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1209Cban"
        android:layout_toRightOf="@id/item_sex"
        android:layout_below="@id/item_class"
        android:layout_marginTop="20dp"
        android:layout_alignLeft="@id/item_class"
        android:textSize="30sp"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_below="@id/item_sex"
        android:background="#000000"/>
</RelativeLayout>
{"studentList":[{"id":1,"name":"张三","sex":"男","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":2,"name":"李四","sex":"男","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":3,"name":"王五","sex":"女","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":4,"name":"赵六","sex":"男","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":5,"name":"蛋蛋","sex":"女","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":6,"name":"饭饭","sex":"女","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":7,"name":"呵呵","sex":"男","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":8,"name":"哈哈","sex":"女","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":9,"name":"嘻嘻","sex":"男","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":10,"name":"嘿嘿","sex":"男","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":11,"name":"andy","sex":"女","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":12,"name":"cindy","sex":"女","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":13,"name":"tom","sex":"男","classroom":"1209C班","grade":"3210教室","icon":1},
{"id":14,"name":"peter","sex":"男","classroom":"1209C班","grade":"3210教室","icon":1}]}

在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值