0

I have a customAdapter whose .xml for the views in the grid is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:orientation="vertical">
    <ImageView
    android:id="@+id/imagepart"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:gravity="top"/>

    <TextView
    android:id="@+id/textpart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor = "#000000"
    android:gravity="top"/>
</LinearLayout>

In this custom adapter i have a method to return an string from a private array.

public class DishesImageAdapter extends BaseAdapter {

    ...

    public String getName(int position) {
        if(position > 0 && position >= mThumbsNames.length)
        {
            return mThumbsNames[position];
        }
        return null;
    }

    ...

}

I would like to call this former method inside of onItemClick, i have tried several ways through "parent" object but i cant...

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
       Intent DishDetailsIntent = new Intent(v.getContext(), DishesDetails.class);
       DishDetailsIntent.putExtra("SectorNum", getIntent().getStringExtra("SectorNum"));
       String DishName = //
       DishDetailsIntent.putExtra("DishName", DishName);
       startActivity(DishDetailsIntent);
}

Any help?

Thanks in advance.

Finally i got the answer:

TextView tv= (TextView) v.findViewById(R.id.textpart);
String DishName = tv.getText().toString();
DishDetailsIntent.putExtra("DishName", DishName);
omniyo
  • 330
  • 2
  • 6
  • 19

1 Answers1

0

I would recommend moving getName() out into the Activity where it is accessible for both purposes.

A second option would be to make getName static so that you can access it directly, this may cause other issues though.

Alan Moore
  • 6,525
  • 6
  • 55
  • 68
  • I cant move getName() out of the ImageAdapter class... Nevertheless, in spite of making getName() static, how can i call getName() from onItemClick() in the part of the code where "//" is... – omniyo Oct 23 '11 at 18:47
  • If it's static you should be able to call as DishesImageAdapter.getName(). – Alan Moore Oct 24 '11 at 01:34