Passing ContactsContract.Data.DISPLAY_NAME between Activities - android

I am having trouble with passing data between two activities within my application. My ActivityOne populates a list of contacts from the phone. The user is then able to select a contact which will take them to ActivityTwo. Here is how I have written the Intent to go from ActivityOne to ActivityTwo after a contact is selected:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(v.getContext(), ActivityTwo.class);
i.putExtra("name", ContactsContract.Data.DISPLAY_NAME);
startActivity(i);
}
In ActivityTwo's onCreate() method, I have included this code in order to restore the data that was passed:
Bundle extras = getIntent().getExtras();
if (extras != null) {
name = extras.getString("name");
}
ActivityOne properly populates the contact list with the correct names, but when I try to use the name that is passed to ActivityTwo, it defaults to display_name instead of the contact's actual name.
Is there something that I've done wrong or something that I am overlooking?

Could you try this code and feed me back ?
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
String name = ((TextView) v.findViewById(android.R.id.text1)).getText().toString();
Intent i = new Intent(v.getContext(), ActivityTwo.class);
i.putExtra("name", name);
startActivity(i);
}

Related

how can i receive intent on 2 activities?

I want to receive intent on the two activity classes, but not sure how since startActivity can only start one intent to only one activity class
private void populateListView()
{
Log.d(TAG, "Populating listview");
Cursor data = dbManager.getTitle();
final ArrayList<String> listTitle = new ArrayList<>();
while (data.moveToNext())
{
listTitle.add(data.getString(1));
}
final ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listTitle);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
String name = adapterView.getItemAtPosition(position).toString();
Cursor data = dbManager.getTitleID(name);
int titleID = -1;
while (data.moveToNext())
{
titleID = data.getInt(0);
}
if (titleID > -1)
{
Intent intent= new Intent(List_Title_Activity.this, MainActivity.class);
//Intent intent2= new Intent(List_Title_Activity.this, ViewListContents.class);
intent.putExtra("id", titleID);
intent.putExtra("title", name);
startActivity(edit);
}
else
{
Toast.makeText(List_Title_Activity.this, "No id associated with that name", Toast.LENGTH_SHORT).show();
}
}
});
MainActivity and ViewListContent:
Intent receiveIntent = getIntent();
selectedID = receiveIntent.getIntExtra("id",-1);
1 Activity is 1 page in android. That is, you can call/view 1 page/Activity at a time. If you want to share same data to multiple pages in use shared preference / Database. Or if you are using 1 activity and multiple fragments in it, use interface.
if you want to send data from one activity to 2nd activity you can use simple intent and pass data from first activity to second . But if you want to send the data first activity to second and get data from second activity to first activity i suggest you to use startActivityForResult. for more see here

Launching an activity on listitem click

I have created a list view something like this: http://imgur.com/a/bhWhR in activity1
Onitemselect i want to launch another acitivity which shows description about listitem1 like below
http://imgur.com/a/agGma
but did not find any helpful material to create and activity with same layout to get different content like this one ::
I did explore for quite some time and I could not find any solution that helped me .
Use OnItemClickListener and use intent to open other activities on item clicks
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 1) {
Intent intent = new Intent(MainActivity.this, Lu1Activity.class);
startActivity(intent);
} else if (i == 2) {
Intent intent = new Intent(MainActivity.this, Lu2Activity.class);
startActivity(intent);
} else if (i == 3) {
Intent intent = new Intent(MainActivity.this, Lu3Activity.class);
startActivity(intent);
} else if (i == 4) {
Intent intent = new Intent(MainActivity.this, Lu4Activity.class);
startActivity(intent);
}
}
});
If you want to use same 2nd activity for each intent and show different content then use a global variable to know which item is selected and change content of 2nd activity as you wish.
Add a variable like this
public static int itemNumber;
Use OnItemClickListner
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
itemNumber = i;
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
In OtherActivity, Set different content to your views depending on itemNumber's value.
Note: You can also use putExtra to extend data to the intent and use that to determine which item is clicked.

Android finishing activity not working

