Sunday, 17 February 2013

Notifies list immediately when an item is removed or added and updated list is shown(notifydatasetchanged)

Phone.java
package com.example.dynamiclistupdation;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class Phone extends Activity {
      final List<Phonebook> listOfPhonebook = new ArrayList<Phonebook>();
    /** Called when the activity is first created. */
       Phonebook pbClass = new Phonebook();
       final PhonebookAdapter adapter = new PhonebookAdapter(this, listOfPhonebook);
    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ListView list = (ListView) findViewById(R.id.ListView01);
      
        Button bt1 = (Button) findViewById(R.id.button1);
      

        listOfPhonebook.add(new Phonebook("Test", "9981728", "test@test.com"));

        listOfPhonebook.add(new Phonebook("Test1", "1234455", "test1@test.com"));

        listOfPhonebook.add(new Phonebook("Test2", "00000", "test2@test.com"));

    
        bt1.setOnClickListener(new OnClickListener() {
          
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
          
                 
                    Intent my_intent = new Intent(Phone.this, PhoneBookAdd.class);
                  //startActivity(my_intent);
                  
                    startActivityForResult(my_intent, 0);
                     adapter.notifyDataSetChanged();
              
            }
        });
      
     

        list.setClickable(true);

    

      

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {

                System.out.println("sadsfsf");

                showToast(listOfPhonebook.get(position).getName());

            }

        });

        list.setAdapter(adapter);
      
 }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                    // TODO Auto-generated method stub
      
                    super.onActivityResult(requestCode, resultCode, data);
                    if(data.getExtras().containsKey("mobile") && (data.getExtras().containsKey("email"))){
                        pbClass.setMail(data.getStringExtra("email"));
                        pbClass.setPhone(data.getStringExtra("mobile"));
                        listOfPhonebook.add(new Phonebook("Test8", pbClass.getPhone(), pbClass.getMail()));
                        adapter.notifyDataSetChanged();
                      
            
                    }
                  
                
                  
                }
  
    private void showToast(String message) {

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

    }

}



Phonebook.java
package com.example.dynamiclistupdation;

public class Phonebook {

    private String name;

    private String phone;

    private String mail;

 

    // Constructor for the Phonebook class

    public Phonebook(String name, String phone, String mail) {

            super();

            this.name = name;

            this.phone = phone;

            this.mail = mail;

    }

 

    // Getter and setter methods for all the fields.

    // Though you would not be using the setters for this example,

    // it might be useful later.

    public Phonebook() {
        // TODO Auto-generated constructor stub
    }



    public String getName() {

            return name;

    }

    public void setName(String name) {

            this.name = name;

    }

    public String getPhone() {

            return phone;

    }

    public void setPhone(String phone) {

            this.phone = phone;

    }

    public String getMail() {

            return mail;

    }

    public void setMail(String mail) {

            this.mail = mail;

    }

}


package com.example.dynamiclistupdation;

import java.util.List;


import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class PhonebookAdapter extends BaseAdapter implements OnClickListener {

    private Context context;

    private List<Phonebook> listPhonebook;

    public PhonebookAdapter(Context context, List<Phonebook> listPhonebook) {
      

        this.context = context;

        this.listPhonebook = listPhonebook;

    }

    public int getCount() {

        return listPhonebook.size();

    }

    public Object getItem(int position) {

        return listPhonebook.get(position);

    }

    public long getItemId(int position) {

        return position;

    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
      

        Phonebook entry = listPhonebook.get(position);

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) context

                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.phone_row, null);

        }

        TextView tvContact = (TextView) convertView.findViewById(R.id.tvContact);

        tvContact.setText(entry.getName());

        TextView tvPhone = (TextView) convertView.findViewById(R.id.tvMobile);

        tvPhone.setText(entry.getPhone());

        TextView tvMail = (TextView) convertView.findViewById(R.id.tvMail);

        tvMail.setText(entry.getMail());

        // Set the onClick Listener on this button

        Button btnRemove = (Button) convertView.findViewById(R.id.btnRemove);
      
      

        btnRemove.setFocusableInTouchMode(false);

        btnRemove.setFocusable(false);

        btnRemove.setOnClickListener(this);
      
      

        // Set the entry, so that you can capture which item was clicked and

        // then remove it

        // As an alternative, you can use the id/position of the item to capture

        // the item

        // that was clicked.

        btnRemove.setTag(entry);
      

      //  btnAdd.setTag(entry);

        // btnRemove.setId(position);

     

        return convertView;

    }

    @Override

    public void onClick(View view) {

        Phonebook entry = (Phonebook) view.getTag();

        listPhonebook.remove(entry);

        // listPhonebook.remove(view.getId());

       notifyDataSetChanged();
    

    }

    private void showDialog(Phonebook entry) {

        // Create and show your dialog

        // Depending on the Dialogs button clicks delete it or do nothing

    }

}


import java.util.ArrayList;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PhoneBookAdd extends Activity {
    Phonebook addDetails = new Phonebook();
    final List<Phonebook> listOfPhonebook = new ArrayList<Phonebook>();
    PhonebookAdapter adapter;
    Phonebook pbClass = new Phonebook();

    /** Called when the activity is first created. */

    TextView mob,email;
    EditText mobedit , emailedit;
    @Override

    public void onCreate(Bundle savedInstanceState) {
      
        super.onCreate(savedInstanceState);

        setContentView(R.layout.add_layout);
        Button addbtn = (Button) findViewById(R.id.button1);
        mob = (TextView) findViewById(R.id.phone);
        email = (TextView) findViewById(R.id.mail);
      
        mobedit = (EditText) findViewById(R.id.phonenum);
      
        emailedit = (EditText) findViewById(R.id.mailaddress);
        addbtn.setOnClickListener(new OnClickListener() {
          
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
              
               
                showToast("add to list");
              
                Intent intent = getIntent();
              
              
              
              
               
               
              
                intent.putExtra("mobile", mobedit.getText().toString());
              
                intent.putExtra("email", emailedit.getText().toString());
                setResult(RESULT_OK, intent);
                pbClass.setMail(emailedit.getText().toString());
                pbClass.setPhone(mobedit.getText().toString());

                finish();
              
                               
              
              
              
              
              
              
              
                
              
                
              
            }
        });

     

    }
  
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
               
        super.onBackPressed();
    }

    private void showToast(String message) {

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

    }

}




main.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" >

    <ListView android:id="@+id/ListView01" android:layout_width="fill_parent"
                android:layout_height="wrap_content"></ListView>

  <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add an item" />


</LinearLayout>

phone_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10sp"
    android:paddingRight="10sp"
    android:paddingTop="2sp" >

    <TextView
        android:id="@+id/tvContact"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textSize="16sp"
        android:textStyle="bold"
        android:typeface="sans" >
    </TextView>

    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mobile  "
            android:textSize="13sp"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/tvMobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Number"
            android:textSize="13sp" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mail  "
            android:textSize="13sp"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/tvMail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Mail"
            android:textSize="13sp" >
        </TextView>

        <Button
            android:id="@+id/btnRemove"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:text="Remove" >
        </Button>
    </LinearLayout>

</LinearLayout>

add_layout.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:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mobile" />

    <EditText
        android:id="@+id/phonenum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/mail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <EditText
        android:id="@+id/mailaddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UpdateList" />

</LinearLayout>





Output
------------





Once we click remove button then list is updated immediately:

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...