Android: How to extract data from GridView? - android

Hi all
I have a GridView of FrameLayout made of an image and textview. I want to get the corresponding image and the text when I click a cell of Grid. Mentioned that textview is top of image.
Does anybody have idea how to do it?

First register item click listener of grid view
e.g. gridview.setOnItemClickListener(gridItemClickListener);
Then
OnItemClickListener gridItemClickListener = new OnItemClickListner()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// here you will get your clicked cell of grid and you can find the image and text.
ImageView image = view.findViewById(R.id.grid_raw_image);
TextView textView = view.findViewById(R.id.grid_raw_text);
}
};

If you use the viewHolder technique as recommended by google, use:
gridView.setOnItemClickListener(new OnItemClickListner()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ViewHolder holder=(ViewHolder)view.getTag();
//use the views you hold in the viewHolder
...
}
});

Related

Get id / position of ListView row

I have an Android ListView with a bunch of rows. Each row contains an image and some text. If I click on the image of a row, I want to perform action A, if I click on any other place in the row, I want to perform action B. Both of these actions need the id of the ListView item to work.
So far, for action B, I simply used listview's onItemClick, where I got the id from the parameter. However, as far as I know, the only way to find out on which view in the row itself the user clicked, is to add an onClick event handler to the ImageView that triggers action A. In that case, I only get the View that was clicked, though - not the id of the row that the ImageView is in.
My question is - how do I go from a View object to getting the id of the parent row within the ListView?
The onItemClickListener method has an onClick interface already with the view and position passed into it: You can get check which view has been clicked by doing a switch statement and performing a dedicated action when the specific view has been clicked.
E.g.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
}
});
as shown in How to create listview onItemclicklistener
You'll see that in the onItemClick interface it has the View view as a parameter and int position as a parameter, perform the action inside the listener using both of those parameters. Like i mentioned before you can use a switch statement.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
switch(view.getId()) {
case R.id.imageView:
//perform action for imageView using the data from Item (if you have used an MVC pattern)
break;
// you can do this for any widgets you wish to listen for click actions
}
}
});
Update: All credit for this goes to wwfloyd in How to know which view inside a specific ListView item that was clicked
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.one_line, parent, false);
}
// This chunk added to get textview click to register in Fragment's onItemClick()
// Had to make position and parent 'final' in method definition
convertView.findViewById(R.id.someName).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
// do stuff...
}
in your adapter apply a click listener to the element and then trigger the interface method, so that you can identify the registered widget that has been clicked and then in your onItemClick do:
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
long viewId = view.getId();
switch (viewId) {
case R.id.someName:
//widget has been clicked
break;
default:
//row has been clicked
break;
}
}
if you aren't using tag, then you can
int itemId=...;
OnClickListener customOnClick=...;
imageView.setTag(itemId);
imageView.setOnClickListener(customOnClick);
and inside your custom OnClickListener
int itemId = (Integer) view.getTag();
note that you can set "any" Object as tag, so you may even set your whole custom object, not only id
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id)
in this method View arg1 gives you Item view and you can find the item content by this arg1.findViewById(R.id.content);. Here position gives you the item position of list view
You can set click listeners for each view in a row from the getView() method in the adapter.
You can impliment it in getView() method of adapter of listview.

Getting views of a custom view in android

Well I have a listview with an OnItemClickListener on it, each item of listview is a customview with two textviews, I want to get text of first textview when an item is clicked, So how can I do that? when an item is clicked I can just get the view that is clicked but how can I get textview on this customview?
Traverse that view's hierarchy:
TextView textView = (TextView) clickedView.findViewById(R.id.first_text_view);
clickedView is provided to you by OnItemClickListener.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textFromFirstView = yourArray.get(position).getTextFromFirst();
}
});
whre getTextFromFirst() is getter for your textview for example

How to modify only one row at a time in Listview?

