I'm working on the fragment, when I click a button, it will turn to another activity, after the activity saved data to database, it will return back to the fragment, however, the fragment can't show the newest data, I need to go to the activity of the fragment, then come back to the fragment again, it will show newest data.
Here is my code of fragment. I thought that because I put the setAdapter in the onActivityCreated() method, I tried to put it in onCreate() and onCreateView(), both will return nullpointer error.
public class TodoFragment extends ListFragment {
private TodoDataSource datasource;
private Todo item;
List<Todo> todoList;
MyCustomAdapter adapt;
ListView listTask;
EditText t;
public TodoFragment(){
}
/*#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setHasOptionsMenu(true);
datasource = new TodoDataSource(getActivity());
datasource.open();
todoList = datasource.getAllTodos();
adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
//ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
listTask.setAdapter(adapt);
}*/
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// if (activity != null) {
// Create an instance of the custom adapter for the GridView. A static array of location data
// is stored in the Application sub-class for this app. This data would normally come
// from a database or a web service.
datasource = new TodoDataSource(getActivity());
datasource.open();
todoList = datasource.getAllTodos();
adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
//ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
listTask.setAdapter(adapt);
Button button = (Button) getActivity().findViewById(R.id.simpleadd);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//String summary = t.getText().toString();
Intent i = new Intent(getActivity(), TodoDetailActivity.class);
//Bundle bundle = new Bundle();
// bundle.putString("Summary", summary);
//i.putExtras(bundle);
startActivity(i);
}
});
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todo, container, false);
t = (EditText) rootView.findViewById(R.id.simple);
listTask = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onResume() {
datasource.open();
super.onResume();
adapt.notifyDataSetChanged();
}
#Override
public void onPause() {
datasource.close();
super.onPause();
}
}
Here is part of my another activity code.
it will update the database and return to the fragment.
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (TextUtils.isEmpty(mTitleText.getText().toString())) {
makeToast();
} else {
final String category = mCategory.getSelectedItem().toString();
final String summary = mTitleText.getText().toString();
final String description = mBodyText.getText().toString();
datasource.createTodo(category, summary, description);
setResult(RESULT_OK);
finish();
}
}
});
It looks like you aren't updating your data source todoList before calling notifyDataSetChanged() in onResume().
Edit: Clear the list first, and then re-populate. Also add logging to help track down the issue:
#Override
public void onResume() {
super.onResume();
datasource.open();
todoList.clear();
todoList.addAll(datasource.getAllTodos());
adapt.notifyDataSetChanged();
Log.d("MyApp", "Size of data: " + todoList.size() );
}
Once you have added that code, try adding a few records, and watch your logcat to see if the size goes up. You can also cycle through each record, if you post your Todo class I can give you some example code.
Related
I have a LongClickListener in my RecyclerView's Adapter file that listens for LongClicks on CardViews in a RecyclerView list. The Listener is linked with an interface to the RecyclerView Activity file.
Step 1 after the LongClick launches a DialogFragment for the user to confirm deletion of a CardView. The dialog presents "Cancel" or "OK" choices. "Cancel" just negates the DialogFragment.
Step 2 is where "OK" was selected and the delete code in the DatabaseHelper file will remove the CardView data from the SQLite database.
Step 3 will be an intent to run the RecyclerView Activity again so that the RecyclerView list refreshes and shows the updated list (with the previous CardView removed since the data was deleted in the database).
I am good with Step 1 as a LongClick on any CardView is launches the DialogFragment as expected. I am stuck on Step 2 in the Fragment line "dbHelper.deletefromDB(UserData userdata);". Android Studio warning message is "Cannot resolve symbol 'userdata'". What am I missing here? Below shows the various files in question.
ListActivity file:
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbList = dbHelper.getAllDataFromDB();
...
ListAdapter mAdapter;
mAdapter = new ListAdapter(this, dbList);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setonItemClickListener(new ListAdapter.ClickListener() {
#Override
public void onItemLongClick(int position, View view) {
FragmentManager fm = getSupportFragmentManager();
DeleteCardViewFragment delCardViewDialog = new DeleteCardViewFragment();
delCardViewDialog.show(fm, "delcardview dialog");
}
});
DatabaseHelper file:
public class DatabaseHelper extends SQLiteOpenHelper {
...
public void deletefromDB(UserData userData) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
db.delete(DBContract.DBEntry.TABLE_NAME_USERINPUTS, DBContract.DBEntry.COLUMN_NAME_ID + " = ?",
new String[]{String.valueOf(userData.getId())});
db.setTransactionSuccessful();
db.endTransaction();
if(db.isOpen())
db.close();
}
}
DeleteCardViewFragment file:
public class DeleteCardViewFragment extends DialogFragment {
DatabaseHelper dbHelper;
public DeleteCardViewFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.delcardview_layout, container, false);
getDialog().setTitle("Delete card");
dbHelper = new DatabaseHelper(getActivity());
Button btnCancel = (Button) rootView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getDialog().cancel();
}
});
Button btnOK = (Button) rootView.findViewById(R.id.btnOK);
btnOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dbHelper.deletefromDB(UserData userdata);
dismiss();
}
});
...
}
}
I have an activity and a fragment
In my activity i have button in my action bar ,on button click a dialog box is popup , in that i have a listview.
If i select any item from that list , that data goes to fragment , and based on that data from activity my fragment listview should be shown
e.g. if i select Technology , technology id =1 should pass in my query parameter and based on that my fragment listview should appear.
everytime i select different item from list my fragment listview should be updated.
But my problem is that i am not getting any data in fragment and my list is not getting updated
Mainactivity.java
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem actionViewItem = menu.findItem(R.id.miActionButton);
// Retrieve the action-view from menu
View v = MenuItemCompat.getActionView(actionViewItem);
// Find the button within action-view
Button b = (Button) v.findViewById(R.id.btnCustomAction);
// Handle button click here
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(CUSTOM_DIALOG_ID);
}
});
return super.onPrepareOptionsMenu(menu);
}
#Override
public Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialoglayout);
dialog.setTitle("Select Category");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
}
});
dialog.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
}
});
// Prepare ListView in dialog
dialog_ListView = (ListView) dialog.findViewById(R.id.dialoglist);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
dialog_ListView.setAdapter(adapter);
dialog_ListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// i = (int) parent.getItemIdAtPosition(position);
category_name = (String) parent.getItemAtPosition(position);
Log.d("category name", category_name);
if(category_name.equals("a"))
{
cat_id=1;
}
if(category_name.equals("b"))
{
cat_id=2;
}
if(category_name.equals("c"))
{
cat_id=3;
}
if(category_name.equals("d"))
{
cat_id=4;
}
if(category_name.equals("e"))
{
cat_id=5;
}
if(category_name.equals("f"))
{
cat_id=6;
}
Log.d("id",""+cat_id);
dismissDialog(CUSTOM_DIALOG_ID);
}
});
break;
}
return dialog;
}
public int getMyData() {
return cat_id;
}
Fragment.java :
public String param;
public String url;
public int cat_id;
Mainactivity activity = (Mainactivity) getActivity();
int myDataFromActivity = activity.getMyData();
cat_id = myDataFromActivity;
Log.d("Category_id", "" + cat_id);
Map<String, String> map1 = new HashMap<String, String>();
param = "?cat_id=" + cat_id;
//URL is fake , original url is working fine.
url = "http://www.abbcd.com/xyz/webservices/data/getAllData.php"
+ param;
map1.put("url", url);
new MultiPartRequester(mainActivity, map1,
NetUtils.RequestCode.GET_ALL_DATA,
this).execute();
Updated Part :
#Override
public void updateFragmentList(int catId) {
// TODO Auto-generated method stub
param = "?cat_id=" + catId;
url = "http://www.abbcd.com/xyz/webservices/data/getAllData.php"+ param;
Map<String, String> map1 = new HashMap<String, String>();
map1.put("url", url);
new MultiPartRequester(mainActivity, map1,
NetUtils.GET_DATA, this)
.execute();
}
The good approach for communication between Activity and Fragment is Listeners. When you want to update fragment, use the listener and update the fragment. Create the interface in MainActivity
Listener
public interface FragmentListener {
void updateFragmentList(int catId);
}
Implement in Fragment as
public class MyFragment extends Fragment implements MainActivity.FragmentListener
then in onActivityCreated of Fragment
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((MainActivity) getActivity()).setFragmentListener(this);
}
MainActivity
public void setFragmentListener(FragmentListener listener)
{
this.fragmentListener = listener;
}
public void sendDataToFragment(int catId) {
this.fragmentListener.updateFragmentList(catId);
}
Hope this helps.
UPDATE
Sending data to fragment using this.fragmentListener.updateFragmentList(catId);, create the method in Fragment as ``
public void updateFragmentList(int catId){
//Here update your listview with cat id
}
use static global variable in activity
public static int cat_id = 0;
access cat_id in fragment like below
Map<String, String> map1 = new HashMap<String, String>();
param = "?cat_id=" + activity.cat_id;
or do below changes in your getMyData method
public int getMyData() {
return cat_id;
}
EDITED:
another approach finally work, call your fragment method from activity ans pass cat_id in method. instead of ExampleFragment use your fragment name.
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.mymethod(cat_id);
scenario:
First mainactivity launches and from the menu option user launches second activity using intent and there he adds some text to edittext and get that edittext value using intent to the first activity and add that value to the listview.
FirstActivity:
public class MainActivity extends Activity {
ListView lv;
EditText et;
String AddedTask ;
ArrayList<Model> modelList;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
if (intent.hasExtra("NewTask")) {
AddedTask = this.getIntent().getExtras().getString("NewTask");
lv = (ListView) findViewById(R.id.listViewData);
String name = AddedTask;
Model md = new Model(name);
modelList.add(md);
adapter = new CustomAdapter(getApplicationContext(), modelList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.action_add_task:
Intent i = new Intent(MainActivity.this, AddTask.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Second Activity:
public class AddTask extends Activity {
Button addtask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task);
// get action bar
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
addtask = (Button) findViewById(R.id.btnaddlist);
findViewById(R.id.btnaddlist).setOnClickListener(
new View.OnClickListener() {
public void onClick(View arg0) {
EditText edit = (EditText) findViewById(R.id.tskname);
Intent i = new Intent(AddTask.this,
MainActivity.class);
//Bundle bundle = new Bundle();
String TaskName = edit.getText().toString();
//bundle.putString("NewTask", TaskName);
i.putExtra("NewTask", TaskName);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//i.putExtras(bundle);
startActivity(i);
}
});
}
}
Now my problem is I'm able to add the data to the listview but each time I come back to mainactivity the previous data which was added is lost and updating the old data with my new data.
I have searched for many SO answers and most of them suggest to add adapter.notifyDataSetChanged(); which I have already tried and nothing worked.
I have done by checking the adapter is null or updating the data this way and getting null pointer exception:
if ( adapter== null )
{
adapter = new CustomAdapter(getApplicationContext(), modelList);
lv.setAdapter(adapter);
}
Can anyone say me how do I get this working ?
new View.OnClickListener() {
public void onClick(View arg0) {
EditText edit = (EditText) findViewById(R.id.tskname);
Intent i = new Intent(AddTask.this,
MainActivity.class);
//Bundle bundle = new Bundle();
String TaskName = edit.getText().toString();
//bundle.putString("NewTask", TaskName);
i.putExtra("NewTask", TaskName);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//i.putExtras(bundle);
startActivity(i);
}
});
You are starting a new Activity each time you want to add an item.
Consider using
startActivityForResult()
Link to Android Documentation
Even if startActivityForResult() is IMHO the best way to fix your problem, i'll give you a hint why it doesn't show your "old" data.
You don't finish your first Activity when going to the Second. You could aswell just finish your second activity and your MainActivity would run into
onResume()
therefor check the Android lifecycle
Don't use putextra and intent techniques. Follow below technique.
In main activity create sharedpref as follows:
public static final String SharedPref = "MyPreferences";
In activity 2 insert this code.
SharedPreferences settings=null;//declaration
settings=getSharedPreferences(SharedPref,0);//initialization
String TaskName= edit.getText().toString();
SharedPreferences.Editor editor = settings.edit();
editor.putString("NewTask", TaskName);
editor.commit();
finishfromchild(AddTask.this);
In main activity:
SharedPreferences settings=null;//declaration
In onCreate
//now initialise settings
settings=getSharedPreferences(SharedPref,0);//SharedPref is the foldername
//of your sharedpreferences(you create it first)
//Now create onResume method
public void onResume()
{
super.onResume();
AddedTask=settings.getString("NewTask", "");
lv = (ListView) findViewById(R.id.listViewData);
String name = AddedTask;
Model md = new Model(name);
modelList.add(md);
adapter = new CustomAdapter(getApplicationContext(), modelList);
lv.setAdapter(adapter);
}
Hope this works. I did the same in my previous project.
I have two activities, in the main one I have a text field and two buttons, the user can enter text in the text field.
When clicking the add button,text will be added to some ArrayList.
When clicking show in list button a new activity with its own fragment should open showing a listview that contains the names that the user has entered.
I used ArrayAdapter and this is working fine. When I am in the list activity and when I click the back button of the device everything goes fine and the data in the ArrayList is not lost.
If I click show in list again I will find the old data I entered at the first time. But, if I click the back icon provided by Android at the top left of the device screen, the ArrayListbecomes empty and when I click show in list the listview shows as empty.
Here is the main activity code,
public class MainActivity extends ActionBarActivity {
String LOG_TAG = this.getClass().getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> itemList= new ArrayList<String>();
final EditText text = (EditText) findViewById(R.id.text_field);
text.setHint("Enter name here");
Button addButton = (Button) findViewById(R.id.add_button);
Button showButton = (Button) findViewById(R.id.show_button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name= text.getText().toString();
itemList.add(name);
if (itemList.size()==1){
Toast.makeText(getApplicationContext(), "one", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "name has been added to the list", Toast.LENGTH_SHORT).show();
}
});
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (getApplicationContext(), ListActivity.class);
intent.putExtra("list", itemList);
startActivity(intent);
}
});
}//end oncreate
This is the ListActivity and its fragment code,
public class ListActivity extends ActionBarActivity {
static ArrayList <String> itemList;
String LOG_TAG = this.getClass().getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
itemList= getIntent().getStringArrayListExtra("list");
setContentView(R.layout.list_activity);
if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction().add(R.id.container, new list()).commit();
}
}
public static class list extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
ListView listView = (ListView) rootView.findViewById(R.id.list);
ArrayAdapter <String> adapter= new ArrayAdapter<String>(getActivity(),R.layout.fragment_main, R.id.row, itemList);
listView.setAdapter(adapter);
return rootView;
}
}
}
Can anyone please tell me why this is happening? How can I solve this issue?
Any help is appreciated.
Many thanks.
create a class Constantss.java and declare
public static ArrayList <String> itemList=null;
then inside your class
use
Constantss. itemList.add(name);
instead of
itemList.add(name);
// replace itemList with Constantss. itemList in all of your classes
I believe this is a navigation issue. So you should close your ListActivity like this:
public class ListActivity extends ActionBarActivity {
//your code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemID = item.getItemId();
switch (itemID ) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Anyway your data can be lost if android destroys your MainActivity. So if you navigate from ListActivity back to MainActivity, MainActivity will be recreated and you data get lost. So you should find the way how to save your
data permanent.
there are two common ways:
Save data to data base (SQLite)
Save data to Preferences How to store list of values in SharedPreferences
i need help please.
I have two activities and a db. All i want to do is when i press a button in activity A, i send the string in my editText to the activity B. In the activity B i try to insert that string in my db and display it in a listview. All works if i start the activity B in the activity A but i wont display the activity B.
Here my code:
Activity A:
public class Example extends Activity {
public final static String ID_EXTRA2="com.example.activities";
public EditText MyInputText = null;
ImageView MyButton;
TextView MyOutputText, MyInputtext;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diccionary);
MyInputText = (EditText)findViewById(R.id.InputText);
MyOutputText = (TextView)findViewById(R.id.OutputText);
MyButton = (ImageView)findViewById(R.id.botonaceptar);
MyButton.setOnClickListener(MyButtonOnClickListener);
public ImageView.OnClickListener MyTranslateButtonOnClickListener = new ImageView.OnClickListener(){
public void onClick(View v) {
String InputString;
InputString = MyInputText.getText().toString();
try{
Intent d = new Intent(Example.this, Secondactivity.class);
d.putExtra(ID_EXTRA2, InputString);
//startActivity(d); <----- If i start the secondactivity, that works...
}catch (Exception e){
}
}
};
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menu1:
startActivity(new Intent("com.example.activities.SECONDACTIVITY"));
return true;
case R.id.menu2:
//something to do
return true;
}
return false;
}
};
Activity B:
public class Secondactivity extends Activity {
NoteAdapter2 adapter = null;
NoteHelper2 helper2 = null;
Cursor dataset_cursor2 = null;
String entriId = null;
public ListView list2;
String passedword = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
try{
list2 = (ListView)findViewById(R.id.list2);
helper2 = new NoteHelper2(this);
---->passedword = getIntent().getStringExtra(Example.ID_EXTRA2);
---->helper2.insert(passedword); //here i insert the string in my db
startManagingCursor(dataset_cursor2);
dataset_cursor2 = helper2.getAll();
adapter = new NoteAdapter2(dataset_cursor2);
list2.setAdapter(adapter);
dataset_cursor2.requery();
adapter.notifyDataSetChanged();
//this is how we know to do when a list item is clicked
list2.setOnItemClickListener(onListClick);
}
catch (Exception e){
// this is the line of code that sends a real error message to the log
Log.e("ERROR", "ERROR IN CODE" + e.toString());
//this is the line that prints out the location in the code where the error ocurred
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
helper2.close();
}
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
entriId = String.valueOf(id);
}
};
class NoteAdapter2 extends CursorAdapter {
NoteAdapter2(Cursor c){
super(Zhistorytranslate.this, c);
}
public void bindView(View row2, Context ctxt, Cursor c) {
NoteHolder2 holder2 = (NoteHolder2)row2.getTag();
holder2.populateFrom2(c, helper2);
}
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row2 = inflater.inflate(R.layout.row2, parent, false);
NoteHolder2 holder2 = new NoteHolder2(row2);
row2.setTag(holder2);
return (row2);
}
}
static class NoteHolder2 {
private TextView entriText = null;
NoteHolder2(View row2){
entriText = (TextView)row2.findViewById(R.id.entri);
}
void populateFrom2(Cursor c, NoteHelper2 helper2) {
entriText.setText(helper2.getEntri(c));
}
}
If I understood you correctly, you want to send data from A to B without launching B. In this case, you need to forget about Intents and consider a way to store data in your app. My suggestion is to use SharedPreferences.
Here is how you can use it:
In your Activity A, store the String:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("string_id", InputString); //InputString: from the EditText
editor.commit();
In your Activity B, you can get the value by:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String data = prefs.getString("string_id", "no id"); //no id: default value
Your question is very difficult to understand, but you seem to want to be able to add the EditText's contents into your database without starting another Activity. Simply do this in your Image's OnClickListener:
public void onClick(View v) {
NoteHelper2 helper2 = new NoteHelper2(this);
helper2.insert(MyInputText.getText().toString());
}
You should also read about Java naming convention.