请参阅以下代码:
活动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));
活动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;
}
我的问题:
我之前问过这个问题,但也许我不够清楚,这是第二次尝试:
答案 0 :(得分:2)
在您的活动中
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("yourvariable", tvName);
startActivity(intent)
在您的活动中
Bundle extras = getIntent().getExtras();
if (extras != null) {
String yourvariable = extras.getString("yourvariable");
}
然后使用yourvariable
获取您想要的任何内容。
答案 1 :(得分:1)
您可以使用intent的额外方法。只有您需要做的是从ActivityA
添加额外信息,然后从ActivityB
获得额外信息。
您应该在startActivity
之前将此信息放在ActivityA
:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("value", tvName);
startActivity(intent);
然后从AvtivityB
中的onCreate
获取额外费用:
Intent intent = getIntent();
String tvName = (String) intent.getSerializableExtra("value");
祝你好运。