2015-09-19 52 views
1

我想從我的列表視圖的項目發送字符串值到第二個活動。但第二項活動沒有得到價值。我應該怎麼做才能發送所需的信息?這是獲得需要物品ID的正確方法嗎?請告訴我。 月1日活動代碼發送字符串項目點擊

private static final int CM_DELETE_ID = 1; 
ListView lvData; 
DB2 db; 
SimpleCursorAdapter scAdapter; 
Cursor cursor; 
Button bt; 
SharedPreferences bh; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test4); 
    bt = (Button)findViewById(R.id.button); 
    bh = getSharedPreferences("O", Context.MODE_PRIVATE); 
    db = new DB2(this); 
    db.open(); 



    cursor = db.getAllData(); 
    startManagingCursor(cursor); 

    String[] from = new String[] { DB2.COLUMN_IMG, DB2.COLUMN_TXT, DB2.COLUMN_NMB }; 
    int[] to = new int[] { R.id.ivImg, R.id.tvText, R.id.tvNmb }; 

    scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to); 
    lvData = (ListView) findViewById(R.id.lvData); 
    lvData.setAdapter(scAdapter); 

    registerForContextMenu(lvData); 
    lvData.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
           long id) { 
      //THIS IS A PART WHERE I NEED HELP! 
      final SharedPreferences.Editor editor = bh.edit(); 
      editor.putString("name", DB2.COLUMN_TXT); 
      editor.putString("number",DB2.COLUMN_NMB); 
      Intent intent = new Intent(TEST4.this,TEST6.class); 
      startActivity(intent); 
     } 
    }); 

    lvData.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 



     public void onItemSelected(AdapterView parentView, View childView, 
            int position, long id) { 
      setContentView(R.layout.item); 
     } 

     public void onNothingSelected(AdapterView parentView) { 

     } 
    }); 


} 

private void buttonClick() 
{ 
    startActivity(new Intent("com.example.dc2.TEST5")); 
} 


public void onButtonClick(View view) { 
    switch(view.getId()) 
    { 
     case R.id.button: { 
      buttonClick(); 
      break; 
     } 
    } 
} 

public void onCreateContextMenu(ContextMenu menu, View v, 
           ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    menu.add(0, CM_DELETE_ID, 0, R.string.delete_record); 
} 


public boolean onContextItemSelected(MenuItem item) { 
    if (item.getItemId() == CM_DELETE_ID) { 
     AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
     db.delRec(acmi.id); 
     cursor.requery(); 
     return true; 
    } 
    return super.onContextItemSelected(item); 
} 

protected void onDestroy() { 
    super.onDestroy(); 
    db.close(); 
} 

第二活動代碼

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test6); 
    SharedPreferences bh; 
    bh = getSharedPreferences("O", Context.MODE_PRIVATE); 
    TextView tv = (TextView)findViewById(R.id.textView2); 
    String name = bh.getString("name","Hello"); 
    String number = bh.getString("number","HELLO"); 
    tv.setText(name + " " + number); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_test6, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
+0

我應該添加我的DB代碼嗎? –

回答

3

使用Intent而不是SharedPreferences捆綁您的string,並把它傳遞給Activity2

可以創建一個Bundle對象,並把你的字符串中的Bundle對象,或者直接把你的字符串中的Intent(這將暗示爲你創建一個包)

方法1

Activity1

public void onItemClick(AdapterView<?> parent, View view, int position, 
          long id) { 
     //THIS IS A PART WHERE I NEED HELP! 
     Bundle bundle = new Bundle(); 
     bundle.putString("name", cursor.getString(cursor.getColumnIndex(DB2.COLUMN_TXT)); 
     bundle.putString("number", cursor.getString(cursor.getColumnIndex(DB2.COLUMN_NMB)); 
     Intent intent = new Intent(TEST4.this,TEST6.class); 
     intent.putExtras(bundle); 
     startActivity(intent); 
    } 
}); 

Activity2

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test6); 
    Bundle bundle = getIntent().getExtras(); 
    String name = bundle.getString("name"); 
    String number = bundle.getString("number"); 
    TextView tv = (TextView)findViewById(R.id.textView2); 
    tv.setText(name + " " + number); 
} 
+0

不是我真正想要的+現在它只是崩潰。我想要的 - 如果我點擊包含字符串「John」的listView上的項目,它應該在第二個活動中顯示「John」 –

+0

使用您的代碼片段編輯答案。 現在,您仍然需要在該位置用'actual item'替換DB2.COLUMN_TXT(通過查詢遊標)。 –

+0

也許我應該編輯我的答案,並在那裏添加數據庫代碼? –