Android list view on click - android

I have a list view with 15 items. When I click on any item I want to change the screen(Intent). how can I change the activity on item selected in android?
any tutorial or source code?

You can use ListView's setOnItemClickListener, and start an new Activity in your implementation of this method. Following is sample code:
myListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id){
// Start your Activity according to the item just clicked.
}
});

final ListView list = (ListView) findViewById(R.id.SCHEDULE);
protected void onCreate(Bundle savedInstanceState) {
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
Toast.makeText(getApplicationContext(),"hiihih",Toast.LENGTH_SHORT).show();
}
});
}

Check the selected answer in ListView OnItemClickListener Not Responding?
If you also need code examples to change activity, head to https://developer.android.com/guide/index.html and start reading.
// Prepare intent
Intent newActivity = new Intent(this, NewActivity.class);
// start activity
startActivity(newActivity);

Related

search item position

I am developing an Android application in which I am using a listview and search bar. When I click on any searched item it will show starting items, not the clicked one.
Can anyone help me how to get the position of selected item?
You can use this for getting position of your selected Item
YourList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SelectedId = YourList.getItemIdAtPosition(arg2);}});
- Use getItemIdAtPosition() method.
Eg:
ListView lv = (ListView) findViewById(R.id.listView_ProductList);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
i = new Intent(ProductActivity.this, ProductDetails.class);
// i.putExtra("keyAapo", arg2 );
i.putExtra("keyAapo", (int) lv.getItemIdAtPosition(arg2));
startActivity(i);
}
});
First, while clicking the listview item,
get the position of that item in listview
Pass the strings/values to that intent if any.
Intent for navigating to next class as below:
YourListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor listItem = (Cursor)<YourListView>.getItemAtPosition(position);
String lID = listItem.getString(1);
Intent i= new Intent(<ClassName>.this, <Navigate to Class>.class)
i.putExtra("lID", lID); // key-value pair
startActivity(i);
}});

Android: How to start new activity for OnItemClick of List View that uses content provider

By using the content provider I have inflated SMS in Inbox List View of my application. Now On item Click I want to show SMS text in another activity. I've implemented custom list view. Now I am not getting how to pick each single list item and show in new activity on click. In a Stack flow answer somebody suggested this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri mailUri = ContentUris.withAppendedId(getIntent().getData(), id);
startActivity(new Intent(Intent.ACTION_VIEW, mailUri));
}
If this code is correct then how I will configure next activity to act upon this ACTION_VIEW?
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int pos, long arg3) {
Intent i= new Intent(currentClass.this,secondActivity.class);
i.putExtra("string",Yourlist.get(pos).sms);
startActivity(i);
finish();
}
});
& on Another Activity You can receive through this:-
String msg=getIntent().getExtras().getString("string");
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{ case 0:
Intent firstIntent = new Intent(AndroidListViewActivity.this, SingleListItem.class);
startActivity(firstIntent);
break;
case 1:
Intent secondintent = new Intent(AndroidListViewActivity.this,jokes.class);
startActivity(secondintent);
break;

set onClick or onItemSelected for item in list view

I have a requirement that, in my list view I am having an image icon, a text view and a button.
when I click on any one of these it should move me to next screen. I tried implementing these methods, but no success.
Can anybody help me?
Note:My class does not extends ListActivity rather only Activity.
Here is my code....
ListView listView = (ListView)findViewById(R.id.listView1);
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Intent in2 = new Intent(City.this, TourDescription.class);
startActivity(in2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
You need to set OnItemClickListener, not OnItemSelectedListener

Switching between screens on a click

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.....

how to implement a listener on specific items using ListVIew?

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>

Categories

Resources