Once the user chooses a product from my ListView, it then puts the selected text from that ListView into an EditText. The problem I am having is when the user selects a product from the list, and then presses back, it comes up with the list again instead of returning to the EditText activity.
I have tried using "finish();" after the activity starts but nothing seems to be working.
Activity that holds the EditText that launches the List activity:
EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
CPU.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent CPUList = new Intent(getApplicationContext(),
CPUList.class);
startActivityForResult(CPUList, 1);
Intent i = getIntent();
String product = i.getStringExtra("key");
EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
CPU.setText(product);
}
});
List view class
#Override
public void onCreate(Bundle OnsaveInstanceState) {
super.onCreate(OnsaveInstanceState);
setContentView(R.layout.activity_cpulist);
ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
String CPUList[] = {
"CPU's go here", "CPU's go here", "CPU's go here", "CPU's go here" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, CPUList);
listViewCPU.setAdapter(adapter);
listViewCPU.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish();
}
});
You need to launch your activity in a way that it doesn't get added to back stack.
Here's how you do that: https://stackoverflow.com/a/12358563/375929
If I understand you correctly, you are calling finish() on the wrong Activity. If you want the list Activity to finish then that's where you need to call finish()
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent(getApplicationContext(),
ListmenuActivity.class);
i.putExtra("key", CPU);
startActivity(getIntent());
startActivity(i);
finish(); // finish here
}
and remove finish() from your EditText Activity
Another issue I see is it looks like you are starting that second bit of code with the first using startActivityForResult() but you aren't sending back a result in your second code. Instead, you seem to be starting another Activity. It seems that second bit should be more like
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish(); // finish here
}

Is it possible to change screen, and send text (putExtra), by chosing a list item, and how?

I'm a working on a recipe app, and I was wondering if you can use list items to change screen, send text to next activity, just like you would using a button,
if its possible then how do i do it?
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CURRENT_ACTIVITY.this*, NEW_ACTIVITY.class);
intent.putExtra("keyName", "somevalue");
startActivity(intent);
}
});
in Activity B
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}

Android: Display list items to new view

How do you display the items in list view? I mean, when you clicked on an item of the list view it will display on a new view, just plain textview. Can anyone help me on this? I want to display it in two textviews.
Here's the code I got:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = mMessagesCursor;
c.moveToPosition(position);
Intent i = new Intent(this, KAHTextApp.class);
i.putExtra(MessagesDBAdapter.KEY_ROWID, id);
i.putExtra(MessagesDBAdapter.KEY_RECIPIENT, c.getString(
c.getColumnIndexOrThrow(MessagesDBAdapter.KEY_RECIPIENT)));
i.putExtra(MessagesDBAdapter.KEY_MESSAGE, c.getString(
c.getColumnIndexOrThrow(MessagesDBAdapter.KEY_MESSAGE)));
startActivityForResult(i, ACTIVITY_EDIT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
switch(requestCode) {
case ACTIVITY_CREATE:
String recipient = extras.getString(MessagesDBAdapter.KEY_RECIPIENT);
String message = extras.getString(MessagesDBAdapter.KEY_MESSAGE);
mDbHelper.createNote(recipient, message);
fillData();
break;
case ACTIVITY_EDIT:
Long rowId = extras.getLong(MessagesDBAdapter.KEY_ROWID);
if (rowId != null) {
String editTextRecipient = extras.getString(MessagesDBAdapter.KEY_RECIPIENT);
String editTextNewMessage = extras.getString(MessagesDBAdapter.KEY_MESSAGE);
mDbHelper.updateNote(rowId, editTextRecipient, editTextNewMessage);
}
fillData();
break;
}
So basically, when I clicked on the list view a new activity will come to front showing just the two textviews namely, the recipient and the message.
Listactivity.class
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent intent = new Intent(view.getContext(), yourclasname.class);
intent.putExtra(String name, String[] value);
.
.
.
startActivity(intent);
}
});
Fulldetail.class
Bundle extras = getIntent().getExtras();
if (extras != null) {
String stringTitle = extras.getString(String string);
if (title != null) {
title.setText(stringTitle);
}
What you do with an onclick event is all up to you, so I don't understand why you will get only one textview.
If I click on a listitem in a listview, then I set an OnItemClickListener with an OnItemClick event.
For a simple example check the developers http://developer.android.com/resources/tutorials/views/hello-listview.html
After your edit I see you start a new Activity (KAHTextApp). In this activity you can define what you want to be shown.
You mean starting a new activity when you click?
In this case, just add an OnItemClickListener on your ListView https://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener) that will have the code to open the new activity.
If using a ListActivity, just override the onListItemClicked method and have your code in there.

Categories

Resources