Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android
I fetch data from Database in listview and it display properly but i need to populate menu item to delete
item from listview and database by Longclicking on it and I searched a lot and tried much but not succeeded. I also want a search particular item from list. Following is the code I did till ....
Thanks in advance.....
 
Add_Customer.java
 
<pre>
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
import com.smartwallet.database.DbAdapterCust;
 
public class Add_Customer extends Activity implements OnClickListener
{
EditText et_cust,et_cno,et_add,et_mail;
Button bt_add;
private DbAdapterCust DBA;
private Long cid;
Cursor c;
//  private TelephonyManager tm;
private String imei="9898";
private String imei_id;
 
@Override
public void onCreate(Bundle b) 
{
    super.onCreate(b);
    setContentView(R.layout.addscustomer);
 
    cid=null;
    Bundle extras=getIntent().getExtras();
    if(extras!=null)
    {
        cid=Long.valueOf(extras.getLong("cust_id"));
    }
    else
        cid = null;
 
    DBA=new DbAdapterCust(this);
    DBA.open();
 
    et_cust=(EditText)findViewById(R.id.et_cust);
    et_cno=(EditText)findViewById(R.id.et_cno);
    et_add=(EditText)findViewById(R.id.et_add);
    et_mail=(EditText)findViewById(R.id.et_mail);
 
    bt_add=(Button)findViewById(R.id.bt_add);
    bt_add.setOnClickListener(this);
 
    //tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    //imei=tm.getSubscriberId();
    updateCust();
}
 
public void updateCust()
{
    if(cid!= null)
    {
        c = DBA.fetchCust_imei(cid);
        startManagingCursor(c);
        if(c.moveToFirst())
        {
            imei_id=c.getString(1);
            et_cust.setText(c.getString(2));
            et_add.setText(c.getString(4));
            et_cno.setText(c.getString(3));
            et_mail.setText(c.getString(5));
            bt_add.setText("Save");
        }
    }
}
public void savestate()
{
    DBA.updateCust1(cid.longValue(),Long.parseLong(imei_id), et_cust.getText().toString(),et_cno.getText().toString(),et_add.getText().toString(),et_mail.getText().toString());
}
 
public boolean validate() {
    if(et_add.getText().toString().equals(""))
        return false;
    if(et_cno.getText().toString().equals(""))
        return false;
    if(et_cust.getText().toString().equals(""))
        return false;
    if(et_mail.getText().toString().equals(""))
        return false;
 
    return true;
}
 
@Override
public void onClick(View v) 
{
    if(!validate()) {
        Toast.makeText(this, "Blank fields are not allowed", Toast.LENGTH_SHORT).show();
        return;
    }
 
    if(v.equals(bt_add))
    {
        if(cid==null)
        {
            long l=DBA.insertCust(0,et_cust.getText().toString(),
                    et_cno.getText().toString(),
                    et_add.getText().toString(),
                    et_mail.getText().toString());
 
            imei+=l;
 
            Log.d("L value :",""+l);
            Log.d("imei_value", imei);
            DBA.updateCust(l,Long.parseLong(imei), et_cust.getText().toString(),et_cno.getText().toString(),et_add.getText().toString(),et_mail.getText().toString());
            Toast.makeText(Add_Customer.this, "Item Added", 2000).show();
        }
        else
        {
            savestate();
            Toast.makeText(Add_Customer.this, "Item Updated", 2000).show();
        }
        DBA.close();
        finish();   
    }
}
@Override
protected void onDestroy() 
{
    DBA.close();
    super.onDestroy();
}
}

ListAdapter_Customer.java
 
import java.util.ArrayList;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
public class ListAdapter_Customer extends BaseAdapter
 
{
Context context;
private ArrayList&lt;string&gt; cnm ;
private ArrayList&lt;string&gt; id;
private ArrayList&lt;string&gt; add;
private ArrayList&lt;string&gt; cno;
private ArrayList&lt;string&gt; mail;
 
public ListAdapter_Customer(Context c,ArrayList&lt;string&gt; id,ArrayList&lt;string&gt; cnm,ArrayList&lt;string&gt; add,ArrayList&lt;string&gt; cno,ArrayList&lt;string&gt; mail)
{
    context = c;
    this.cnm = cnm;
    this.id = id;
    this.add = add;
    this.cno=cno;
    this.mail=mail;
}
 
public void clear()
{
        add.clear();
        id.clear();
        cnm.clear();
        mail.clear();
        cno.clear();
}
 
public void addRecord(String id1,String cnm1,String add1,String cno1)
{
    id.add(id1);
    cnm.add(cnm1);
    add.add(add1);
    cno.add(cno1);
}
 
@Override
public int getCount() 
{
    return id.size();
}
 
@Override
public Object getItem(int arg0) 
{
    return null;
}
 
@Override
public long getItemId(int arg0) 
{
    return Long.parseLong(id.get(arg0));
}
 
