ANDROID- how to save selected item from gridView in a string? - android

I am showing captured images in one layout using different gridView for each category (Shirts , Trousers e.t.c). The number of the gridViews is 6.
I am trying to select one image from each gridView and via onClick method in the button to show them in another activity but the code i wrote doesn't work.
I suppose something is wrong with the strings.
Can anyone help me to fix this? Thnak you in advance!
public class CreateOutfit extends Activity implements View.OnClickListener {
private DatabaseHandler handler;
GridView grid1;
GridView grid2;
ArrayList<Clothes> clothes = new ArrayList<Clothes>();
GridviewAdapter GridAdapter;
String image1;
String image2;
public static final String ITEM_IMAGE1 = "image1";
public static final String ITEM_IMAGE2 = "image2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clothes_grids);
handler = new DatabaseHandler(getApplicationContext());
grid1 = (GridView) findViewById(R.id.grid1);
grid1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
image1 = String.valueOf(view.findViewById(R.id.cloth_image_grid).getContext().toString());
}
});
fillGrid1();
grid2 = (GridView) findViewById(R.id.grid2);
grid2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
image2 = String.valueOf(view.findViewById(R.id.cloth_image_grid));
}
});
fillGrid2();
ImageButton btn_in = (ImageButton)findViewById(R.id.btn_insert);
btn_in.setOnClickListener(this);
}
public void fillGrid1() {
clothes = (ArrayList<Clothes>) handler.readGridShirts();
GridAdapter = new GridviewAdapter(this, R.layout.grid_row, clothes);
grid1.setAdapter(GridAdapter);
for (Clothes c : clothes) {
String record = "ID=" + c.getID() + " | Category=" + c.getCategory() + " | " + c.getSize();
Log.d("Record", record);
}
}
public void fillGrid2() {
clothes = (ArrayList<Clothes>) handler.readGridTrousers();
GridAdapter = new GridviewAdapter(this, R.layout.grid_row, clothes);
grid2.setAdapter(GridAdapter);
for (Clothes c : clothes) {
String record = "ID=" + c.getID() + " | Category=" + c.getCategory() + " | " + c.getSize();
Log.d("Record", record);
}
}
#Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(), ViewOutfit.class);
in.putExtra(ITEM_IMAGE1 , image1);
in.putExtra(ITEM_IMAGE2, image1);
startActivity(in);
}
}
public class ViewOutfit extends Activity {
DatabaseHandler handler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_outfit);
Intent i = getIntent();
handler = new DatabaseHandler(getApplicationContext());
ImageView im1 = (ImageView) findViewById(R.id.im1);
String image1 = i.getStringExtra("image1");
im1.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra(image1)));
ImageView im2 = (ImageView) findViewById(R.id.im2);
String image2 = i.getStringExtra("image2");
im2.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra(image2)));
}

The main problem here is: the string's values which are not getting the image path.
According to this line:
image1 = String.valueOf(view.findViewById(R.id.cloth_image_grid).getContext().toString());
It'll never return the image's path... Did you try to display this value in Logcat? It should display the memory's address of the widget's ImageView, and not the image's path.
You should override Object getItem(int position) in GridView's adapter, get the item's model (known as Clothes) and retrieve its image's path. Thus, the adapter should has this:
public Object getItem(int position) {
return clothes.get(position); // return the item by its position
}
Then, in the class, when you set the listener, it should be as this:
grid1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// retrieve the model's item
Clothes selectedItem = (Clothes) tShirtGridAdapter.getitem(position);
// then, get the image's path and save it
image1 = selectedItem.image_url; // "image_url" is the String path's value in model
}
});
Beside, you saw adapterTShirt in the above code, because you used the same variable of the adapter GridAdapter and you set a new instance of the adapter on this line for both GridViews:
GridAdapter = new GridviewAdapter(this, R.layout.grid_row, clothes);
This might be better to not re-initialize the adapter and display the same for two gridviews. You'd have two variables (two instances) of the adapter as:
GridviewAdapter tShirtsGridAdapter, troussersGridAdapter;
// then
tShirtsGridAdapter = new GridviewAdapter(this, R.layout.grid_row, clothes);
// and
troussersGridAdapter = new GridviewAdapter(this, R.layout.grid_row, clothes);
With this, you will have two separate instances and you can't retrieve the image 's path in click listener from the right clicked item like my snippnet code above.

