-1

I have a custom adapter for GridView Adapter

I want to get an specific value from the selected Item such as "stamp ID"

I was developing this to display only the stamps long time ago and now I want to be able to click on one stamp to send him to next page

I looked in the internet for a solution but most of the tutorials require me to modify a lot on the Adapter code (Witch means creating a new class)

I need to know if there a simple way to fix this using the adapter that I am using.

this is the part on StampActiviy class where connect

GridView gridView = (GridView) findViewById(R.id.gvStamps);
// Pass the results into ListViewAdapter.java  adapterCollected,adapterunCollected
adapterListStamps = new ListViewListStampsAdapter(KioskStampsListActivity.this, arraylistStamps);
// Set the adapter to the ListView
gridView.setAdapter(adapterListStamps);

gridView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // I need to get StampId then send the user to next screen
        Toast.makeText(KioskStampsListActivity.this, ""+arg2+" - "+stampsUtil.getStampId(), Toast.LENGTH_LONG).show();


    }
});

this is the ListViewListStampsAdapter

public class ListViewListStampsAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();


    public ListViewListStampsAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables

        TextView tvStampName, tvStampId;
        ImageView stampImage;
        String STAMP_IMAGE_URL = SharedPrefUtils.getUrl(context, "images/stamp/");

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.single_stamp_layout, parent, false);
        // Get the position
        resultp = data.get(position);

        tvStampName = (TextView) itemView.findViewById(R.id.tvStampName);
        tvStampId = (TextView) itemView.findViewById(R.id.tvStampId);
        stampImage = (ImageView) itemView.findViewById(R.id.stampImage);

        tvStampName.setText(resultp.get(KioskStampsListActivity.TAG_STAMP_NAME));
        tvStampId.setText(resultp.get(KioskStampsListActivity.TAG_STAMPS_ID));

        // Passes stampImage images URL into ImageLoader.class
        imageLoader.DisplayImage(STAMP_IMAGE_URL+resultp.get(StampsActivity.TAG_STAMP_IMAGE), stampImage);
        Animation myFadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fadein);
        stampImage.startAnimation(myFadeInAnimation);


        return itemView;
    }
}

this is StampUtil class

public class StampsUtil {
    private static String StampId, message, stampimage, stampname;

    public  String getStampId() {
        return StampId;
    }

    public  void setStampId(String stampId) {
        StampId = stampId;
    }

    public static String getMessage() {
        return message;
    }

    public static void setMessage(String message) {
        StampsUtil.message = message;
    }

    public static String getStampimage() {
        return stampimage;
    }

    public static void setStampimage(String stampimage) {
        StampsUtil.stampimage = stampimage;
    }

    public static String getStampname() {
        return stampname;
    }

    public static void setStampname(String stampname) {
        StampsUtil.stampname = stampname;
    }


}

Thanks

Ahmad Saleh
  • 879
  • 2
  • 10
  • 20
  • why StampId is static? why getItem return null in adapter? wy you are using HashMaps instead ArrayList of Stamp class? this code is completely messed up... `I need to know if there a simple way to fix this using the adapter that I am using` the simplest way is to learn more java and do some more reaserch(ok, there is one simpler way - paid someone to do the stuff for you) – Selvin May 28 '14 at 09:57

2 Answers2

0

Try below code:

gridView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // I need to get StampId then send the user to next screen
        Toast.makeText(KioskStampsListActivity.this, ""+arg2+" - "+stampsUtil.getStampId(), Toast.LENGTH_LONG).show();

         System.out.println(arraylistStamps.get(i).getStampId());// you can get value 
    }
});
duggu
  • 37,851
  • 12
  • 116
  • 113
0

You can use

grid.getItemAtPosition(position).toString();

I have used like this:-

public static String getUrl(int position){
    String itemName = grid.getItemAtPosition(position).toString();
    return itemName;// Return your string value
}

OR

gridView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
        int arg2, long arg3) {

    String itemName = grid.getItemAtPosition(position).toString();
}});

and in Adapter :-

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return web[position]; //--> web is my array of string
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
Sophia
  • 1,046
  • 1
  • 12
  • 15