    @Override
public View getView(int position, View convertView, ViewGroup arg2) 
{
    if (convertView == null) 
    {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
        convertView = infalInflater.inflate(R.layout.custlist, arg2,false);
    }
 
    TextView tv1 = (TextView) convertView.findViewById(R.id.nm);
    //TextView tv2 = (TextView) convertView.findViewById(R.id.txt_add);
    //TextView tv3 = (TextView) convertView.findViewById(R.id.txt_cno);

     tv1.setText(cnm.get(position));
     //tv2.setText(add.get(position));
     //tv3.setText(cno.get(position));
     return convertView;
 
}
 
    public void remove(String string) {
        // TODO Auto-generated method stub

    }       
}

List_Customer.java
 
import java.util.ArrayList;
 
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.smartwallet.database.DbAdapterCust;
 
public class List_Customer extends Activity implements OnItemClickListener
 
{
int textlength=0;
ListView ls;
ArrayList&lt;string&gt; list=new ArrayList&lt;string&gt;();
ListAdapter_Customer adapter;
private DbAdapterCust DBA;
private static final int NWACTIVITY = 1;
private static final int EDIT_CUST =2;
Cursor c;
Intent i;
EditText inputSearch;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_search);
 
    ls = (ListView) findViewById(R.id.lsv);
    inputSearch=(EditText)findViewById(R.id.inputSearch);
 
    adapter = new ListAdapter_Customer(this,new ArrayList&lt;string&gt;(), new ArrayList&lt;string&gt;(), new ArrayList&lt;string&gt;(), new ArrayList&lt;string&gt;(),new ArrayList&lt;string&gt;());
 
    ls.setAdapter(adapter);
    ls.setTextFilterEnabled(true);
    ls.setOnItemClickListener(this);
 
    //registerForContextMenu(getListView());

    DBA = new DbAdapterCust(List_Customer.this);
    DBA.open();
    filldata();
 
   /* ls.setOnClickListener(new OnLongClickListener() {
 
        public boolean onLongClick(View arg0) {
            // TODO Auto-generated method stub
            remove(ls.indexOfChild(arg0));
            return true;
        }
    });*/
    ls.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView                    int arg2, long arg3) {
            Toast.makeText(getApplicationContext(), "Long Clicked : ", Toast.LENGTH_LONG).show();
            list.remove(arg2);
            adapter.notifyDataSetChanged();
            adapter.notifyDataSetInvalidated();
            return true;
        }
});
 
    /**
     * Enabling Search Filter
     * */
    inputSearch.addTextChangedListener(new TextWatcher() {
 
        /*@Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            ( List_Customer.this.nm).getFilter().filter(cs);
 
        }
 
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
 
        }
 
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
 
        }
    });*/
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text

        }
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });
}
 
protected void remove(int indexOfChild) {
    // TODO Auto-generated method stub

}
 
@Override
protected void onResume() 
{
    super.onResume();
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
// Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
}
 
@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch(item.getItemId())
    {
    case R.id.add_option:
        Toast.makeText(this, "Add Me selected", Toast.LENGTH_SHORT).show();
        Add_cust();
        return true;
 
    }
    return super.onOptionsItemSelected(item);
}
 
public void Add_cust()
{
    i = new Intent(this, Add_Customer.class);
    startActivityForResult(i, NWACTIVITY);
}
 
public void  filldata() 
{
    c = DBA.fetchAllCust();
    startManagingCursor(c);
    adapter.clear();
    if(c.moveToFirst())
    {
        do
        {
            Log.d("Details","Rec:"+ c.getString(0)+"   "+c.getString(1)+"  "+ c.getString(2)+"  "+ c.getString(3));
            adapter.addRecord(c.getString(1),c.getString(2), c.getString(4), c.getString(3));
        }while(c.moveToNext());
    }
    Toast.makeText(this, "Size: "+adapter.getCount(), Toast.LENGTH_LONG).show();
    adapter.notifyDataSetChanged();
}
 
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    filldata();
}
 
