【问题标题】:how to get value of item in Gridview using custom Adapter如何使用自定义适配器在 Gridview 中获取项目的值
【发布时间】:2014-05-28 09:37:26
【问题描述】:

我有一个用于 GridView 适配器的自定义适配器

我想从所选项目中获取特定值,例如“stamp ID”

很久以前我正在开发它以仅显示图章,现在我希望能够单击一个图章将他发送到下一页

我在互联网上寻找解决方案,但大多数教程都要求我对适配器代码进行大量修改(Witch 意味着创建一个新类)

我需要知道是否有一种简单的方法可以使用我正在使用的适配器来解决此问题。

这是 StampActiviy 类中连接的部分

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


    }
});

这是 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;
    }
}

这是 StampUtil 类

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


}

谢谢

【问题讨论】:

  • 为什么 StampId 是静态的?为什么getItem在适配器中返回null?你为什么使用 HashMaps 而不是 Stamp 类的 ArrayList?这段代码完全搞砸了...I need to know if there a simple way to fix this using the adapter that I am using 最简单的方法是学习更多的 java 并做更多的研究(好吧,有一种更简单的方法 - 花钱请人为你做这些事情)

标签: android gridview custom-adapter listview-adapter


【解决方案1】:

试试下面的代码:

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

【讨论】:

  • ListViewListStampsAdapter 类型的方法 get(int) 未定义
  • getStampId() 是 StampUtil 中的方法,它不会检索任何内容,因为我没有为 stampID 设置任何数据
  • @AhmadSaleh arg0.getAdapter().getItem(arg2) 返回 StampsUtil 的对象
【解决方案2】:

你可以使用

grid.getItemAtPosition(position).toString();

我曾经这样使用过:-

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

gridView.setOnItemClickListener(new OnItemClickListener() {

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

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

在适配器中:-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多