Related

Get object from ListView in android

I want to get object from ListView. I put my data from a csv file.
I have two column and around 100 rows. First column is Name, second is number.
I want get number after clicking on a row.
So I have in onCreate:
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Button btn2 = (Button) findViewById(R.id.button3);
btn2.setEnabled(true);
Object listItem = listView.getItemAtPosition(position);
String item = ( listView.getItemAtPosition(position).toString());
btn2.setText(item);
}
});
And after that when I click on a row (first and second column= the same score), my Button text shows [LJAVA.LANG.STRING;#42791450, every row have other numbers.
EDIT:
listView = (ListView) findViewById(R.id.list_view2);
itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.single_list_item);
Parcelable state = listView.onSaveInstanceState();
listView.setAdapter(itemArrayAdapter);
listView.onRestoreInstanceState(state);
InputStream inputStream = getResources().openRawResource(R.raw.manager_number);
CSVReader csv = new CSVReader(inputStream);
List<String[]> scoreList = csv.read();
for (String[] scoreData : scoreList) {
itemArrayAdapter.add(scoreData);
}
If you have a way to retrieve the object from your ItemArrayAdapter, for example itemArrayAdapter.get(index), you could declare it as an instance attribute and use the position as index. Like so:
public class myActivity extends Activity {
private ItemArrayAdapter itemArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.single_list_item);
InputStream inputStream = getResources().openRawResource(R.raw.manager_number);
CSVReader csv = new CSVReader(inputStream);
List<String[]> scoreList = csv.read();
for (String[] scoreData : scoreList) {
itemArrayAdapter.add(scoreData);
}
ListView listView = (ListView) findViewById(R.id.list_view2);
listView.setClickable(true);
Parcelable state = listView.onSaveInstanceState();
listView.setAdapter(itemArrayAdapter);
listView.onRestoreInstanceState(state);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Button btn2 = (Button) findViewById(R.id.button3);
btn2.setEnabled(true);
String item = this.itemArrayAdapter.get(position);;
btn2.setText(item);
}
});

OnClickItem with unknown number of items Android

