How to display ListView Selected item with ViewPager in android - java

I want to move the selected quotes (text) from the ListView and display that selected item in another activity with ViewPager + Next and Previous button that show the next and previous element inside the ListView...
(Thanks in advance...)
Here is my Code...
lvAmazingQuotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedQuote = amazing_Quotes_List.get(position);
Toast.makeText(AmazingQuotes_Activity.this, selectedQuote, Toast.LENGTH_SHORT).show();
}
});
ScreenShot:

Using this code you can send your selected item, previous item and next item to another activity
lvAmazingQuotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String previousitem= amazing_Quotes_List.get(position-1).toString();
String selectedQuote = amazing_Quotes_List.get(position).toString();
String nextitem= amazing_Quotes_List.get(position+1).toString();
Intent intent = new Intent(context, AnotherActivity.class);
intent.putExtra("previous_item", previousitem);
intent.putExtra("selected_quote", selectedQuote);
intent.putExtra("next_item", nextitem);
context.startActivity(intent);
}
});

Related

How to get data from selected item in GridView?

I want to get data from selected item in GridView.
My app should work like this: open activity where is GridView => GridView load data from server (its working) => when I click an item, app get & send data to server.
Here is Code:
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
UniqueID = UNIQUEID.getText().toString();
POints = POINTS.getText().toString();
DEscription = DESCRIPTION.getText().toString();
AddPoints = ADDPOINTS.getText().toString();
//these two strings are displayed on gridview
Operations(UniqueID, POints, AddPoints, DEscription);
}
});
I have found solution:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ADDPOINTS = (TextView)view.findViewById(R.id.tv_points_grid);
DESCRIPTION = (TextView)view.findViewById(R.id.tv_points_describtion);
UniqueID = UNIQUEID.getText().toString();
POints = POINTS.getText().toString();
DEscription = DESCRIPTION.getText().toString();
AddPoints = ADDPOINTS.getText().toString();
Operations(UniqueID, POints, AddPoints, DEscription);
}
});
Inside the onItemClick() method,
Try parent.getItem(position). Or parent.getItemAtPosition(position)
Hope this was I you were looking for!

how to get values from ListView in onItemClick and fill in ViewPager?

I have a ListView and when the user click on one item , a ViewPager should open with the same title.So if Item 1 was clicked on , a ViewPager page with the same title should open, like in the image(on the left side). Also the pages should be swipe able but they must be in the same row as the ListView like in the image(on the right side). How is it possible to do that? The OnItemClick method I have implemented already but what should I do now?
listview to viewpager screen look
ListviewActiviy.java
public void onItemClick(AdapterView<?> arg0, View arg1, int position
,long id) {
mCursor.moveToPosition(position);
String rtitle = unescape(mCursor.getString(mCursor.getColumnIndex("rtitle")));
Intent i = new Intent(ListviewActiviy.this, ViewpagerActivity.class);
i.putExtra("rtitle", rtitle);
i.putExtra("POS", position);
startActivity(i);
}
ViewpagerActivity.java
public Object instantiateItem(View collection, int position) {
final View view = getLayoutInflater().inflate(R.layout.backgroundd, null);
position = getIntent().getExtras().getInt("POS");
mCursor.moveToPosition(position);
((ViewPager)collection).addView(view, ((ViewPager)collection).getChildCount() > position ? position : ((ViewPager)collection).getChildCount());
final TextView tv=(TextView)view.findViewById(R.id.rhymeviewpagertext);
title = getIntent().getStringExtra("rtitle");
tv.setText(rtitle);
}
Follow this steps
step 1
create new Activity with ViewPager like ViewPagerActivty
step 2
set ItemClickListener to your ListView and pass the clicked item position to your ViewPagerActivty.this using Intent
ListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Intent intent= new Intent(this,ViewPagerActivty.this);
intent.putExtra("POS",position);
startActivity(intent);
Toast.makeText(YourActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
step 3
get the listview clicked item position in your ViewPagerActivty like this
int position=getIntent().getIntExtra("POS",1);
Now display data in your ViewPager based on that position

Spinner onClick listener

I would like to call an action every time my spinner selects a new item. The current setup is that an action is called when a button is pressed.
My approach:
Create a new function: public void onSpinnerItemSelection(View V){...}
Then in content_home.xml I would add onSpinnerItemSelection to onClick for Spinner.
This isn't working. I'm not familiar with Android errors and I'm struggling to interpret these errors.
Unfortunately, myApp has stopped
Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
Step by step:
1ªDeclare variables from the layoutSpinner sp; the arraylist in this case ArrayList listTeams; and the adapter Adapter adapter;
2ªinstances
private void instances() {
sp = (Spinner) findViewById(R.id.spinner);
listaTeam = new ArrayList();
3º add items for the adapter
private void listAdapter() {
listaTeam.add(new Equipo(R.string.Atleti));
adaptador = new Adaptador(MainActivity.this, listaEquipo);
sp.setAdapter(adapter);
now we make actions, use onItemSelected.
public void onItemSelected(AdapterView parent, View view, int position, long id) {
Toast.makeText("this is my team", Toast.LENGTH_LONG).show();
}
https://www.youtube.com/watch?v=H3W0-Nv664I
Here is an example:
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
Toast.makeText("log", "position = " position, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText("log", "onNothingSelected", Toast.LENGTH_LONG).show();
}
});

Android: make a single item in a listview clickable [duplicate]

This question already has answers here:
Android - How to create clickable listview?
(5 answers)
Closed 8 years ago.
I currently have a ListView id=listview, which I am populating with an ArrayList id=myarraylist. Here is how I do it:
final ListView listview = (ListView) findViewById(android.R.id.list);
myarraylist = (ArrayList<String>) getArray();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
myarraylist.add(...And then some other stuff I add...);
//This is the part I want to focus on
myarraylist.add("Reset High Scores");
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
Now, I want the entry Reset High Scores in my ListView to be clickable, and when it is clicked, I want it to do Some Stuff. I know for buttons all you do is set an onClickListener
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
..Some Stuff..
}
});
My question is, how can I make Some Stuff happen when the element Reset High Scores is clicked in the ListView
Edit: This is what I have so far
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#Override
String myString = (String) parent.getAdapter().getItem(position);
if(myString.equals("Clear High Scores")) {
//Stuff
}
}
});
ListView, just like Button, has the ability to set a click listener, but for ListView, the name of the method is setOnItemClickListener(). Instead of using OnClickListener, though, it uses a class called OnItemClickListener, which is more advantageous for using with ListView. The method looks as follows:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
{
// some stuff
}
}
)
Now the advantage of this is that you can check what's contained at the position you've pressed, so the way you could tackle your problem could look something like this:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
{
String myString = (String) parent.getAdapter().getItem(position);
if(myString.equals("Reset High Scores"))
{
// Do what you want
}
}
});
/**
* Creates a new instance
*/
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
super.onListItemClick(l, v, position, id);
//Do stuff
}
that should get ya started... Andrew is right though.

ListView Item Open New Activity

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?
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.
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;
}
}
};
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);
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:
parent: The AdapterView where the click happened.
view: The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position: The position of the item clicked (0 the first, 1 the second etc.)
id: The row id of the item that was clicked.
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.
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);
}
}
});
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);
}
});

Categories