how can i go from one fragment to another fragment? - android

I want to go from one fragment to another fragment via click on a widgets.
I used this code in the main activity and used onclick in wedget property:
public void goOther(View v) {
Intent go_To_FragmentTwo = new Intent(this, FragmentTwo.class);
startActivity(go_to_FragmentTwo);
}
please with sample code. and please introduce some articles.

You would need to do the following:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ID = ((TextView) view.findViewById(R.id.pid)).getText().toString();
Intent intent = new Intent(view.getContext(), EventDetails.class);
intent.putExtra(pid, ID);
view.getContext().startActivity(intent);
}
The code below is actually also passing the id of the selected item from one fragment to another, to then display the details according to the value in the ID.
Hope this helps :)

Related

how to send intent.putExtra("getid",id) without displaying it in listview custom adapter?

i am receiving JSON string which contains id, and other string, now i want to pass id to another activity without displaying id in listview, id should be pass when i click specific icon then that icon's id should be pass from activity(A) to another actvity(B)
here is some my code just took relevant part
id = jsonObject.getString("id");
platformno = jsonObject.getString("platform");
listitem.add(new Latest_list(icon,name,date,id,platformno));
here above i no want to display id in listview
String id=Integer.toString(listitem.get(arg2).getNews_id());
Intent intent = new Intent(getApplicationContext(), Webview_news.class);
intent.putExtra("getid",id);
Toast.makeText(LatestNews.this, "platformno="+strPlatform+", id="+id, Toast.LENGTH_LONG).show();
startActivity(intent);
Your question is unclear. Depending on what type of value, here i have an example with String in an ArrayList<String> arrList.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked on an item, pass it to another activity
Intent i = new Intent(this, AnotherActivity.class);
i.putExtra("key", arrList.get(position));
startActivity(i);
}
});

new intent after click callback

I have a listview and when it clicks on a certain option I want it to create a new intent. Like a menu item. This is my first time working with list views, so I'm probably just missing the right way to do this, but is there an easy fix?
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.mainlist);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
TextView textView = (TextView) viewClicked;
Toast.makeText(main.this, position, Toast.LENGTH_SHORT).show();
if(textView.getText().toString() == "my Profile"){
Intent intent = new Intent(this.getApplicationContext(), myProfile.class);
startActivity(intent);
}
}
});
cannot resolve method 'getapplicationcontext'
You can't use this.getApplicationContext() since you're inside a new AdapterView.OnItemClickListener() and there,this doesn't have that method.
You should create a class field containing the application context and use it or use viewClicked.getContext() as suggested by #codeMagic.
Also, as you might have been read in the comments, you should use .equals for string comparison. The == operator is not enough.
Try -
Intent intent = new Intent(MainActivity.this, myProfile.class);
startActivity(intent);
Where MainActivity is he activity where you implemented onItemClick().
You should replace it by your activity name.

How do i get the rowID of a list item in another activity class?

In my main activity I have a list view of database items, when i long press on an item in a list it opens into a new activity that allows you to edit the item clicked.
The problem I'm having is that i'm not quite sure how to get the information of the rowID/primary key to the other activity java class so that i can start manipulating in that class e.g. set the initial values as the current row information and then being able to apply an update to it.
Incase this is of use to helping you help me here is what i have to take me to my new activity
dialog.setNegativeButton(R.string.dialogEdit,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
long id = idInDB;
Intent intent = new Intent(MainActivity.this, edit_expense.class);
startActivity(intent);
}
});
onClick send the row id using putExtra
intent.putExtra("rowid", rowid);
On second activity get the extra string in onCreate()
String rowid = getIntent().getExtras().getString("rowid");
I'm assuming id is the id you want to pass to the other activity, use Intent extras to pass it.
intent.putExtra("key", id);
and to get the value in the other activity:
Intent intent = getIntent();
long id = intent.getLongExtra("key", -1);
Intent reference: https://developer.android.com/reference/android/content/Intent.html
Hope fully in your list row you have a textView that contains the ID.
So when you load the listView, every row will add its own unique ID in the textView. Now when you click/hold the any specific listItem, it will trigger the click event and will retrieve the ID into the String rowID. Now you can pass this ID using intent.
list.setOnLongClickListener(new AdapterView.OnLongClickListener() {
#Override
public void onLongClick(AdapterView<?> adapterView, View view, int i, long l) {
String rowID =((TextView) view.findViewById(R.id.rowID)).getText().toString();
Intent nextActivity = new Intent();
nextActivity.putExtra("id",rowID);
startActivity();
}

Displaying ListView Items in another activity thruough onItemLongClick method

How to display the details of any of the items clicked in a listview in another activity?
I referred to this. But unfortunately couldn't understand as to how to go about in displaying the details. Could anyone please help me on this?
you can use putExtra of intent and can pass any value or any serialized class object
yourActivity.java
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Intent intent = new Intent(yourActivity.this,item.class);
intent.putExtra("item",parent.getSelectedItem().toString);
startActivity(intent)
}
item.java
Intent intent = getIntent();
String itemText = intent.getStringExtra("item");
textView.settext(itemText);
hope u got the point..:

Implementing viewpager in a list

How to implement an ExpandableList in a ViewPager in Android?
My question is the exact opposite of the above question, I want to display a list view (preferably an expandable one) and when an item is clicked I want display a bunch of images (different ones for each item of course).
P.S: Please dont tell me to launch a different activity for each list item.
This is some possible guidance not solution. (I am not exactly clear on what the problem is)
I would probably just create one activity. Each time you get a click on the listview it creates a intent with some stored flags. Then you start the activity with that intent. Unpackage the intent to find out what you want you activity to do.
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(this, myclass.class);
intent.putExtra(KEY, v.getId())
intent.putExtra(KEY2, position)
intent.putExtra(KEY3, id)
startActivity(intent);
}
};
And then get the intents flags in the new activity onCreate()
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
resId = intent.getIntExtra(KEY, 0);
...
super.setTitle(title);
Not sure if that what you were looking for hope it helps

Categories

Resources