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.
Related
I have a list view with data base and when i click on an item on the list i want to move to edit page activity in order to update the item.
What am i doing wrong?
There is my code:
//by pressing a short press on any item the user moved to the edit page in order to edit his note
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
intent=new Intent(MainActivity.this,Edit_Note.class);
intent.putExtra("item_id", list.get(position).getId());
intent.putExtra("position", position);
intent.putExtra("item_title", list.get(position).getTitle().toString());
intent.putExtra("item_content", list.get(position).getContent().toString());
startActivityForResult(intent, FLAG_FOR_EDITING);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==ADD_FLAG && resultCode==RESULT_OK ){
//i am getting the information (title and content) from the edit note activity
String the_title=data.getStringExtra("user_title");
String the_content=data.getStringExtra("user_content");
//the information is added to the database
database.addNote(the_title,the_content);
}
else if(requestCode==ADD_FLAG && resultCode==RESULT_CANCELED){
Toast.makeText(MainActivity.this, "no changes have been made", Toast.LENGTH_LONG).show();
}
else if
(requestCode==FLAG_FOR_EDITING && resultCode==RESULT_OK){
int position_from_edit=data.getIntExtra("position", -2);
String title_editing=data.getStringExtra("user_title");
String content_editing=data.getStringExtra("user_content");
database.editNote("position_from_edit", title_editing, content_editing);
}
edit_title=(EditText)findViewById(R.id.edit_title);
edit_content=(EditText)findViewById(R.id.edit_content);
ImageButton save=(ImageButton)findViewById(R.id.save);
ImageButton clear=(ImageButton)findViewById(R.id.clear);
String received_title=intent.getStringExtra("item_title");
String received_content=intent.getStringExtra("item_content");
item_id=String.valueOf(intent.getIntExtra("item_id", -1));
edit_title.setText(received_title);
edit_content.setText(received_content);
//by pressing the save button, the information is sending to the main activity page
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//i am receiving the text that the user entered to the title and to the content fields
entered_title=edit_title.getText().toString();
entered_content=edit_content.getText().toString();
intent.putExtra("position", item_id);
intent.putExtra("user_title", entered_title);
intent.putExtra("user_content", entered_content);
setResult(RESULT_OK, intent);
finish();
}
}
When you want to pass so much data through an activity, it is best to bind that data into a bundle. You should store all your data into a bundle and then bind it to the intent and then pass in the activity.
Bundle value=new Bundle;
value.putString("item_title", list.get(position).getTitle().toString());
value.putString("item_content", list.get(position).getContent().toString());
value.putInt("item_id", list.get(position).getId());
value.putInt("position", position);
intent.putExtras(value);
startActivityForResult(intent,0);
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);
}
I am trying to use the OnItemListClick method of ListActivity to launch a new activity with more specific information about the list item. To do this I am using a bundle as advised by the top answer of How do I pass an object from one activity to another on Android? but no matter what I try the OnItemListClick doesn't seem to do anything. I also tried without passing the bundle thinking that was causing a problem as advised here Using an intent to start new activity from ListActivity and that also does nothing onClick. How am I supposed to make it so when an item in the listview is clicked it returns the object associated with that row and passes it to a new activity?
public class TaskList extends ListActivity {
private ArrayList<Task> stuffToDo, completed;
private TaskAdapter t_adapter;
public Button add;
private File saveFile;
private Bundle clickBundle = new Bundle();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_list);
stuffToDo = new ArrayList<Task>();
completed = new ArrayList<Task>();
t_adapter = new TaskAdapter(this, R.layout.row_rl, stuffToDo);
setListAdapter(t_adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Make a bundle and put the Task selected in it then pass this to the
// intent to start the new activity
Intent launchIntent = new Intent(getApplicationContext(), TaskDisplay.class);
Log.d("Debug", "Intent made");
clickBundle.clear();
clickBundle.putSerializable("Task", stuffToDo.get(position));
Log.d("Debug", "Task put in bundle");
launchIntent.putExtras(clickBundle);
this.startActivity(launchIntent);
Log.d("Debug", "launched");
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.addtaskbutton:
promptUserForInfo();
t_adapter.notifyDataSetChanged();
// added for debug
saveInfo();
return true;
case R.id.cleartasks:
clearTasks();
t_adapter.notifyDataSetChanged();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
and my code that is in the more specific activity
// Set the bundle equal to the extra put with the intent
input = this.getIntent().getExtras();
// If there are extras(a task) then assign it to the empty task variable
if (input != null){
task = (Task) input.getSerializable("Task");
}
here this mean you are pointing towards list.
change Intent launchIntent = new Intent(this, TaskDisplay.class);
to Intent launchIntent = new Intent(youractivityname.this, TaskDisplay.class);
or to Intent launchIntent = new Intent(getApplicationContext(), TaskDisplay.class);
and make this super.onListItemClick(l, v, position, id); first line of list item click method.
Try this...
this.startActivity(launchIntent);
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");
}
My requirement is : List all sales route & highlight default, It should be first option. If the salesperson select other option(not selected default), then popup window should come. The popup window contain one form, it talk to DB.After submit the popup window form , it return previous screen , It need to allow to go other position.
See my code
This initial list Activity:
ArrayList<Object> routeList = getWmRoute();
ArrayList<String> routhPath = new ArrayList<String>();
for(int i = 0; i<routeList.size();i++){
routhPath.add(((WMRoute) routeList.get(i)).getDescription());
}
ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice,routhPath);
setListAdapter(ad);
final ListView list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setItemsCanFocus(true);
list.setTextFilterEnabled(true);
list.setItemChecked(0,true);
list.setSelection(0);
This is listener method
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(position !=0 ){
Bundle bundle = new Bundle();
int postion = position;
String aString = Integer.toString(postion);
bundle.putString("positon", aString);
Intent showContent = new Intent(getApplicationContext(),SalesRouteDevitionActivity.class);
// startActivityForResult(showContent,0);
startActivity(showContent);
}
}
This is my SalesRouteDevitionActivity class
array_spinner=new String[2];
array_spinner[0]="Rain";
array_spinner[1]="Floods";
Bundle bundle = this.getIntent().getExtras();
param1 = bundle.getString("param1");
Spinner s = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, array_spinner);
s.setAdapter(adapter);
final Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(SalesRouteDevitionActivity.this, "Beep Bop", Toast.LENGTH_SHORT).show();
Intent showContent = new Intent(getApplicationContext(),SalesRouteActivity.class);
Bundle bundle = new Bundle();
bundle.putString("position", param1);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
startActivity(showContent);
}
});
This is my manifest file
<activity android:theme="#android:style/Theme.Dialog" android:name=".SalesRouteDevitionActivity"
android:label="Sales Route Diviation">
</activity>
After finish pop window work , how we can go to previous activity that particular place?
Please help me...
Thanks in advance
from wherever you start the popup window call it using intent throught call this startActivityForResult(your_intent,requestCode)
it will start the activity and in the popup activity do like this way
Bundle bundle = new Bundle();
bundle.putString("position", param1);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
so the popup will finish his activity and go back to previous activity where it will invoked with the result
and in your activity override the onActivityResult like this way
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==1){
if(resultCode==RESULT_OK)
Toast.makeText(this, "SUCCESS", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
}
}
You could create a global variable for the position of the selected item. Then when you return to the previous activity, you call listView1.smoothScrollToPosition(int position).
Do you want listview focus on a position which is not default?If so, you only need return intent with position and at onActivityForResult() you set focus at that position.If you have any problems, please add my yahoo or skype:fsoft_duonghv and we discuss .
Instead of starting a new activity for a popup window you can create a AlertDialog. You can call dismiss() to return your activity then.