Reputation: 351
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:
Upvotes: 0
Views: 358
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
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