How to get data from selected item in GridView? - java

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!

Related

How to get Item from POJO in listview onClick()?

I have a POJO, which describes some model (Item?) and some custom Adapter.
I am setting my adapter for ListView and then, in onItemClick() I want to get value of one of variables which I had added Into the Item.
How I can reach this?
In my code I am doing something like:
private List<SomeItem> items = new ArrayList();
items.add(new SomeItem(firstValueString, secondValueBitmap, thirdValueString));
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
...
#Override
public void onItemClick(){
//How to reach for example firstValueString value of currently clicked item??
}
)
Use android.widget.AdapterView.OnItemClickListener:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
+ id);
SomeItem item = items.get(position); // specific item
}
});
Based on Android SDK documentation:
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
If you implement your adapter completely then you can get item like bellow:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item item = (Item)parent.getItemAtPosition(position);
}
});

How to display ListView Selected item with ViewPager in android

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

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

How to determine which Listview or Adapter is using an OnClick Method. Android

I want to use an OnClick method for multiple ListViews and Adapters. Inside my code I would like to check which adapter or listview click instantiated the method call. Here is what I was trying to no avail
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(parent==listView1.getParent()) {
If you have multiple ListView, you need to know which ListView is being clicked. So you need to check it first.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.listView1: // this is ID in XML layout
// do action for ListView 1;
break;
case R.id.listView2: // this is ID in XML layout
// do action for ListView 2;
break;
default:
break;
}
}
You can try this.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value = list.get(position);
//Action when clicked
Toast.makeText(MainActivity.this, value, 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.

Categories