I have an array of objects. In my listview, I pass only the name of those objects but when someone clicks on any of them, I want a new window to pop up and to see the extra information from my items. Can I do that somehow?
This is how my list activity looks like:
public class ListItemsActivity extends ListActivity {
String[] mTestArray;
ListView listView;
private static final String TAG = "ListActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "Trying to create the list activitye");
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter;
mTestArray = getResources().getStringArray(R.array.sections);
ArrayList<Sweet> sweets = getSweets(mTestArray);
ArrayList<String> result = getSweetsNames(mTestArray);
Log.d(TAG, mTestArray.toString());
adapter = new ArrayAdapter<String>(
this,
R.layout.activity_list_items,
result;
setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.setItemChecked(position, parent.isItemChecked(position));
Toast.makeText(this, "You have selected " + mTestArray[position],
Toast.LENGTH_SHORT).show();
}
So this is ok, it shows me a lsit of names. And when I click on them it jsut tells me on a small popup thing that I've selected it. What I want is actually to open a new window and show all the information from my items. Is that possible? How would I go around to do it?
The only way I found is to do something like this:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position ) {
case 0: Intent newActivity = new Intent(this, i1.class);
startActivity(newActivity);
break;
case 1: Intent newActivity = new Intent(this, i2.class);
startActivity(newActivity);
break;
case 2: Intent newActivity = new Intent(this, i3.class);
startActivity(newActivity);
break;
case 3: Intent newActivity = new Intent(this, i4.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this, i5.class);
startActivity(newActivity);
break;
}
}
But it's a bad approach for these reasons:
1) I have an unknown number of elements
2)I dont have 1000 activities for each item, I want 1 general window that would depend on some integer position.
Can I do it this way?
If you are getting position of the item from listView, then I think you can get the information about same item by the use of Adapter.
Codes that you can try:
Make a xml that your list view items would have:
This can include any types of items and items would be seen in the list view as you would want to show it. I am making an xml named list_items_view.xml and including just a text view in the listview.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/nameInList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:padding="7dp"/>
</RelativeLayout>
Make a class that would include the items that you want to bind with each list-items:
Here I am binding each list items with it's description, price, and callories (You can change that according to your need), and make constructor and getter-setter method for each one.Name of the class is ListDetailsClass:
public class ListDetailsClass {
String price,name, description,calories;
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public ListDetailsClass(String price, String name, String description, String calories) {
this.price = price;
this.name = name;
this.description = description;
this.calories = calories;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCalories() {
return calories;
}
public void setCalories(String calories) {
this.calories = calories;
}
}
Make an adapter that could adapt the properties of the xml and the class in one single item:
Here I have made an adapter class that extends BaseAdapter and implemented it's methods according to use of my purpose.Name of the class is adapterForLV:
public class adapterForLV extends BaseAdapter {
ArrayList<ListDetailsClass> itemsInList;
Context mContext;
LayoutInflater inflater;
public Context getmContext() {
return mContext;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public ArrayList<ListDetailsClass> getItemsInList() {
return itemsInList;
}
public void setItemsInList(ArrayList<ListDetailsClass> itemsInList) {
this.itemsInList = itemsInList;
}
public adapterForLV(ArrayList<ListDetailsClass> itemsInList, Context mContext) {
this.itemsInList = itemsInList;
this.mContext = mContext;
}
#Override
public int getCount() {
return itemsInList.size();
}
#Override
public Object getItem(int position) {
return itemsInList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(inflater == null){
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(convertView == null){
convertView = inflater.inflate(R.layout.list_items_view,null);
}
TextView nameOfItem = (TextView) convertView.findViewById(R.id.nameInList);
ListDetailsClass items = itemsInList.get(position);
String name = items.getName();
nameOfItem.setText(items.getName());
return convertView;
}
}
Finally implement adapter in your main activity so as to include the list items with bound data:(Name of the activity is MainActivity)
ListView listView;
ArrayList<ListDetailsClass> list = new ArrayList<>();
adapterForLV customAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv) ;
//Adapted the list form with customAdapter
customAdapter = new adapterForLV(list,this);
//Set the listview to the customAdapter
listView.setAdapter(customAdapter);
//Made two new objects of the ListDetaisClass to add data in the listview
ListDetailsClass newData = new ListDetailsClass("3$","abc","description","543 cal");
ListDetailsClass newData2 = new ListDetailsClass("35.3$","item name","description about item","callories about it");
//Added data to the list
list.add(newData);
list.add(newData2);
//Listview item click listener implementation
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = customAdapter.getItemsInList().get(position).getName();
String description = customAdapter.getItemsInList().get(position).getDescription();
String price = customAdapter.getItemsInList().get(position).getPrice();
String calories = customAdapter.getItemsInList().get(position).getCalories();
//Intent to pass the data of the list item to next activity
Intent i = new Intent(getApplicationContext(),Main2Activity.class);
i.putExtra("Item_Name",name);
i.putExtra("Item_Desc",description);
i.putExtra("Item_Price",price);
i.putExtra("Item_cal",calories);
startActivity(i);
}
});
}
Getting the data to show in the form according to our use in the new activity:
Here you have to define a new xml for the new activity so that data could be shown in the form we want.
Main2Activity:
//defined textViews to show my data
TextView itemName,itemDescription,itemPrice,itemCal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemName = (TextView) findViewById(R.id.ItemName);
itemDescription = (TextView) findViewById(R.id.ItemDescr);
itemCal = (TextView) findViewById(R.id.ItemCal);
itemPrice = (TextView) findViewById(R.id.ItemPrice);
//Getting data from oldActivity i.e. MainActivity
Intent i = getIntent();
//Setting data to textViews
itemName.setText("Name: "+i.getStringExtra("Item_Name"));
itemDescription.setText("Description: "+i.getStringExtra("Item_Desc"));
itemPrice.setText("Price: "+i.getStringExtra("Item_Price"));
itemCal.setText("Calories: "+i.getStringExtra("Item_cal"));
}
Screenshots after implementation:
Listview
Item details in new activity
Hope this help you!
I didn't understand well but you could use Intent for new Window For example:
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.setItemChecked(position, parent.isItemChecked(position));
Intent intent=new Intent(ListItemActivity.this, newDetailActivity.class); //newDetailActivity is a Activity you need to create or can say redirect window
startActivity(intent); // This opens a window
}
Here's Official Documentation for more information Follow Documentation
You can start a common activity and pass the selected item along with the intent :
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.setItemChecked(position, parent.isItemChecked(position));
//DetailsActivity is the activity which shows the extra details
Intent intent=new Intent(ListItemActivity.this, DetailsActivity.class);
//Add the item that the user clicked on, the class has to implement Parcelable or Serializable
intent.putExtra("data", sweets.getItem(position));
startActivity(intent); // This opens a window
}
In the opened activity, you can get the item from the intent and display it's contents :
//in newDetailActivity :
Sweet s = getIntent().getExtras.getParcelable("data");
The easiest way of passing data between activities is using intents.
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent newActivity = new Intent(this, i1.class);
newActivity.putExtra("id", postion);
newActivity.putExtra("key", value);
startActivity(newActivity);
}
In short, putExtra method takes a key and value
which can be retrieved in the destination Activity.
Bundle extras = getIntent().getExtras();
String id,key;
if(extras == null) {
id = null;
key = null;
} else {
id= extras.getString("id");
key= extras.getString("key");
}

