我想從我的列表視圖的項目發送字符串值到第二個活動。但第二項活動沒有得到價值。我應該怎麼做才能發送所需的信息?這是獲得需要物品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);
}
我應該添加我的DB代碼嗎? –