Dynamic Add and delete instance code of data in Android ListView _android

Source: Internet
Author: User
Tags stub

Main.xml file:

Copy Code code as follows:

<?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= "Horizontal"

>

<linearlayout

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

android:orientation= "Vertical"

>

<listview

Android:id= "@+id/listview"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

/>

<button

Android:id= "@+id/add"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "Add"

/>

</LinearLayout>

</LinearLayout>


Listview_item.xml file:
Copy Code code as follows:

<?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= "Wrap_content"

android:orientation= "Horizontal"

Android:background= "#000000"

Android:padding= "20DP"

>

<edittext

Android:id= "@+id/edit"

Android:layout_width= "200DP"

android:layout_height= "Wrap_content"

/>

<button

Android:id= "@+id/del"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "Delete"

/>

</LinearLayout>

Mainactivity. java

Copy Code code as follows:

Package com.yyy.testandroid;


Import java.util.ArrayList;

Import android.app.Activity;

Import Android.content.Context;

Import Android.os.Bundle;

Import Android.view.LayoutInflater;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.view.View.OnFocusChangeListener;

Import Android.view.ViewGroup;

Import Android.widget.BaseAdapter;

Import Android.widget.Button;

Import Android.widget.EditText;

Import Android.widget.ListView;

Import Android.widget.TextView;

public class Testandroidactivity extends activity {

/** called the activity is a. */

Private Button Button,add;

private TextView text;

Private ListView ListView;

Public Myadapter adapter;

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

ListView = (ListView) Findviewbyid (R.id.listview);

Add = (Button) Findviewbyid (R.id.add);

adapter = new Myadapter (this);

Listview.setadapter (adapter);

Add.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View arg0) {

TODO auto-generated Method Stub

Adapter.arr.add ("");

Adapter.notifydatasetchanged ();

}

});

}

Private class Myadapter extends Baseadapter {

private context;

Private Layoutinflater Inflater;

Public arraylist<string> arr;

Public Myadapter {

Super ();

This.context = context;

Inflater = Layoutinflater.from (context);

arr = new arraylist<string> ();

for (int i=0;i<3;i++) {//listview initializes 3 subkeys

Arr.add ("");

}

}

@Override

public int GetCount () {

TODO auto-generated Method Stub

return Arr.size ();

}

@Override

Public Object getitem (int arg0) {

TODO auto-generated Method Stub

return arg0;

}

@Override

public long getitemid (int arg0) {

TODO auto-generated Method Stub

return arg0;

}

@Override

Public View GetView (final int position, view view, ViewGroup arg2) {

TODO auto-generated Method Stub

if (view = = null) {

View = Inflater.inflate (R.layout.list_item, NULL);

}

Final EditText edit = (edittext) View.findviewbyid (R.id.edit);

Edit.settext (Arr.get (position)); When refactoring adapter, it's not data-disordered.

Button del = (button) View.findviewbyid (R.id.del);

Edit.setonfocuschangelistener (New Onfocuschangelistener () {

@Override

public void Onfocuschange (View V, Boolean hasfocus) {

TODO auto-generated Method Stub

if (Arr.size () >0) {

Arr.set (position, Edit.gettext (). toString ());

}

}

});

Del.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View arg0) {

TODO auto-generated Method Stub

Deletes the contents of the EditText of the deleted item from the collection

Arr.remove (position);

Adapter.notifydatasetchanged ();

}

});

return view;

}

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.