How to pass item Click Position in Listview - android

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.
}
}
});

Related

onlistitemclick in AppCompatActivity

I followed steps mentioned in this answer to set a toolbar for a listview [listview image][1].
Now the list items are not clickable. When using ListActivity list items were clickable, when clicking on any item it will open another activity with item title and its content.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Note note = posts.get(position);
Intent intent = new Intent(this, EditNoteActivity.class);
intent.putExtra("noteId", note.getId());
intent.putExtra("noteTitle", note.getTitle());
intent.putExtra("noteContent", note.getContent());
startActivity(intent);
}
Do this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Note note = posts.get(position);
Intent intent = new Intent(this, EditNoteActivity.class);
intent.putExtra("noteId", note.getId());
intent.putExtra("noteTitle", note.getTitle());
intent.putExtra("noteContent", note.getContent());
startActivity(intent);
}
});

How to transfer the clicked item in listview to editText in another activity in android

I want to get the item on list view to edit Text in another activity.
When clicked on list view item, I want to transfer the item in another activity in edit Text.
You have to make onItemClickListner of listview like that.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
}
});
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
And finally set Value to editText like this
editText.setText(value);
Hope this will help you.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("text", text want to transfer);
startActivity(intent);
}
});
You can make use of SharedPreferences. And when you pass the content of the ListView to the next activity, you can use editText.setText("Your Text").
You can also pass your data through intents from which you are calling your new activity.
create onClick method like this.
ListView list = (ListView) findViewById(R.id.newsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {
NewsItem item = (NewsItem) adapter.getItem(position);
Intent intent = new Intent(getApplicationContext(), NewsDetailsActivity.class);
intent.putExtra(KEY, item.getHeadline());
startActivity(intent);
}
});
In next activity
Intent intent = getIntent();
headline = intent.getStringExtra(KEY);
have a look here

onItemClick() doesn't work with a Custom ListView

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);
}
});

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;

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