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

自己整理的android studio 自定义弹框获取和修改listview的数据

程序员文章站 2022-03-11 23:50:31
1.自定义编辑弹框update.xml

1.自定义编辑弹框update.xml

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

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

    <EditText
        android:id="@+id/viewName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电话号码" />

    <EditText
        android:id="@+id/viewPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

2.listview界面activity_controller.xml

<?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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hyld.controller">

    <TextView
        android:id="@+id/getName"
        android:layout_width="match_parent"
        android:layout_height="60dp"/>

    <Button
        android:id="@+id/up"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="修改"></Button>

    <TextView
        android:id="@+id/getNumber"
        android:layout_width="match_parent"
        android:layout_height="60dp"/>

    <TextView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="60dp"/>


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

</LinearLayout>

3.listview操作controller.class

package com.example.hyld;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class controller extends AppCompatActivity {

    TextView tv1;
    TextView tv2;
    Button btn1;
    ListView listView1;
    SimpleAdapter adapter;
    List<Map<String, String>> data = new ArrayList<>();
    int current = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_controller);
        listView1 = findViewById(R.id.listview);//在视图中找到ListView
        tv1 = findViewById(R.id.getName);
        tv2 = findViewById(R.id.getNumber);
        Intent intent=getIntent();
        Bundle bundle = intent.getExtras();
        String name = bundle.getString("name");
        tv1.setText(name);
        int number = bundle.getInt("number");
        tv2.setText(number+"");

        //2.准备显示的数据

        Map<String, String> map1 = new HashMap<>();
        map1.put("name", "数据1");
        map1.put("phone", "1111111");

        Map<String, String> map2 = new HashMap<>();
        map2.put("name", "数据2");
        map2.put("phone", "2222222");

        Map<String, String> map3 = new HashMap<>();
        map3.put("name", "数据3");
        map3.put("phone", "3333333");

        Map<String, String> map4 = new HashMap<>();
        map4.put("name", "数据4");
        map4.put("phone", "444444");

        data.add(map1);
        data.add(map2);
        data.add(map3);
        data.add(map4);
        adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"name", "phone"}, new int[]{R.id.tv_name, R.id.tv_phone}
        );
        listView1.setAdapter(adapter);
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i){
                    case 0:
                        Map<String, String> map = (Map<String, String>)adapterView.getItemAtPosition(i);
                        String name = map.get("name");
                        String phone = map.get("phone");
                        Toast.makeText(controller.this,"你点击了"+i+"按钮--"+name+"--"+phone, Toast.LENGTH_SHORT).show();
                        showDialog(name,phone,i);
                        break;//当我们点击某一项就能吐司我们点了哪一项

                    case 1:
                        map = (Map<String, String>)adapterView.getItemAtPosition(i);
                        name = map.get("name");
                        phone = map.get("phone");
                        Toast.makeText(controller.this,"你点击了"+i+"按钮--"+name+"--"+phone, Toast.LENGTH_SHORT).show();
                        showDialog(name,phone,i);
                        break;

                    case 2:
                        map = (Map<String, String>)adapterView.getItemAtPosition(i);
                        name = map.get("name");
                        phone = map.get("phone");
                        Toast.makeText(controller.this,"你点击了"+i+"按钮--"+name+"--"+phone, Toast.LENGTH_SHORT).show();
                        showDialog(name,phone,i);
                        break;

                    case 3:
                        map = (Map<String, String>)adapterView.getItemAtPosition(i);
                        name = map.get("name");
                        phone = map.get("phone");
                        Toast.makeText(controller.this,"你点击了"+i+"按钮--"+name+"--"+phone, Toast.LENGTH_SHORT).show();
                        showDialog(name,phone,i);
                        break;

                    case 4:
                        map = (Map<String, String>)adapterView.getItemAtPosition(i);
                        name = map.get("name");
                        phone = map.get("phone");
                        Toast.makeText(controller.this,"你点击了"+i+"按钮--"+name+"--"+phone, Toast.LENGTH_SHORT).show();
                        showDialog(name,phone,i);
                        break;
                }
            }
        });

        btn1 = findViewById(R.id.up);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> map1 = new HashMap<>();
                map1.put("name", "修改");
                map1.put("phone", "888888");
                data.set(2,map1);
                adapter.notifyDataSetChanged();
            }
        });

    }

    //自定义弹框修改
    public void showDialog(String name,String phone,int i){
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.update, null);
        final EditText viewName = (EditText) textEntryView.findViewById(R.id.viewName);
        final EditText viewPhone = (EditText)textEntryView.findViewById(R.id.viewPhone);
        AlertDialog.Builder ad1 = new AlertDialog.Builder(controller.this);
        ad1.setTitle("修改");
        ad1.setView(textEntryView);
        viewName.setText(name);
        viewPhone.setText(phone);
        current = i;
        ad1.setPositiveButton("是", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
                Toast.makeText(controller.this,current+"当前",Toast.LENGTH_LONG).show();
                Map<String, String> map1 = new HashMap<>();
                map1.put("name", viewName.getText()+"");
                map1.put("phone", viewPhone.getText()+"");
                data.set(current,map1);
                adapter.notifyDataSetChanged();
            }
        });
        ad1.setNegativeButton("否", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {

            }
        });
        ad1.show();// 显示对话框
    }
}

本文地址:https://blog.csdn.net/qq_35086941/article/details/107411031

相关标签: android studio