从onItemClick)传递活动之间的值

时间:2015-02-24 15:01:41

标签: android

请参阅以下代码:

活动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;
            }

我的问题:

我之前问过这个问题,但也许我不够清楚,这是第二次尝试:

  1. 活动A具有来自getText()。toString()
  2. 的“tvName”变量
  3. 此getText()。toString()来自用户点击活动A
  4. 活动B不知道onCreate
  5. 下的“tvName”
  6. 我把它定义为“”(没有)
  7. 活动B如何知道“tvName”值,可能是“名称1”或“名称2”或“名称3”,具体取决于用户从活动A点击的内容?这需要在活动B上创建onCreate。
  8. 最后一旦知道了值,我的数据库助手类就可以使用该值来选择“WHERE name ='”+ tvName +“'”
  9. 到目前为止,数据库帮助程序类使用“”(无),因为“tvName”在活动B上没有定义,因为我无法将其定义为“tvName”

2 个答案:

答案 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");
祝你好运。