TwoStarII
TwoStarII

Reputation: 351

Pass Values From onItemClick) Between Activities

Please see the following code:

Activity A:

        private void clickableListView() {

        ListView lv = (ListView) findViewById(R.id.listViewArtists);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) { //LIST ITEM CLICK

                LinearLayout ll = (LinearLayout) viewClicked;
                TextView tv = (TextView) ll.findViewById(R.id.textViewArtistsSingle);
                String tvName = tv.getText().toString(); //THIS IS MY VALUE "tvName" WHICH IS A GET STRING FROM DATABASE

                startActivity(new Intent(ActivityA.this, ActivityB.class)); 

Activity B:

        public class ActivityB extends MainActivity implements AdapterView.OnItemClickListener {

        DatabaseHelper dbHeplper;

        ListView list;
        List<String> arrArtist;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_artist);

        dbHeplper = new DatabaseHelper(getApplicationContext());

        arrArtist = dbHeplper.getDBNameSingle(""); //my get Database name clicked is just "" and cannot be "tvName" because it is unkown
        //arrArtist = dbHeplper.getDBNameSingle(tvName); // CANNOT BE "tvName" becuase it is unknown

DatabaseHelper:

        DATABASEHELPER:

        public List<String> getDBNameSingle(String tvName){
                List<String> listRows = new ArrayList<String>();
                SQLiteDatabase db = this.getWritableDatabase();

                Cursor c;

                try {

                c = db.rawQuery("SELECT name FROM " + TABLE_LM + " WHERE name = '"  + tvName +"'", null); //tvName is just "" becuase of Activity B and cannot be "tvName" because it is unkown 

                if(c == null) return null;

                String row;
                c.moveToFirst();
                do {            
                    row = c.getString(0);            
                    listRows.add(row);
                } while (c.moveToNext()); 
                c.close();
                } catch (Exception e) {
                Log.e("ts04", e.getMessage());
                }

                db.close();        

                return listRows;
            }

My Question:

I have asked this before but maybe I was not clear enough, here goes my second try:

  1. Activity A has "tvName" variable from getText().toString()
  2. This getText().toString() comes from user click from Activity A
  3. Activity B does not know of "tvName" under onCreate
  4. I have instead defined it as "" (nothing)
  5. How can Activity B know about "tvName" value which might be "name 1" or "name 2" or "name 3" depending on what the user clicked from Activity A? This needs to be under onCreate on Activity B.
  6. Finally once the value is known my database helper class can use that value to select " WHERE name = '" + tvName +"'"
  7. So far the database helper class uses "" (nothing) because "tvName" is defined as nothing on Activity B as I cannot define it as "tvName"

Upvotes: 0

Views: 358

Answers (2)

Batuhan Coşkun
Batuhan Coşkun

Reputation: 2969

You can use intent's extra methods for this. Only you have to do is put an extra info from ActivityA and then get this extra from ActivityB.

You should put this info before startActivity on ActivityA:

Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("value", tvName);
startActivity(intent);

Then get this extra from AvtivityB in onCreate:

Intent intent = getIntent();
String tvName = (String) intent.getSerializableExtra("value");

Good luck.

Upvotes: 1

Manuel Ram&#237;rez
Manuel Ram&#237;rez

Reputation: 2415

In your ActivityA

Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("yourvariable", tvName);
startActivity(intent)

In your ActivityB

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String yourvariable = extras.getString("yourvariable");
}

Then use yourvariable for whatever you want.

Upvotes: 2

Related Questions