@Override
public void onItemClick(AdapterView {
        Intent i = new Intent(this, Add_Customer.class);
        i.putExtra("cust_id", arg3);
        startActivityForResult(i, EDIT_CUST);
}
 
}

DbAdapterCust.java
 
android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
 
public class DbAdapterCust 
{
// Database fields of Customer Table

public static final String CID = "cid";
public static final String IMEI_ID="imei_id";
public static final String NAME = "cname";
public static final String ADDRESS = "c_add";
public static final String CNO = "c_cno";
public static final String MAIL = "c_mail";
private static final String T_CUST = "Customer";
 
private Context context;
private SQLiteDatabase db;
private DbHelper dbHelper;
 
public DbAdapterCust(Context context) 
{
    this.context = context;
}
 
public DbAdapterCust open() throws SQLException 
{
    dbHelper = new DbHelper(context);
    db = dbHelper.getWritableDatabase();
    return this;
}
 
public void close() 
{
    dbHelper.close();
}
 
/**
 * Create a new todo If the todo is successfully created return the new
 * rowId for that note, otherwise return a -1 to indicate failure.
 */
 
//For Customer Table

public long insertCust(long imei_id,String cname, String c_cno,String c_add,String c_mail) 
{
    ContentValues values = createContentValues_cust(imei_id,cname,c_cno,c_add,c_mail);
    return db.insert(T_CUST, null, values);
}
 
/**
 * Update the todo
 */
public boolean updateCust(long rowId, long imei_id,String cname, String c_cno,String c_add,String c_mail) {
    ContentValues values = createContentValues_cust(imei_id,cname,c_cno,c_add,c_mail);
 
    return db.update(T_CUST, values, CID + "=" + rowId, null) &gt; 0;
}
 
public boolean updateCust1(long rowId, long imei_id,String cname, String c_cno,String c_add,String c_mail) {
    ContentValues values = createContentValues_cust(imei_id,cname,c_cno,c_add,c_mail);
 
    return db.update(T_CUST, values, IMEI_ID + "=" + rowId, null) &gt; 0;
}
 
/**
 * Deletes todo
 */
public boolean deleteCust(long rowId) 
{
    return db.delete(T_CUST, IMEI_ID + "=" + rowId, null) &gt; 0;
}
 
/**
 * Return a Cursor over the list of all todo in the database
 * 
 * @return Cursor over all notes
 */
public Cursor fetchAllCust() 
{
    return db.query(T_CUST, new String[] { CID,IMEI_ID, NAME,CNO,ADDRESS,MAIL}, null, null, null, null, null);
}
 
/**
 * Return a Cursor positioned at the defined todo
 */
public Cursor fetchCust(long rowId) throws SQLException 
{
    Cursor mCursor = db.query(true,T_CUST, new String[] {CID, IMEI_ID,NAME,CNO,ADDRESS,MAIL}, CID + "="
            + rowId, null, null, null, null, null);
    if (mCursor != null)
    {
        mCursor.moveToFirst();
    }
    return mCursor;
}
 
public Cursor fetchCust_imei(long rowId) throws SQLException 
{
    Cursor mCursor = db.query(true,T_CUST, new String[] {CID, IMEI_ID,NAME,CNO,ADDRESS,MAIL}, IMEI_ID + "="
            + rowId, null, null, null, null, null);
    if (mCursor != null)
    {
        mCursor.moveToFirst();
    }
    return mCursor;
}
 
private ContentValues createContentValues_cust(long imei_id,String cname,String c_cno, String c_add,String c_mail)
{
    ContentValues values = new ContentValues();
    values.put(IMEI_ID, imei_id);
    values.put(NAME, cname);
    values.put(ADDRESS, c_add);
    values.put(CNO, c_cno);
    values.put(MAIL, c_mail);
    return values;
}   
}</pre>

Please suggest the solution... To delete and search item

[edit]Code block added and some phantom tags corrected[/edit]
Posted 21-Apr-13 1:28am
Edited 21-Apr-13 3:43am
(no name)69.6K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 335
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200
0 OriginalGriff 465
1 Sergey Alexandrovich Kryukov 275
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200


Advertise | Privacy | Mobile
Web02 | 2.8.150331.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100