I would like to create a list of androidPlot, and when I click on one item i would like to start another activity.
Now the List of Plot is done, but the listener doesn'work.
I try to do this:
mPlotList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent=new Intent(getApplicationContext(),Activity.class);
startActivity(intent);
}
}
what is the problem?
Thank you to all!
you should set this:
mPlotList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// position means item that clicked
Intent intent=new Intent(getApplicationContext(),Activity.class);
startActivity(intent);
}
});
mPlotList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// position means item that clicked
Intent intent=new Intent(getApplicationContext(),Activity.class);
startActivity(intent);
}
});
Related
I am trying to send an item position from a Fragment to an Activity, but I always get invoiceId 0 every time I select an item in listView.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent seperateView = new Intent(rootView.getContext(),SeperateViewForDeliveryList.class);
seperateView.putExtra("invoiceId", listView.getItemAtPosition(position).toString());
startActivity(seperateView);
}
});
listView.setAdapter(deliveryListAdapter);
return rootView;
In activity class
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seperate_view_for_delivery_list);
invoiceId = getIntent().getExtras().getString("invoiceId");
Toast.makeText(this, "invoiceID " + invoiceId, Toast.LENGTH_SHORT).show();
}
Use this
seperateview.putExtra("invoiceId",String.valueOf(position));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent seperateview=new Intent(rootView.getContext(),SeperateViewForDeliveryList.class);
seperateview.putExtra("invoiceId",parent.getItemAtPosition(position).toString());
startActivity(seperateview);
}
});
listview.setAdapter(deliveryListAdapter);
return rootView;
Try this
getItemAtPosition returns an object of the data associated with the specified position in the list, while position itself is what you want.
Change your code to
seperateView.putExtra("invoiceId", String.valueOf(position));
We can also pass integer value in an Intent. Try this:
seperateview.putExtra("invoiceId",listview.getItemAtPosition(position));
getIntent().getIntExtra("invoiceId",0);
This is My Main Listview `
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
intent.putExtra("position", i);
startActivity(intent);
}
});
This is code i am using for item position`This is my 2nd Listview its open when i am clicking on main ListView But This opens in all position and i want to open this 2nd listview on 0th position of main(1ST) Listview
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
if(i == 0){
intent.putExtra("position", i);
startActivity(intent);
}else {// do whatever your stuff
}
}
});
In Second Activity
int position = getIntent().getIntExtra("position", 0); // 0 will be default value if there is no value in bundle
If you want to open second listview only when you click on 0th position then use following code.
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
if(i == 0){
intent.putExtra("position", i);
startActivity(intent);
} else {
//do your stuff for other position.
}
}
});
I have created one listview of some names,what i need is when i will click selected row it will go to that page only,on click on different row it will move to the same class but different content.I think it will move by question id.could anybody help me how to pass the question id Or any other method to do this..
here is my code..
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
};
You can try something like this -
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if(Some condition)
{
Intent i= new Intent(YourActivity.this,ActivityOne.class);
// To pass data
i.putExtra("SomeId", someValue);
startActivity(i);
}
else if(Some other condition)
{
Intent i= new Intent(YourActivity.this,SecondActivityTwo.class);
startActivity(i);
}
else
{
// Do something else--
}
}
};
And in the other activity -
String identifier = getIntent().getExtras().getString("SomeId");
Here I've given an example assuming you have user list and clicking on item you want to show user profile...
In List_Act activity...
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.rowitem,parent,false);
convertView.setTag(UserId);
}
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i=new Intent(List_Act.this, Profile_Act.class);
int UserId = ((View)v.getParent()).getTag();
i.putExtra("UserId", UserId); //Setting variable you want to pass to another activity
startActivity(i);
}
};
in Profile_Act activity in onCreate()
String UserId = getIntent().getExtras().getString("UserId"); //retrieving value in another activity
now you'll have UserId variable set and you can use it...
i am creating an application with a listview and what i want to happen is when you choose an item from the listview the application will automatically move you to the next screen. Is this possible to do?
Thanks
have a look at setOnClickListener() of your ListView. then you can use startActivity() to skip to the next screen/activity.
ListView lv = (ListView)findeViewById(...);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int index, long arg3) {
// index = the index of the clicked element in the listview
// create an intent and add index to its data
startActivity(intent);
}
});
Have a look at onItemClick and use startActivity() to bring up a new screen.
For listening the list item click events you need to override onItemClickListener method .
See here :
mylist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent=new Intent(getApplicationContext(),NextActivity.class);
//If you want to pass parameters/values to NextActivity use putExtra
intent.putExtra("my_data","my_values");
startActivity(intent);
}
});
try this code..it will helpful for u..
"list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent=new Intent();
intent.setClass(nextScreen.this,profile_details.class);
intent.putExtra("profile", mArray.get(mPosition));
startActivityForResult(intent,0);
}
});"
use this code.....
I created a ListView with bunch of items, but I am not actually sure how to implement a listener on a specific items. Please help me! I would realy applreacte it a lot.
I tried to use this code, but when I click on any items, it will only bring me to the same Activity.
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id)
{
Intent i = new Intent(MainActivity.this, Activity2.class);
startActivity(i);
}
});
But if I use this code with if statement, nothing is happening:
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id)
{
if(list.getItemAtPosition(position).equals(mStrings[0]))
{
Intent i = new Intent(MainActivity.this, Activity2.class);
startActivity(i);
}
}
});
You could base your condition on position itself:
if (position == 0) {
... start activity ...
}
Maybe set different listeners in the getView method of the adapter>