欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 综合练习Task3

程序员文章站 2022-03-11 22:52:42
Android 综合练习Task3题目要求编写一个程序,1个TextView(用于显示学号姓名)、1个ListView。ListView是风景点的地点名称和风景图片(适配器需要改写)。在所描述功能的基础上,对ListView增加上下文菜单(修改、删除、新增三个功能),修改功能支持对风景地点改名称和图片重选(弹出的对话框中有图文并茂的GridView),并且改完名称后,ListView的名称也会被修改;删除功能则对该风景删除,ListView也会相应更新;新增功能能在弹出的对话框中选择GridView显示...

Android 综合练习Task3

题目要求

编写一个程序,1个TextView(用于显示学号姓名)、1个ListView。ListView是风景点的地点名称和风景图片(适配器需要改写)。在所描述功能的基础上,对ListView增加上下文菜单(修改、删除、新增三个功能),修改功能支持对风景地点改名称和图片重选(弹出的对话框中有图文并茂的GridView),并且改完名称后,ListView的名称也会被修改;删除功能则对该风景删除,ListView也会相应更新;新增功能能在弹出的对话框中选择GridView显示的风景进行添加。选项菜单的功能则为:增加风景、重置两个功能。重置功能则将ListView中的风景列表恢复成程序启动时的状态。修改和新增功能弹出的对话框,第一行为EditText和ImageView,第二行为GridView,并有确定和取消按钮。
Android 综合练习Task3

分析

主界面只有一个TextView和一个Listview
其中Listview需要自己设置样式 为 img + textview
主界面需要同添加menu 含有2个功能
Alert的内容也需要设置样式 EditText + image + Gridview

主界面

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show my name and id" />

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

row_Listview

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/row_img"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/da_luo_shan" />

    <TextView
        android:id="@+id/row_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:textSize="20sp"
        android:text="大罗山" />
</LinearLayout>

menu

一个是右上角的menu,一个是点击ListView中内容出现的menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/detail_modify"
        android:title="修改" />
    <item
        android:id="@+id/detail_delete"
        android:title="删除" />
    <item
        android:id="@+id/detail_add"
        android:title="增加" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/main_reset"
        android:title="重置" />
    <item
        android:id="@+id/main_add"
        android:title="增加风景" />
</menu>

Alert内容

这个先放着

实现步骤

我们一步一步的来实现功能
根据我的思路,先实现以下ListView的布局,对前面的知识进行以简单的回顾

ListView内容实现

City.Class

package com.example.tty_task3_example;

public class City {
    private String name;
    private int img_id;

    public City(String name, int img_id) {
        this.name = name;
        this.img_id = img_id;
    }

    public String getName() {
        return name;
    }

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

    public int getImg_id() {
        return img_id;
    }

    public void setImg_id(int img_id) {
        this.img_id = img_id;
    }
}

CityAdapter.Class

package com.example.tty_task3_example;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class CityAdapter extends ArrayAdapter<City> {
    private Context context;
    private ArrayList<City> list;

    public CityAdapter(@NonNull Context context, ArrayList<City> list) {
        super(context, android.R.layout.simple_list_item_1, list);
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View v;
        if (convertView == null) {
            v = LayoutInflater.from(context).inflate(R.layout.row_lv, null, false);
        } else {
            v = convertView;
        }
        City city = list.get(position);
        ImageView img = v.findViewById(R.id.row_img);
        TextView tv = v.findViewById(R.id.row_tv);
        img.setImageResource(city.getImg_id());
        tv.setText(city.getName());
        return v;
    }
}

包含部分功能的MainActivity.Class

package com.example.tty_task3_example;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<City> list;
    ArrayAdapter<City> adapter;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_main);
        lv = findViewById(R.id.main_lv);
        list = new ArrayList<>();
        list = init_list();
        adapter = new CityAdapter(this, list);
        lv.setAdapter(adapter);
        registerForContextMenu(lv);
    }

    private ArrayList<City> init_list() {
        City city1 = new City("长城", R.drawable.great_wall);
        City city2 = new City("大罗山", R.drawable.da_luo_shan);
        list.add(city1);
        list.add(city2);
        return list;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.detai_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = menuInfo.position;
        switch (item.getItemId()) {
            case R.id.detail_add:
                break;
            case R.id.detail_delete:
                list.remove(position);
                adapter.notifyDataSetChanged();
                break;
            case R.id.detail_modify:
                break;
        }
        return super.onContextItemSelected(item);
    }
}

增加和修改的内容涉及到Alert页面,这个我打算后面再写,这里仅仅实现了删除的功能
初始界面我打算放如下两个Item,定义在init_list的方法中
Android 综合练习Task3
下面我打算添加一下右上角的菜单

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.main_add:
                break;
            case R.id.main_reset:
                list = init_list();
                adapter.notifyDataSetChanged();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

接下来就是Alert页面了

Alert页面

最上面一层是 EditText和Image
下面是GridView
其中GridView中的Item要自己定义

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/alert_et"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:maxLines="1"
            android:inputType="textPersonName"
            android:text="大罗山" />

        <ImageView
            android:id="@+id/alert_img"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/da_luo_shan" />
    </LinearLayout>

    <GridView
        android:id="@+id/alert_gv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:stretchMode="columnWidth" />
</LinearLayout>
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/row_gv_img"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/great_wall" />

    <TextView
        android:id="@+id/row_gv_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:text="长城" />
</LinearLayout>

