android putextra without start activity

2019-05-11 23:18发布

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));
    }
}

2条回答
趁早两清
2楼-- · 2019-05-12 00:05

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
    
查看更多
孤傲高冷的网名
3楼-- · 2019-05-12 00:11

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.

查看更多
登录 后发表回答