listView item click event not firing

I am having an issue with using a listview that when I click it nothing is happening. I would like for it when I click the listview item to make a toast so I know it is being clicked. I have been trying/researching for a while and nothing. Would anybody mind taking a look to see if I am missing something I just am overlooking? Many thanks in advance!
Here is my class:
public class MyCar extends Activity {
/**
* Called when the activity is first created.
*/
public ListView mylistView;
String carInfo;
private ArrayAdapter<String> mylistAdapter;
ArrayList<String> arrayListCar = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mycar);
mylistView = (ListView) findViewById(R.id.listView);
arrayListCar = new ArrayList<String>();
//Just had to remove setting this adapter 2 times. Took out line below to fix.
mylistView.setAdapter(mylistAdapter);
mylistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
carInfo = trySomethin();
fillList();
}
public void fillList() {
String make = "";
String model = "";
String[] pieces = carInfo.split("\"");
make = pieces[3];
model = pieces[7];
ArrayList<String> carList = new ArrayList<String>();
carList.add(make + " " + model);
// Create ArrayAdapter using the car list.
mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);
mylistView.setAdapter(mylistAdapter);
mylistAdapter.notifyDataSetChanged();
}
}
if you have any elements on listview item change this for them
android:focusable="false"
and if you are changing any elements visibility on runtime you have to handle focus programatically each time you change its visibility.
Try this
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});

passing the data of the selected items to another activity - android

