title: How to retrieve the data from database from listview click
body: <p>I want to show the data from database for editing,on listview item click.on which item user click,it show the data in editbox in next activity.so that user can update it,and also i want onlongClick user can delete that particular data.in listview i'am displaying only name.,nt the contact number and message..,how to display them too in the edittext in smsSend activity..?</p>
  
  <p>Campaign_Details.java</p>
  
  <pre><code>    package com.example.smscampaign;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.concurrent.Semaphore;
  
  import com.example.second.Scale;
  
  
  
  
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager.NameNotFoundException;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.os.Build;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class Campaign_Details extends Activity implements OnClickListener {
       Cursor c;
       private DataBaseHelp mdb;
          private ListView lvMessage;
          private ArrayList&lt;Bean&gt; alSentMessage;
          private Demo adapter;
          public String ADD_TO_NAME="toname";
   ListView listview;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_list_demostration);
          TextView txt1= (TextView) findViewById(R.id.data);
           listview = (ListView) findViewById(R.id.listview);
           PackageInfo pInfo = null;
           try 
           {
              pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
           } 
           catch (NameNotFoundException e) 
           {
              e.printStackTrace();
           }
          txt1.setText(pInfo.versionName);
  
          TextView txt= (TextView) findViewById(R.id.textnum1);
    /*      mdb=new DataBaseHelp(this);
          alSentMessage=new ArrayList&lt;Bean&gt;();
          lvMessage=(ListView)findViewById(R.id.listview);
          c=mdb.fetchAllContacts();
          c.moveToFirst();
          Log.d("cursor",""+c.getCount());
          while(c.moveToNext()){
            Bean mb=new Bean();
            Log.e("ColumnIndex",""+c.getColumnIndex(DataBaseHelp.KEY_NAME));
            mb.setToName((c.getString(c.getColumnIndex(DataBaseHelp.KEY_NAME))));
            alSentMessage.add(mb);
          }
          adapter=new Demo(Campaign_Details.this,alSentMessage);
          lvMessage.setAdapter(adapter);*/
        //  String [] values1= data.split("\n");
         // int t = values1.length;
         // txt.setText(Integer.toString(t));
  
         // String[] values = new String[] { data };
          DataBaseHelp info= new DataBaseHelp(this);
          info.open();
          //ArrayList&lt;String&gt; list1 = new ArrayList&lt;String&gt;();
          //list1= info.getData();
          //String[] values= list1.;
          ArrayList&lt;String&gt; arr = info.getData();
                  final ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
                  for (int i = 0; i &lt;  arr.size(); i++) {
                      list.add(arr.get(i));
                  }
                  adapter = new Demo(this, list);
                //  Demo.setNotifyOnChange(true);
  
  
                  listview.setAdapter(adapter);
  
  
         listview.setOnClickListener(this);
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
           Intent i= new Intent("com.example.second.SMSSEND");
          String mName= SmsSend.ed1.getText().toString();
         String mScale=SmsSend.ed2.getText().toString();
         String mContatcs= SmsSend.conct.toString() ;
        String st=   information.getText().toString();
          long lt= Long.parseLong(st);
          DataBaseHelp e= new DataBaseHelp(this);
          e.open();
          e.updateEntry(lt,mName,mScale,mContatcs);
  
          e.close();  
           startActivity(i);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // TODO Auto-generated method stub
          getMenuInflater().inflate(R.menu.main2, menu);
          return true;
  
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // TODO Auto-generated method stub
  
          switch (item.getItemId()) {
          case R.id.nextPage:
              Intent i= new Intent(Campaign_Details.this,SmsSend.class);
              startActivity(i);
              break;
  
          }
          return true;
      }
  
  
  
  
  } 
  </code></pre>
  
  <p>SmsSend.java</p>
  
  <pre><code>import java.util.Map;
  
  import com.example.smscampaign.MainActivity.MyAdapter;
  
  import android.app.ActionBar;
  import android.app.Activity;
  import android.app.Dialog;
  import android.app.PendingIntent;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.database.sqlite.SQLiteDatabase;
  import android.os.Bundle;
  import android.telephony.SmsManager;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class SmsSend extends Activity implements OnClickListener {
      BroadcastReceiver smsSentReciver, smsSentDelivery;
      static EditText ed1, ed2;
      static int ResultCode = 12;
      static ArrayList&lt;String&gt; sendlist = new ArrayList&lt;String&gt;();
      Button b1, b2, b3, b4;
      TextView txt;
      static StringBuilder conct = new StringBuilder();
      String contacts = "";
      String delim = ";";
      public static String Name;
      TextView ed;
      int i = 0;
      String[] cellArray;
      DataBaseHelp notasdb = new DataBaseHelp(this);
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.smssend);
  
          ed1 = (EditText) findViewById(R.id.editText1);
          ed2 = (EditText) findViewById(R.id.editText2);
          b1 = (Button) findViewById(R.id.button1);
          b1.setOnClickListener(this);
          b2 = (Button) findViewById(R.id.button2);
          b2.setOnClickListener(this);
          b3 = (Button) findViewById(R.id.button3);
          b3.setOnClickListener(this);
          b4 = (Button) findViewById(R.id.button4);
          b4.setOnClickListener(this);
          txt = (TextView) findViewById(R.id.textnum2);
          ActionBar actionBar = getActionBar();
          actionBar.setDisplayHomeAsUpEnabled(true);
  
      }
  
      @Override
      protected void onPause() {
          // TODO Auto-generated method stub
          super.onPause();
          unregisterReceiver(smsSentReciver);
          unregisterReceiver(smsSentDelivery);
      }
  
      @Override
      protected void onResume() {
          // TODO Auto-generated method stub
          super.onResume();
  
          smsSentReciver = new BroadcastReceiver() {
  
              @Override
              public void onReceive(Context arg0, Intent arg1) {
                  // TODO Auto-generated method stub
                  switch (getResultCode()) {
                  case Activity.RESULT_OK:
                      Toast.makeText(getBaseContext(), "sms has been sent",
                              Toast.LENGTH_SHORT).show();
                      break;
                  case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                      Toast.makeText(getBaseContext(), "Generic Fail",
                              Toast.LENGTH_SHORT).show();
                      break;
                  case SmsManager.RESULT_ERROR_NO_SERVICE:
                      Toast.makeText(getBaseContext(), "No Service",
                              Toast.LENGTH_SHORT).show();
                      break;
                  case SmsManager.RESULT_ERROR_NULL_PDU:
                      Toast.makeText(getBaseContext(), "Null PDU",
                              Toast.LENGTH_SHORT).show();
                      break;
                  case SmsManager.RESULT_ERROR_RADIO_OFF:
                      Toast.makeText(getBaseContext(), "Radio Off",
                              Toast.LENGTH_SHORT).show();
                      break;
                  default:
                      break;
  
                  }
              }
  
          };
          smsSentDelivery = new BroadcastReceiver() {
  
              @Override
              public void onReceive(Context context, Intent intent) {
                  // TODO Auto-generated method stub
                  switch (getResultCode()) {
                  case Activity.RESULT_OK:
                      Toast.makeText(getBaseContext(), "Sms Delivered",
                              Toast.LENGTH_SHORT).show();
                      break;
                  case Activity.RESULT_CANCELED:
                      Toast.makeText(getBaseContext(), "Sms not Delivered",
                              Toast.LENGTH_SHORT).show();
                      break;
                  }
              }
  
          };
          registerReceiver(smsSentReciver, new IntentFilter("SMS_SENT"));
          registerReceiver(smsSentDelivery, new IntentFilter("SMS_DELIVERED"));
  
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
          switch (v.getId()) {
  
          case R.id.button3:
  
              Intent a = new Intent(SmsSend.this, MainActivity.class);
  
              startActivityForResult(a, ResultCode);
              break;
          case R.id.button4:
              Intent file = new Intent(SmsSend.this, File_Selecter.class);
              startActivity(file);
              break;
  
          case R.id.button1:
              if (ed1.getText().toString().length() == 0) {
                  ed1.setError("First name is required!");
              } else {
                  boolean diditwork1 = true;
  
                  try {
                      String Name = ed1.getText().toString();
  
                      SmsManager smsManager = SmsManager.getDefault();
  
                      String msg = ed2.getText().toString();
  
                      PendingIntent piSend = PendingIntent.getBroadcast(this, 0,
                              new Intent("SMS_SENT"), 0);
                      PendingIntent piDelivered = PendingIntent.getBroadcast(
                              this, 0, new Intent("SMS_DELIVERED"), 0);
  
                      Log.i("SMS", "contacts: " + contacts);
  
                      String[] cellArray;
  
                      contacts = conct.toString();
  
                      cellArray = contacts.split(";");
  
                      for (int a1 = 0; a1 &lt; cellArray.length; a1++) {
  
                          // smsManager.sendTextMessage(cellArray[a1].toString(),
                          // null,
                          // msg, piSend, piDelivered);
                      }
                      DataBaseHandler entry = new DataBaseHandler(SmsSend.this);
                      entry.open();
  
                      entry.entryCreate(Name, msg , contacts);
  
                      entry.close();
                  } catch (Exception e) {
                      diditwork1 = false;
                      String erroe = e.toString();
  
                      Dialog d = new Dialog(this);
                      d.setTitle("Dang it!");
                      TextView tv = new TextView(this);
                      tv.setText(erroe);
                      d.setContentView(tv);
                      d.show();
  
                  } finally {
                      if (diditwork1) {
                          Dialog d = new Dialog(this);
                          d.setTitle("Heck Yeah!");
                          TextView tv = new TextView(this);
                          tv.setText("Success");
                          d.setContentView(tv);
                          d.show();
                      }
                  }
                  ed1.setText("");
                  ed2.setText("");
                  txt.setText("0");
                  conct.delete(0, conct.length());
                  break;
  
              }
  
          case R.id.button2:
  
              Log.i("SMS", "Sendlist Size: " + sendlist.size());
                String inputLine = "";
              if (ed1.getText().toString().length() == 0
                      || ed1.getText().toString().length() == 0
          || txt.getText().equals("0")) {
                  ed1.setError("First name is required!");
                  ed2.setError("Message and contacts are required!");
                  txt.setError("Contacts required!");
              } else {
                  boolean diditwork1 = true;
  
                  try {
                      String Name = ed1.getText().toString();
  
                      SmsManager smsManager = SmsManager.getDefault();
  
                      String msg = ed2.getText().toString();
  
                      PendingIntent piSend = PendingIntent.getBroadcast(this, 0,
                              new Intent("SMS_SENT"), 0);
                      PendingIntent piDelivered = PendingIntent.getBroadcast(
                              this, 0, new Intent("SMS_DELIVERED"), 0);
  
                      Log.i("SMS", "contacts: " + contacts);
  
  
  
                      contacts = conct.toString();
  
                      cellArray = contacts.split(";");
  
                      for (int a1 = 0; a1 &lt; cellArray.length; a1++) {
  
                          // smsManager.sendTextMessage(cellArray[a1].toString(),
                          // null,
                          // msg, piSend, piDelivered);
  
                      }
                      DataBaseHelp entry = new DataBaseHelp(SmsSend.this);
                      entry.open();
  
                      entry.entryCreate(Name, msg , contacts);
  
                      entry.close();
  
                  } catch (Exception e) {
                      diditwork1 = false;
                      String erroe = e.toString();
  
                      Dialog d = new Dialog(this);
                      d.setTitle("Dang it!");
                      TextView tv = new TextView(this);
                      tv.setText(erroe);
                      d.setContentView(tv);
                      d.show();
  
                  } finally {
                      if (diditwork1) {
                          Dialog d = new Dialog(this);
                          d.setTitle("Heck Yeah!");
                          TextView tv = new TextView(this);
                          tv.setText("Success");
                          d.setContentView(tv);
                          d.show();
                      }
                  }
                  ed1.setText("");
                  ed2.setText("");
                  txt.setText("0");
                  conct.delete(0, conct.length());
  
  
  
  
  
  
                  break;
              }
          }
      }
  
  
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  
          if (requestCode == ResultCode) {
  
              if (resultCode == RESULT_OK) {
                  // Intent t = getIntent();
                  sendlist = data.getStringArrayListExtra("name");
                  if (sendlist != null) {
  
                      for (int i = 0; i &lt; sendlist.size(); i++) {
                          conct.append(sendlist.get(i).toString());
                          conct.append(delim);
                      }
  
                  }
  
              }
  
              i = sendlist.size();
              txt.setText(Integer.toString(i));
              if (resultCode == RESULT_CANCELED) {
  
              }
  
          }
      }
  
  }
  </code></pre>
  
  <p>Databasehelp.java</p>
  
  <pre><code>package com.example.smscampaign;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.w3c.dom.Comment;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.SQLException;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteDatabase.CursorFactory;
  import android.database.sqlite.SQLiteOpenHelper;
  
  
  
  public class DataBaseHelp{
  
      public static final String KEY_ROWID="_id";
      public static final String KEY_NAME="person_name";
      public static final String KEY_SCALE="scale_person";
      public static final String KEY_CONTACTS="Contacts_person";
  
      private static final String DATABASE_NAME="Himani";
       static final String DATABASE_TABLE="peopleTable";
      private static final int DATABASE_VERSION=1;
  
      private DbHelper ourHepler;
      private final Context ourContext;
       SQLiteDatabase ourDatabase;
      public class DbHelper extends SQLiteOpenHelper{
  
          public DbHelper(Context context) {
              super(context,DATABASE_NAME,null,DATABASE_VERSION);
              // TODO Auto-generated constructor stub
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              // TODO Auto-generated method stub
              db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
                      KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT,  " +
                      KEY_NAME + " TEXT NOT NULL, " +
                      KEY_SCALE + " TEXT NOT NULL ,"  + KEY_CONTACTS + ")"
                      );
              }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              // TODO Auto-generated method stub
              db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
              onCreate(db);
          }
  
      }
  
      public DataBaseHelp(Context c){
          ourContext=c;
      }
  
  
      public DataBaseHelp open() throws SQLException{
          ourHepler = new DbHelper(ourContext);
          ourDatabase=  ourHepler.getWritableDatabase();
          return this;
      }
      public void close()
      {
          ourHepler.close();
      }
  
  
      public long entryCreate(String name, String scale , String contacts) {
          // TODO Auto-generated method stub
  
          ContentValues cv=new ContentValues();
          cv.put(KEY_NAME, name);
          cv.put(KEY_SCALE, scale);
          cv.put(KEY_CONTACTS, contacts);
          return ourDatabase.insert(DATABASE_TABLE, null, cv); 
  
      }
  
  
      public ArrayList&lt;String&gt; getData() {
  
          String[] col= new String[]{KEY_ROWID,KEY_NAME,KEY_SCALE, KEY_CONTACTS};
          Cursor c=  ourDatabase.query(DATABASE_TABLE, col, null, null, null, null, null);
          String run="";
          int iRow=c.getColumnIndex(KEY_ROWID);
          int iName=c.getColumnIndex(KEY_NAME);
          int iScale=c.getColumnIndex(KEY_SCALE);
          int iMessage=c.getColumnIndex(KEY_CONTACTS);
          ArrayList&lt;String&gt; newList= new ArrayList&lt;String&gt;();
          for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
           newList.add(c.getString(iRow)+ " " + c.getString(iName) + " " + c.getString(iScale) + " " + c.getString(iName));
          }
  
          return newList;
      }
  
      public Cursor fetchAllContacts()
  
      { 
          ourDatabase=ourHepler.getReadableDatabase();
  
      String selectQuery = "SELECT *  FROM " + DATABASE_TABLE;
  
      return ourDatabase.rawQuery(selectQuery, null);
      }
  
  
  
      public String getScale(long l) {
          // TODO Auto-generated method stub
          String[] col= new String[]{KEY_ROWID,KEY_NAME,KEY_SCALE , KEY_CONTACTS};
          Cursor c= ourDatabase.query(DATABASE_TABLE, col,KEY_ROWID + "-" + l, null, null, null, null);
  
          if(c != null){
              c.moveToFirst();
              String scale=c.getString(2);
              return scale;
          }
          return null;
      }
  
  
      public String getName(long l)  {
          // TODO Auto-generated method stub
          String[] col= new String[]{KEY_ROWID,KEY_NAME,KEY_SCALE , KEY_CONTACTS};
          Cursor c= ourDatabase.query(DATABASE_TABLE, col,KEY_ROWID + "-" + l, null, null, null, null);
  
          if(c != null){
              c.moveToFirst();
              String name=c.getString(1);
              return name;
  
          }
          return null;
      }
      public String getContacts(long l)  {
          // TODO Auto-generated method stub
          String[] col= new String[]{KEY_ROWID,KEY_NAME,KEY_SCALE ,KEY_CONTACTS};
          Cursor c= ourDatabase.query(DATABASE_TABLE, col,KEY_ROWID + "-" + l, null, null, null, null);
  
          if(c != null){
              c.moveToFirst();
              String contacts=c.getString(3);
              return contacts;
  
          }
          return null;
      }
  
      public  void updateEntry(long lt, String mName, String mScale ,String mContatcs) {
          // TODO Auto-generated method stub
            ContentValues cvUpdate=new ContentValues();   
            cvUpdate.put(KEY_NAME,mName);
            cvUpdate.put(KEY_SCALE,mScale);
            cvUpdate.put(KEY_CONTACTS,mContatcs );
            ourDatabase.update(DATABASE_TABLE,  cvUpdate, KEY_ROWID + "-" + lt, null);
      }
  
  
      public void deleteEntry(long ltt) throws SQLException{
          // TODO Auto-generated method stub
          ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + ltt,null);
  
      }
  }
  </code></pre>