Reputation: 351
I'm still following a couple of tutorials in order to learn Android Development, I am struggling to open new activities. I can open only one activity, in fact all items on my ListView
open this one activity, which would be great for ListView
item in position 0 (the first item) not all of them.
My code below:
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext, MyActivity1.class);
mContext.startActivity(intent);
}
});
return view;
}
As you can see MyActivity1.class opens fine, but what if I had a second class called MyActivity2 which should be opened up by listview item in position 1 (second item), how would I do that? Also what does (View arg0) mean? I can't find proper explanation on this?
Upvotes: 0
Views: 164
Reputation: 14847
public void onClick(View arg0) {
onClick
is called when you click on something, and arg0
(you could call it view
, arg0
is so useless as name) is what you clicked.
This callback is often used in Buttons
, Views
in general but cannot be used with ListView
.
If you want to open something when an item in the listview is clicked, you should use setOnItemClickListener
!
On AdapterView.OnItemClickListener
you have AdapterView<?> parent, View view, int position, long id
arguments.
From android doc:
When you write your onItemClick
you should remember you are in an anonymous class!
So you cannot use this
when you need to pass your context to new Intent
pastesList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = null;
switch (position)
{
case 0:
intent = new Intent(ActivityClass.this, Class1.class);
break;
case 1:
intent = new Intent(ActivityClass.this, Class2.class);
break;
default:
return;
}
startActivity(intent);
}
});
This line
ActivityClass.this
is used when you need to use the instance of the class ActivityClass
(You should replace ActivityClass
with your class name) but you cannot access it directly using this
which here refer to new AdapterView.OnItemClickListener()
In your code you use mContext
i don't know what is it, but i suppose it's a context.
Next time you don't have this variable, follow my advise if you want.
Upvotes: 1
Reputation: 133560
Use listview OnItemClickListener
listView.setOnItemClickListener( new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
switch(positon)
{
case 0:
Intent intent = new Intent(mContext, MyActivity1.class);
mContext.startActivity(intent);
break;
case 1:
Intent intent = new Intent(mContext, MyActivity2.class);
mContext.startActivity(intent);
break;
}
}
});
position
is the index of list item. so based on the index you can start the respective activity.
The other way without switch case
String[] classes ={"MyActivity1","MyActivity2"};
Then in onItemClick
String value = classes[position];
Class ourClass =Class.forName("packagename."+value);
Intent intent = new Intent(mContext,ourClass);
mContext.startActivity(intent);
Upvotes: 1
Reputation: 5368
If my understanding is correct, then here is your answer:
Implement OnItemClickListener
to your main activity:
then set listener to your listview: listView.setOnItemClickListener(this);
finallly :
private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch(positon)
{
case 0:
Intent intent = new Intent(mContext, MyActivity1.class);
mContext.startActivity(intent);
break;
case 1:
Intent intent = new Intent(mContext, MyActivity2.class);
mContext.startActivity(intent);
break;
}
}
};
Upvotes: 1
Reputation: 22972
You can use Itemclick listner.
listviewOBJ.setOnItemClickListener( new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
}
});
here you can use int position
to decide which item is clicked.
Upvotes: 1
Reputation: 86
lv.setOnItemClickListener(n## Heading ##ew OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
if(position == 1) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), Html_file1.class);
startActivityForResult(myIntent, 0);
}
if(position == 2) {
//code specific to 2nd list item
Intent myIntent = new Intent(view.getContext(), Html_file2.class);
startActivityForResult(myIntent, 0);
}
}
});
Upvotes: 0
Reputation: 30794
What you want to use is an AdapterView.OnItemClickListener
.
final ListView list = ...;
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(mContext, MyActivity1.class);
if (position == 1) {
intent = new Intent(mContext, MyActivity2.class);
}
startActivity(intent);
}
});
Upvotes: 0