how to call a java class from OnItemClick()? - android

I have a problem to call a java class from OnItemClickListener widget..
private OnItemClickListener detailClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent productDetailsIntent = new Intent(getBaseContext(),MenuRestaurantActivity.class);
startActivity(productDetailsIntent);
}
};
Parameter in currentActivity isn't send to anotherActivity class..
How to fix it?
thanks for your attention..

you must declare following lines in your AndroidManifest.xml file, otherwise it will generate an error.
<activity android:name=".MenuRestaurantActivity" android:label="#string/app_name">
</activity>

Instead of this in your onclick
productDetailsIntent = new Intent(getBaseContext(),MenuRestaurantActivity.class);
use this line
Intent intent = new Intent(currentActivity.this, NextActivity.class);
startActivity(intent);
In FirstActivity.java file onclick button you should use below code.
Intent i1 = new Intent(firstactivity.this, secondactivity.class);
i1.putExtra("type", "edit");
startActivity(i1);
In secondActivity.java file oncreate .. use this code.
Bundle extras = getIntent().getExtras();
Strinjg Value = extras.getSerializable("type").toString();

private OnItemClickListener detailClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent productDetailsIntent = new Intent(ActivityName.this,MenuRestaurantActivity.class);
startActivity(productDetailsIntent);
}
};

Related

In Fragment, click on listView item to open new activity and pass value to other activity

In Fragment, click on listView item to open new activity and pass value to other activity
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
sowing sowing=new sowing();
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.FragmentContainer,sowing,sowing.getTag())
.addToBackStack("fragBack").commit();
String selectedFromList =(list.getItemAtPosition(position).toString());
}
});
What you are looking for is Bundles
Here is an example of how you'd use it in your case:
list.setOnClickListener(new AdapterView.onItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
sowing sowing = new sowing();
FragmentManger fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.FragmentContainer, sowing, sowing.getTag())
.addToBackStack("fragBack").commit();
String selectedFromList = (list.getItemAtPosition(position).toString));
// THE BUNDLE
// start a new intent to open the activity
Intent newIntent = new Intent(Context, Activity.class);
newIntent.putStringExtra("NAME_OF_BUNDLE", selectedFromList); // I used putStringExtra because you defined 'selectedFromList' to be a string
startActivity(newIntent);
}
});
Now in the activity you opened just call the name of the bundle like so:
Intent intent = getIntent(); // get the intent that caused this activity to be opened
String selectedFromLastActivity = intent.getStringExtra("NAME_OF_BUNDLE"); // from the intent that caused this activity to be opened, get any extras passed through
The best way is to use SharedPreferences.
https://developer.android.com/reference/android/content/SharedPreferences
https://developer.android.com/training/data-storage/shared-preferences

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.

Possible to pass intent extra from custom adapter to activity to another custom adapter?

So im trying to figure out if I can pass a intent extra from a custom adapter to an activity which then passes that intent extra to another activity?
Is something like that possible or is there a better way to do what I want to do?
Activity A:
Intent intent = new Intent(A.this, B.class);
intent.putExtra("someString", "string");
startActivity(intent):
Activity B:
onCreate(...) {
String myString = getIntent().getStringExtra("someString");
MyAdapter myAdapter = new MyAdapter(B.this, myString);
}
MyAdapter :
Context myContext;
String myString;
MyAdapter(Context context, String string) {
this.myContext = context;
this.myString = string
}
Now you have the String from activity A into your adapter :)
I assume, that you know how to send intent extras to an activity. And what you are looking for is a way to forward that same intent to another activity. You can do it like this:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtras(getIntent());
All the answers mentioned above would work. What I ended up using was an onItemClickListener on my listview and got the information that way. Instead of starting the intent in the custom adapter, i started the intent in the activity the custom adapter was in.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is where you get the item clicked and specifically what you want from the custom adapter.
String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString();
Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class);
goToNewActivity.putExtra("yourExtra", yourString);
startActivity(goToNewActivity);
}
});
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is where you get the item clicked and specifically what you want from the custom adapter.
String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString();
Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class);
goToNewActivity.putExtra("yourExtra", yourString);
startActivity(goToNewActivity);
}
});

Error :Intent(new AdapterView.OnItemClickListener(){}, Class<ProductListActivity>) is undefined

I am developing an android application and use Gridview to display 16 Text View Controls
When user clicks a particular Text View control I need to open another activity
My Gridview item click event as follows
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(this, ProductListActivity.class);
} });
But this makes an error and says “The constructor
Intent(new AdapterView.OnItemClickListener(){}, Class<ProductListActivity>) is undefined”
Could someone please help me to correct this ?
your code should be:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(activity_name.this, ProductListActivity.class);
} });
You need to pass the context as first argument. In your case YourActivity.this.
send getApplicationContext or yourActivity.this as the first parameter in your intent. Hope this solve your issue.
gridview.setOnItemClickListener(new OnItemClickListener() {
switch (position)
{
case 1:
Intent intent = new Intent(YourActivity.this, ProductListActivity.class);
break;
case 2:
Intent intent = new Intent(YourActivity.this, AnotherActivity.class);
break;
default: // default.....
break;
}});
Handle your activities with switch case....

After ListView onClick,change activity

Here's my code
this.getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(this,lastview.class);
startActivity(i);
}
});
the "this" is a ListActivity,but I want the next activity is a normal activity
and my code is wrong in this line
Intent i = new Intent(this,lastview.class);
the wrong message is
The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<lastview>) is undefined
how can I fix it ?
change this line
Intent i = new Intent(this,lastview.class);
like this, change your activity to MyListActivity
Intent i = new Intent( MyListActivity.this, lastview.class);
Here is the sample code .This will help you.
public class YourListActivity extends ListActivity {
private Context context;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
context =this;
.....
......
this.getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(context,lastview.class);
// else you can use the code like below
//Intent i = new Intent(YourListActivity.this ,lastview.class);
startActivity(i);
}
});
}
You can also write it like :
startActivity(new Intent(Yourclass_name.this,lastview.class));

Categories

Resources