为添加功能创造一个函数add_more
第一次写的时候忘记加bl.setView(v);了,找了半天才发现

    private void add_more() {
        gv_list = init_gv_list();
        AlertDialog.Builder bl = new AlertDialog.Builder(this);
        bl.setTitle("增加内容");
        View v = LayoutInflater.from(this).inflate(R.layout.alert_detail, null, false);
        bl.setView(v);
        final EditText et = v.findViewById(R.id.alert_et);
        final ImageView img = v.findViewById(R.id.alert_img);
        GridView gv = v.findViewById(R.id.alert_gv);
        final GridViewAdapter gv_adpter = new GridViewAdapter(this, gv_list);
        gv.setAdapter(gv_adpter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                City city = gv_adpter.getItem(position);
                et.setText(city.getName());
                img.setImageResource(city.getImg_id());
            }
        });
        bl.setPositiveButton("确认", null).setNegativeButton("取消", null).create().show();
    }

可以基本实现如下的效果
Android 综合练习Task3
可以看到,虽然出现了选择框,但是点击确认后没什么用,所以接下来要继续编写setPositiveButton的内容

String name = et.getText().toString();
list.add(new City(name, img_id[0]));
adapter.notifyDataSetChanged();

修改和新增的功能区别在与,点击修改的时候Editview里面要出现点击的那个城市的信息
而新增的时候需要有默认的名字和图片
Android 综合练习Task3

全代码

因为java学的不是很好,所以很多地方写的很粗糙,之后复习的时候会优化一下

package com.example.tty_task3_example;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<City> list, gv_list;
    ArrayAdapter<City> adapter;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_main);
        lv = findViewById(R.id.main_lv);
        gv_list = new ArrayList<>();
        list = new ArrayList<>();
        list = init_list();
        adapter = new CityAdapter(this, list);
        lv.setAdapter(adapter);
        registerForContextMenu(lv);
    }

    private ArrayList<City> init_gv_list() {
        gv_list.clear();
        City city1 = new City("西湖", R.drawable.west_lake);
        City city2 = new City("灵隐寺", R.drawable.ling_yin_temple);
        City city3 = new City("*", R.drawable.the_imperial_palace);
        City city4 = new City("南溪河", R.drawable.nan_xi_river);
        gv_list.add(city1);
        gv_list.add(city2);
        gv_list.add(city3);
        gv_list.add(city4);
        return gv_list;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.main_add:
//                增加功能
                add_more();
                break;
            case R.id.main_reset:
                list = init_list();
                adapter.notifyDataSetChanged();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private ArrayList<City> init_list() {
        list.clear();
        City city1 = new City("长城", R.drawable.great_wall);
        City city2 = new City("大罗山", R.drawable.da_luo_shan);
        list.add(city1);
        list.add(city2);
        return list;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.detai_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = menuInfo.position;
        switch (item.getItemId()) {
            case R.id.detail_add:
                add_more();
//                增加功能
                break;
            case R.id.detail_delete:
                list.remove(position);
                adapter.notifyDataSetChanged();
                break;
            case R.id.detail_modify:
                City city2 = list.get(position);
//                修改功能
                modify(city2);
                break;
        }
        return super.onContextItemSelected(item);
    }

    private void modify(City city){
        gv_list = init_gv_list();
        AlertDialog.Builder bl = new AlertDialog.Builder(this);
        bl.setTitle("增加内容");
        View v = LayoutInflater.from(this).inflate(R.layout.alert_detail, null, false);
        bl.setView(v);
        final EditText et = v.findViewById(R.id.alert_et);
        et.setText(city.getName());
        final ImageView img = v.findViewById(R.id.alert_img);
        img.setImageResource(city.getImg_id());
        GridView gv = v.findViewById(R.id.alert_gv);
        final GridViewAdapter gv_adpter = new GridViewAdapter(this, gv_list);
        final int[] img_id = new int[1];
        gv.setAdapter(gv_adpter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                City gv_city = gv_adpter.getItem(position);
                et.setText(gv_city.getName());
                img_id[0] = gv_city.getImg_id();
                img.setImageResource(gv_city.getImg_id());
            }
        });
        bl.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name = et.getText().toString();
                list.add(new City(name, img_id[0]));
                adapter.notifyDataSetChanged();
            }
        }).setNegativeButton("取消", null).create().show();
    }


    private void add_more() {
        gv_list = init_gv_list();
        AlertDialog.Builder bl = new AlertDialog.Builder(this);
        bl.setTitle("增加内容");
        View v = LayoutInflater.from(this).inflate(R.layout.alert_detail, null, false);
        bl.setView(v);
        final EditText et = v.findViewById(R.id.alert_et);
        et.setText("钱江");
        final ImageView img = v.findViewById(R.id.alert_img);
        img.setImageResource(R.drawable.qian_jiang_cbd);
        GridView gv = v.findViewById(R.id.alert_gv);
        final GridViewAdapter gv_adpter = new GridViewAdapter(this, gv_list);
        final int[] img_id = new int[1];
        img_id[0] = R.drawable.qian_jiang_cbd;
        gv.setAdapter(gv_adpter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                City gv_city = gv_adpter.getItem(position);
                et.setText(gv_city.getName());
                img_id[0] = gv_city.getImg_id();
                img.setImageResource(gv_city.getImg_id());
            }
        });
        bl.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name = et.getText().toString();
                list.add(new City(name, img_id[0]));
                adapter.notifyDataSetChanged();
            }
        }).setNegativeButton("取消", null).create().show();
    }
}

本文地址:https://blog.csdn.net/Weary_PJ/article/details/108728039