title: adding a list view Item from another activity
body: <p>I am a beginner in android development, I am trying to add an list item value from a different activity, i wrote a code where I am able to add an list item from within the activity, but not from outside the activity</p>
  
  <p>TestDatabaseActivity.java</p>
  
  <pre><code>package com.laith.sql;
  
  import java.util.List;
  import android.app.ListActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.EditText;
  
  public class TestDatabaseActivity extends ListActivity {
  private CommentsDataSource datasource;
  public ArrayAdapter&lt;Comment&gt; adapter;
  private EditText insert_et;
  public Comment comment;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.sqlite);
  
      insert_et =(EditText)findViewById(R.id.editText1);
      datasource = new CommentsDataSource(this);
      datasource.open();
  
      List&lt;Comment&gt; values = datasource.getAllComments();
  
      // Use the SimpleCursorAdapter to show the
      // elements in a ListView
      adapter = new ArrayAdapter&lt;Comment&gt;(this,
              android.R.layout.simple_list_item_1, values);
      setListAdapter(adapter);
  }
  
  // Will be called via the onClick attribute
  // of the buttons in main.xml
  public void onClick(View view) {
      ArrayAdapter&lt;Comment&gt; adapter = (ArrayAdapter&lt;Comment&gt;) getListAdapter();
      comment = null;
      switch (view.getId()) {
      case R.id.add:
                      MySingleton mys = MySingleton.getInstance();
  
          String test = mys.getInstance().getMyStrings();
          addComment(test);
          break;
      case R.id.delete:
          if (getListAdapter().getCount() &gt; 0) {
              comment = (Comment) getListAdapter().getItem(0);
              datasource.deleteComment(comment);
              adapter.remove(comment);
          }
          break;
      }
      adapter.notifyDataSetChanged();
  }
  
  @Override
  protected void onResume() {
      datasource.open();
      super.onResume();
  }
  
  @Override
  protected void onPause() {
      datasource.close();
      super.onPause();
  }
  
  /// method used to add a comment from a different directory 
  public void addComment(String LastComment)
  {
      // code here to be called by another activity
  
                  ArrayAdapter&lt;Comment&gt; adapter = (ArrayAdapter&lt;Comment&gt;)getListAdapter();
          comment = datasource.createComment(LastComment);
          adapter.add(comment);
          adapter.notifyDataSetChanged();
             // am getting a null value for comment when I call the function from another activity
  
  
  }
  
  }
  </code></pre>
  
  <p>Test button in another activity</p>
  
  <pre><code>public Button.OnClickListener Test_button = new Button.OnClickListener() 
   {
       public void onClick(View v)
       {
           String verify_string="test";
           MySingleton mys = MySingleton.getInstance();
           mys.setMyStrings(verify_string);
  
           TestDatabaseActivity tdba = new TestDatabaseActivity();
           tdba.addComment(mys.getMyStrings());
       }
   };
  </code></pre>
  
  <p>Please help ! :)</p>
  
  <p>Thank you</p>
  
  <p>&lt;--------------------------- working solution --------------------------------------------->
  new class created</p>
  
  <pre><code>  public class ConnectToDB 
  {
  private CommentsDataSource datasource;
  
  public void addCommentToDB(Context context, String new_comment)
  {
      datasource = new CommentsDataSource(context);
      datasource.open();
      datasource.createComment(new_comment);  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>onResume edited</p>
  
  <pre><code>protected void onResume() {
      datasource.open();
      values = datasource.getAllComments();
      adapter = new ArrayAdapter&lt;Comment&gt;(this,
              android.R.layout.simple_list_item_1, values);
      setListAdapter(adapter);
      adapter.notifyDataSetChanged();
  
      super.onResume();
  }
  </code></pre>