I am trying to figure out the solution for the following listed problem. I have a Listview generated using Simpleadapter. When I click on a row in the listview I want to make a Layout with an id colorful as visible. I am able to do this. But my problem starts here. When I click on another row say row number 5 the colorful layout is visible, but the layout is also visible for the previously clicked row too. What I want to do is make the layout colorful visible for only the clicked row (i.e it should be visible for only one row at a time i.e is currently clicked row and hidden for all the remaining rows) and the layout should get invisible for the previously clicked rows. I tried doing with viewholder but it doesn't help. My code snippet is below. Guide me step by step as I am very new to Android.
final BaseAdapter k=new SimpleAdapter(getActivity(),val,R.layout.mytaskdata,new String[]{"sname","heading","desc","id","path","receiver","sender"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.hide1,R.id.hide2,R.id.hide3,R.id.hide4})
{
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final View v = super.getView(position, convertView, parent);
TextView myname=(TextView)v.findViewById(R.id.textView1);
TextView mydes=(TextView)v.findViewById(R.id.textView2);
TextView mytopic=(TextView)v.findViewById(R.id.textView3);
ImageView edit=(ImageView)v.findViewById(R.id.ImageView03);
sent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
RelativeLayout r=(RelativeLayout)arg1.findViewById(R.id.colorful);
// r.setVisibility(arg1.VISIBLE);
int temp=sent.getCheckedItemPosition();
Log.i("itemposition",""+temp);
Toast.makeText(getActivity(),"pos"+arg2+"hii"+positionThatSaysHi,1000).show();
if(arg2!=positionThatSaysHi)
{
r.setVisibility(arg1.VISIBLE);
positionThatSaysHi = arg2;
notifyDataSetChanged();
}
else
{
r.setVisibility(arg1.GONE);
notifyDataSetChanged();
}
});
I would suggest modifying the OnClickListener to just record the selected row, then call notifyDataSetChanged(). This will cause the ListView to redraw its items by calling the adapter. Therefore, you just need to check this value in getView() to determine whether the "colorful" view should be visible or not.
So the updated code would be something like:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final View v = super.getView(position, convertView, parent);
TextView myname=(TextView)v.findViewById(R.id.textView1);
TextView mydes=(TextView)v.findViewById(R.id.textView2);
TextView mytopic=(TextView)v.findViewById(R.id.textView3);
ImageView edit=(ImageView)v.findViewById(R.id.ImageView03);
RelativeLayout r = (RelativeLayout)v.findViewById(R.id.colorful)
r.setVisibility(position == positionThatSaysHi ? View.VISIBLE : View.GONE);
sent.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
positionThatSaysHi = arg2;
notifyDataSetChanged();
}
});
}
That way, you ensure that only one view will be highlighted (while also simplifying your code).
Take advantage of choice mode in ListView ( see setChoiceMode()), and set it to CHOICE_MODE_SINGLE.
Use the selection state to toggle the visibility of your colorful layout. If possible, the easiest way to do this is to have the colorful bits in a selector drawable, with selected=true. That way it will show automatically, and you don't have to worry about hiding and showing views.

How to get another item then click on item in gridview?

I want to know how to get different item from selected item in gridview. I have gridview game of matching pictures. When i click on one image then that will flip and then when click on second image first will flip back. How to perform this animation? I want to get imageview of first selected image.
You can do that by saving the last view in a variable
View lastView = null;
// .....
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
if(lastView != null) {
// do something with the last view
}
// the rest of your code
lastView = view;
}
});
Not tested but should work.

How to retrieve the right view?

My adapter contain the following Views:
private static class ViewHolder {
ImageView p_Image;
TextView p_Name;
TextView p_Price;
TextView p_Psave;
}
This Is how I implement onItemClick :
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
});
How can I know which view from the above 4 was actually pressed (while running OnItemClickListener via GridView) ?
Thanks
The second parameter to onItemClick, View v is a reference to the view that was clicked. You can compare this to your views to see which one it was.

Categories

Resources