實驗三 ListView的使用

實驗三 UI設計(二)

實驗目的:

學習使用ListView

 

實驗要求:

1.  實現一個列表,其中顯示班級學號姓名,提供添加功能,如需要刪去某一項,長按該項,通過彈出菜單顯示刪除功能。



代碼:

MainActivity.java

package com.example.walker.exp3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private Button btn_add;
    private EditText et_name;
    private EditText et_class;
    private EditText et_numer;
    private ListView listview;
    private SimpleAdapter simpleAdapter;
    private List<Map<String, Object>> Datas = new ArrayList<Map<String, Object>>();
    //String number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //實例化數據集
        listview = (ListView) findViewById(R.id.listview);
        et_class = (EditText) findViewById(R.id.et_class);
        et_name = (EditText) findViewById(R.id.et_name);
        et_numer = (EditText) findViewById(R.id.et_number);
        btn_add = (Button) findViewById(R.id.btn_add);

        //爲 ListView 的所有 item 註冊 ContextMenu
//        this.registerForContextMenu(listview);
        //設置創建上下文菜單的監聽
        listview.setOnCreateContextMenuListener(this);
        ////設置按鈕的監聽

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_class.getText().toString().trim().equals("") || et_numer.getText().toString().trim().equals("") || et_name.getText().toString().trim().equals("")) {
                    Toast.makeText(MainActivity.this, "請輸入正確數據", Toast.LENGTH_SHORT).show();
                } else {
                    Map<String, Object> item = new HashMap<String, Object>();
                    item.put("class", et_class.getText().toString());
                    item.put("number", et_numer.getText().toString());
                    item.put("name", et_name.getText().toString());
                    Datas.add(item);
                    simpleAdapter.notifyDataSetChanged();
                    Toast.makeText(MainActivity.this, "添加數據成功", Toast.LENGTH_SHORT).show();

                }
            }
        });
        //map對象中的key的數組, 用於得到對應的value
        String[] from = {"class", "number", "name"};
        //Item佈局文件中的子view的id的數組
        int[] to = {R.id.tv_class, R.id.tv_number, R.id.tv_name};
        // /設置適配器
        simpleAdapter = new SimpleAdapter(this, Datas, R.layout.item_view, from, to);
        listview.setAdapter(simpleAdapter);

    }

    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //添加菜單項
        menu.add(0, 1, 0, "刪除");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int pos = (int) listview.getAdapter().getItemId(menuInfo.position);
        Datas.remove(pos);
//        if (Datas.remove(pos) != null) {
//            System.out.println("success");
//        } else {
//            System.out.println("failed");
//        }
        simpleAdapter.notifyDataSetChanged();
        Toast.makeText(getBaseContext(), "刪除此項", Toast.LENGTH_SHORT).show();
        return super.onContextItemSelected(item);
    }

}

activity_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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@color/layout">


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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="班級:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/et_class"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:inputType="text"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="學號:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/et_number"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:inputType="number"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/et_name"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:textSize="20sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

       <Button
           android:id="@+id/btn_add"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/app_button"
           android:textSize="20sp"
           android:background="@color/app_bk"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="班級"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="學號"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="姓名"
            android:textSize="20sp"/>

    </LinearLayout>

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

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

    </LinearLayout>

</LinearLayout>

item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_class"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tv_number"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp"/>
</LinearLayout>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章