Contact.java
package com.example.testandroid;
public class Contact {
public String name;
public String phone;
public Boolean selected;
public Contact()
{
}
public Contact(String name,String phone){
this.name=name;
this.phone=phone;
this.selected=false;
}
}
ContactsAdapter.java
package com.example.testandroid;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class ContactsAdapter extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId,temp;
ArrayList<Contact> data;
public ContactsAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ContactHolder holder = null;
Contact contact = data.get(position);
final int pos=position;
temp=position;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactHolder();
holder.name = (TextView)row.findViewById(R.id.name);
holder.phone = (TextView)row.findViewById(R.id.mobile);
holder.chkBox = (CheckBox)row.findViewById(R.id.chk_sel);
holder.chkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
//Contact contact = (Contact) cb.getTag();
Toast.makeText(context,"Clicked on Checkbox: " + data.get(pos).name +" is " + cb.isChecked(),Toast.LENGTH_LONG).show();
data.get(pos).selected=cb.isChecked();
}
});
row.setTag(holder);
}
else
{
holder = (ContactHolder)row.getTag();
}
holder.name.setText(contact.name);
holder.phone.setText(contact.phone);
return row;
}
static class ContactHolder
{
TextView name;
TextView phone;
CheckBox chkBox;
}
}
MainActivity.java
package com.example.testandroid;
import java.util.ArrayList;
import java.util.ListIterator;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ArrayList<Contact> mContacts;
ListView listView1;
ContactsAdapter adapter;
String mAction;
ListIterator<Contact> litr ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContacts=new ArrayList<Contact>();
for(int i=0;i<10;i++)
{
Contact c=new Contact("name"+i,"9748544"+i);
mContacts.add(c);
}
adapter = new ContactsAdapter(this,R.layout.container, mContacts);
listView1 = (ListView)findViewById(R.id.lisstview);
listView1.setAdapter(adapter);
}
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
public void add_edit_dialog(String title,String action){
mAction=action;
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.add_edit_dialog, null);
Dialog d= new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setView(textEntryView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String name,phoneno;
EditText et_name,et_phone;
et_name=(EditText)textEntryView.findViewById(R.id.name_edit);
et_phone=(EditText)textEntryView.findViewById(R.id.mobile_edit);
name=et_name.getText().toString();
phoneno=et_phone.getText().toString();
if(mAction.equals("add"))
{
Contact c=new Contact(name,phoneno);
mContacts.add(c);
listView1.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else if(mAction.equals("edit"))
{
Toast.makeText(getApplicationContext(), "mae="+name, Toast.LENGTH_LONG).show();
litr = mContacts.listIterator();
while (litr.hasNext()) {
Contact element = litr.next();
if(element.selected==true)
{
element.name=name;
element.phone=phoneno;
}
Log.e("test","name is "+element.name);
}
listView1.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
/* User clicked OK so do some stuff */
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
d.show();
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_add:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainActivity.this, "Add is Selected", Toast.LENGTH_SHORT).show();
add_edit_dialog("Add Contact","add");
return true;
case R.id.menu_edit:
Toast.makeText(MainActivity.this, "Edit is Selected", Toast.LENGTH_SHORT).show();
add_edit_dialog("Edit Contact","edit");
return true;
case R.id.menu_del:
Toast.makeText(MainActivity.this, "del is Selected", Toast.LENGTH_SHORT).show();
litr = mContacts.listIterator();
while (litr.hasNext()) {
Contact element = litr.next();
if(element.selected==true)
{
litr.remove();
}
}
/* for(int i=0;i<mContacts.size();i++)
{
if(mContacts.get(i).selected==true)
{
mContacts.remove(i);
}
}*/
listView1.setAdapter(adapter);
adapter.notifyDataSetChanged();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
package com.example.testandroid;
public class Contact {
public String name;
public String phone;
public Boolean selected;
public Contact()
{
}
public Contact(String name,String phone){
this.name=name;
this.phone=phone;
this.selected=false;
}
}
ContactsAdapter.java
package com.example.testandroid;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class ContactsAdapter extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId,temp;
ArrayList<Contact> data;
public ContactsAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ContactHolder holder = null;
Contact contact = data.get(position);
final int pos=position;
temp=position;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactHolder();
holder.name = (TextView)row.findViewById(R.id.name);
holder.phone = (TextView)row.findViewById(R.id.mobile);
holder.chkBox = (CheckBox)row.findViewById(R.id.chk_sel);
holder.chkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
//Contact contact = (Contact) cb.getTag();
Toast.makeText(context,"Clicked on Checkbox: " + data.get(pos).name +" is " + cb.isChecked(),Toast.LENGTH_LONG).show();
data.get(pos).selected=cb.isChecked();
}
});
row.setTag(holder);
}
else
{
holder = (ContactHolder)row.getTag();
}
holder.name.setText(contact.name);
holder.phone.setText(contact.phone);
return row;
}
static class ContactHolder
{
TextView name;
TextView phone;
CheckBox chkBox;
}
}
MainActivity.java
package com.example.testandroid;
import java.util.ArrayList;
import java.util.ListIterator;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ArrayList<Contact> mContacts;
ListView listView1;
ContactsAdapter adapter;
String mAction;
ListIterator<Contact> litr ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContacts=new ArrayList<Contact>();
for(int i=0;i<10;i++)
{
Contact c=new Contact("name"+i,"9748544"+i);
mContacts.add(c);
}
adapter = new ContactsAdapter(this,R.layout.container, mContacts);
listView1 = (ListView)findViewById(R.id.lisstview);
listView1.setAdapter(adapter);
}
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
public void add_edit_dialog(String title,String action){
mAction=action;
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.add_edit_dialog, null);
Dialog d= new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setView(textEntryView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String name,phoneno;
EditText et_name,et_phone;
et_name=(EditText)textEntryView.findViewById(R.id.name_edit);
et_phone=(EditText)textEntryView.findViewById(R.id.mobile_edit);
name=et_name.getText().toString();
phoneno=et_phone.getText().toString();
if(mAction.equals("add"))
{
Contact c=new Contact(name,phoneno);
mContacts.add(c);
listView1.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else if(mAction.equals("edit"))
{
Toast.makeText(getApplicationContext(), "mae="+name, Toast.LENGTH_LONG).show();
litr = mContacts.listIterator();
while (litr.hasNext()) {
Contact element = litr.next();
if(element.selected==true)
{
element.name=name;
element.phone=phoneno;
}
Log.e("test","name is "+element.name);
}
listView1.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
/* User clicked OK so do some stuff */
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
d.show();
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_add:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainActivity.this, "Add is Selected", Toast.LENGTH_SHORT).show();
add_edit_dialog("Add Contact","add");
return true;
case R.id.menu_edit:
Toast.makeText(MainActivity.this, "Edit is Selected", Toast.LENGTH_SHORT).show();
add_edit_dialog("Edit Contact","edit");
return true;
case R.id.menu_del:
Toast.makeText(MainActivity.this, "del is Selected", Toast.LENGTH_SHORT).show();
litr = mContacts.listIterator();
while (litr.hasNext()) {
Contact element = litr.next();
if(element.selected==true)
{
litr.remove();
}
}
/* for(int i=0;i<mContacts.size();i++)
{
if(mContacts.get(i).selected==true)
{
mContacts.remove(i);
}
}*/
listView1.setAdapter(adapter);
adapter.notifyDataSetChanged();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lisstview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
add_edit_dialog.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="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="Name"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/name_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/mobile_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="Mobile no"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/mobile_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:password="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
container.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:weightSum="4"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="3">
<TextView
android:id="@+id/name"
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:paddingLeft="10dp"
android:id="@+id/mobile"
android:text="Mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:gravity="center"
android:layout_weight="1">
<CheckBox
android:id="@+id/chk_sel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_add"
android:title="Add" />
<item android:id="@+id/menu_del"
android:title="Delete" />
<item android:id="@+id/menu_edit"
android:title="Edit" />
</menu>
No comments:
Post a Comment