I have a menu (fishcombovaluemeals), and when the user select more than 1 item i want those
items to appear in a list in another activity (shopping cart) ,i have tried alot but the data
never appears in the shopping cart ! what is wrong in the onitemclick !? ,
I have
fishcombovaluemeal.java
RowItem.java
CustomListViewAdapter.java
fishcombovaluemealsactivitycode:
package com.example.newlist;
public class FishComboValueMeals extends Activity implements
OnItemClickListener {
ImageButton Ib2;
public static final String[] titles = new String[] {
" Fillet-O-Fish (Medium Value Meals) ",
"Fillet-O-Fish (Large Value Meals) ",
"Double Fillet-O-Fish (Medium Value Meals)",
" Double Fillet-O-Fish (Large Value Meals) ",
};
public static final String[] descriptions = new String[] {
"Light, flaky filet of white fish topped with tangy tartar ",
"Light, flaky filet of white fish topped with tangy tartar ",
"2 patties of tender fish filet over a layer of cheese, ",
" 2 patties of tender fish filet over a layer of "
};
public static final Integer[] images = {
R.drawable.imfc1,
R.drawable.imfc2,
R.drawable.imfc3,
R.drawable.imfc4 };
public static final Double[] prices = { 20.0, 22.73, 24.77, 27.04 };
ListView listView;
List<RowItem> rowItems;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_breakfast);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem (images[i], titles[i], descriptions[i],
prices[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
Ib2 = (ImageButton) findViewById (R.id.Button02);
Ib2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generatedmethod stub
Intent openStartingPoint = new Intent (getApplicationContext(),ShoppingCart.class);
startActivity (openStartingPoint);
}
});}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent i = new Intent(getApplicationContext(), ShoppingCart.class);
i.putExtra("EXTRA", "Item " + (position + 1) + ": " + rowItems.get(position));
startActivity(i);
}
}
get the selected item of list view and send the value via intent
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String value = lv1.getItemAtPosition(position).toString();
Intent i = new Intent(getApplicationContext(), ShoppingCart.class);
i.putExtra("EXTRA", value);
startActivity(i);
}
and get the value in other activity
Bundle bundle=getIntent().getExtras();
String value=bundle.getString("EXTRA");
You can use a very simple Class which will store your data into it.
I warn you that this class is just a simple proposition how to solve your problem.
It has many weaknesses but it works ; )
You can improve it : )
Just init it at Application Class.
Here is example:
/**
* Manager to handle passing data between Activities
*
* #author Klawikowski
*
*/
public class ManagerBundle implements IConstants
{
public static final String TAG = "ManagerBundle";
private static Hashtable< Integer , Object > sDataTable;
public static void init()
{
Log.i( TAG , "[ManagerBundle] Initializing ..." );
sDataTable = new Hashtable< Integer , Object >();
}
public static void addBundle( int pKey , Object pObjectToPass )
{
Log.i( TAG , "[ManagerBundle] Adding Object to Manager using key: " + pKey );
sDataTable.put( pKey , pObjectToPass );
}
public static Object getBundle( int pKey )
{
Log.i( TAG , "[ManagerBundle] Getting Object from Manager using key: " + pKey );
Object lData = sDataTable.get( pKey );
sDataTable.remove( pKey );
return lData;
}
}
Just simply put an Table / Array or any other container before starting Activity, and collect it from 2nd one ; )
You need to create a singleton array of the menu items. When user performs multi select jst store the selectedindex to an array and then pass that array to the intent.
Here is the tutorial for multi-select listview
http://www.vogella.com/articles/AndroidListView/article.html
Here is the code to send array in intent
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
You can try this
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
RowItem selItem = rowItems.get(position);//get the selected RowItem from the list
String[] selValues= selItem.getMethodName();//your method name in RowItem.java class
//which returns array
//if you have getter and setters you can access them and put them in array
//String[] selArray = new String[size];//size = number of value you want to supply
//String[0]= selItem.getTitle();//for title
//String[1] = selItem.getDescription();//for description and so on
//then send the array as parameter
// remaining is same as answered by the Jay Gajjar
Bundle b=new Bundle();
b.putStringArray("EXTRA", selArray);
Intent i = new Intent(getApplicationContext(), ShoppingCart.class);
i.putExtras(b);
startActivity(i);
}
and to retrieve the values in the ShoppingCart class
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray("EXTRA");

How to pass a jpg id between activities Android

I have a gridView with imageViews in one Activity and I want to load one of them in other activity on a new ImageView when select it, but i have no results
Activity 1:
public class Level extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level);
GridView gridview = (GridView) findViewById(R.id.gridviewLevel);
ImageAdapter ia = new ImageAdapter(this);
gridview.setAdapter(ia);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Level.this, "" + position, Toast.LENGTH_SHORT).show();
initImageCheck( v.getId());
}
});
}
public void initImageCheck(int id){
Intent intentIC = new Intent(this, ImageCheck.class);
intentIC.putExtra("ID", id);
startActivity(intentIC);
}
}
Activity 2:
public class ImageCheck extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_check);
Bundle extras = getIntent().getExtras();
int id = extras.getInt("ID");
ImageView imgView = (ImageView) findViewById(R.id.imageViewCheck);
imgView.setImageResource(id);
}
}
Whats the problem??? Thanks.
v.getId() is the ID of the View, not of the image resource.
Have ImageAdapter also hold the id of resource used per item, and also provide a method like getResourceIdForItem(int position).

Categories

Resources