Reputation: 93
I want to send string values from item of my list view to second activity. But second activity doesn't get the value. What should I do in order to send needed information? And is it the right way to get needed item's ID? Please let me know. 1st activity code
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();
}
2nd activity code
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);
}
Upvotes: 1
Views: 84
Reputation: 925
Use Intent
instead of SharedPreferences
to bundle your string
and pass it to the Activity2
.
Either create a Bundle
object and put your string in the Bundle
object or directly put your string in the Intent
(which will implicitly create a bundle for you)
Method 1
In 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);
}
});
In 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);
}
Upvotes: 3