Add and delete data by android imitating Listview

Keywords: Android xml Java encoding

Add and delete data by android imitating Listview

Recently, I want to implement the following figure. Some functions include clicking on minus sign to delete and clicking on blue font to add residence.

Analytical Writing

First of all, my idea is to use listview to write, then instantiate minus sign and EditText in item, click minus sign to delete this line, so that the last item of ListView can be changed to a custom layout, but when I use this method to write, a problem arises because I am in the Item click event. It's instantiated, so it's equivalent to double-clicking to react.
Simply, the response of the first click is intercepted by item, digested by item, and then the minus sign is instantiated, and then the minus sign has the click event. I estimated that I might need to modify the click event, but because my contemporaries do not understand this aspect very well, in line with the idea of simplification, suddenly came up with an idea.

First of all, the code has been packaged into a tool class, and the notes are written in detail.

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baibian.R;

import java.util.ArrayList;
import java.util.List;

/**
 * This is a layout to replace ListView, which has built-in methods.
 * Initialize data to get strings of all editview s
 */
public class LinearLayout_Inflaterable {
    private LayoutInflater layoutInflater;
    private Context context;
    private LinearLayout linearLayout;
    private View view;//Click on the view added
    private List<String> edit_string_list=null;//Text box string list
    private List<String> data;//Input data source
    private List<View> layoutlist;
    private int maxChildCount;
    public LinearLayout_Inflaterable(Context context, LinearLayout linearLayout, View view , List<String> data,int maxChildCount){
        this.context=context;
        this.linearLayout=linearLayout;
        this.view=view;
        this.data=data;
        this.maxChildCount=maxChildCount-1;
        layoutlist=new ArrayList<>();

        layoutInflater= LayoutInflater.from(context);
    }
    public void initlayout(){
        for (int i =0;i<data.size();i++){
            init_add_layout(data.get(i));
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add_layout();
            }
        });
    }
    //Get all edittext data
    public List<String> getEdit_String_List(){
        edit_string_list=new ArrayList<String>();
        for(int i=0;i<linearLayout.getChildCount();i++){
            EditText editText=(EditText) linearLayout.getChildAt(i).findViewById(R.id.edit_text);
            String string= editText.getText().toString();
            edit_string_list.add(string);
        }
        return edit_string_list;
    }
    //Additional layout with element input at initialization
    private void init_add_layout(String string ){
       final View textlayout=layoutInflater.inflate(R.layout.edit_information_listitem,null);
        layoutlist.add(textlayout);

        linearLayout.addView(textlayout);
        View remove_btn=(View) textlayout.findViewById(R.id.remove_btn);
         EditText textView=(EditText) textlayout.findViewById(R.id.edit_text);
        textView.setText(string);

        remove_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                linearLayout.removeView(textlayout);
            }});
    }
    //Use Add Layout when Clicking Add No Data Input
    private void add_layout(){
        if (linearLayout.getChildCount()<=maxChildCount){
            final View textlayout=layoutInflater.inflate(R.layout.edit_information_listitem, null);
            layoutlist.add(textlayout);
            linearLayout.addView(textlayout);
            View remove_btn=(View) textlayout.findViewById(R.id.remove_btn);
            EditText textView=(EditText) textlayout.findViewById(R.id.edit_text);
            remove_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    linearLayout.removeView(textlayout);
                }
            });
        }else {
            int realmaxChildCount =maxChildCount+1;
            Toast.makeText(context,context.getString(R.string.maxadd)+ realmaxChildCount  +context.getString(R.string.maxadd2),Toast.LENGTH_SHORT).show();
        }

    }
    //Get the number of linearlayout sublayouts
    public int getChildCount(){
        return linearLayout.getChildCount();
    }
}

Code used

 house_linearlayout_inflaterable=new LinearLayout_Inflaterable(this,house_LinearLayout,add_house_layout,data,10
        );
        house_linearlayout_inflaterable.initlayout();

xml file edit_information layout

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

    <ImageView
        android:clickable="true"
        android:id="@+id/remove_btn"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/red_minus"
        android:layout_width="20dp"
        android:layout_height="20dp" />
    <EditText
        android:singleLine="true"
        android:id="@+id/edit_text"
        android:maxLength="15"
        android:paddingTop="3dp"
        android:background="@null"
        android:textSize="15sp"
        android:hint="Please add residence"
        android:layout_width="wrap_content"
        android:layout_height="30dp" />
</LinearLayout>

Places to use

            <LinearLayout
                android:layout_marginLeft="3dp"
                android:layout_marginTop="3dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_gravity="center_vertical"
                    android:src="@mipmap/gray_house"
                    android:layout_width="20dp"
                    android:layout_height="20dp" />
                <TextView
                    android:textSize="15sp"
                    android:text="Place of residence"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/house_LinearLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </LinearLayout>
            <include
                android:id="@+id/add_house_layout"
                layout="@layout/edit_information_listitem_bottom"/>

edit_information_listitem_bottom

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="20dp"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_gravity="center_vertical"
        android:src="@mipmap/blue_add"
        android:layout_width="20dp"
        android:layout_height="20dp" />
    <TextView
        android:padding="3dp"
        android:textColor="@color/baibian_back_color"
        android:textSize="15sp"
        android:text="Adding Residence"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Solutions

Layout Inflater is used to fill the layout. When you click on the plus sign, you fill in a new layout. At the same time, you instantiate the minus sign. The minus sign response event is to delete the current item directly.

Novice entry works, limited ability, I hope God can give advice, thank you.

Posted by fahrvergnuugen on Wed, 10 Jul 2019 11:12:12 -0700