当前位置:编程学习 > JAVA >>

【请教贴】我的页面上是GridView,图片下隐藏了一个TextView值,我如何获取每一个图片下的ID值,我运行时出异常

我的页面上是GridView,图片下隐藏了一个TextView值(ID),我如何获取每一个图片下的ID值下面是我的代码,包括ShopActivity,Adapter_Shop

下面是ShopActivity

package com.example.foodbcms_android;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.example.foodbcms_android.adapter.Adapter_Shop;
import com.example.foodbcms_android.vo.SHOP;

public class ShopActivity extends Activity {
private GridView gridView;
private List<SHOP> list = new ArrayList<SHOP>();
private MyHandler handler ;
private Thread thread ;
private SimpleAdapter saImageItems; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        gridView=(GridView)findViewById(R.id.gridview);
        handler = new MyHandler();
thread = new Thread(downloadRun);
thread.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_shop, menu);
        return true;
    }
    
    class MyHandler extends Handler{

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Adapter_Shop adapter=new Adapter_Shop(ShopActivity.this,list);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new ItemClickListener());  
}
class  ItemClickListener implements OnItemClickListener  
    {  
public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened   
                                    View arg1,//The view within the AdapterView that was clicked  
                                    int arg2,//The position of the view in the adapter  
                                    long arg3//The row id of the item that was clicked  
                                    ) {  
      
HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);  
//setTitle((String) item.get("ItemText"));
String rid = (String) item.get("rid");
System.out.println("rid=========="+rid);
}  
    }
    }
    
    Runnable downloadRun = new Runnable() {

@Override
public void run() {
try {

// 创建HttpGet对象
HttpGet request = new HttpGet(
"http://10.0.2.2/foodBCMS/UserAction!FindAllShop.action");

// 使用execute方法发送HTTP GET请求,并返回HttpResponse对象
// DefaultHttpClient为Http客户端管理类,负责发送请
HttpResponse response = new DefaultHttpClient()
.execute(request);


if (response.getStatusLine().getStatusCode() == 200) {

String json = EntityUtils.toString(response.getEntity(), "gbk");
try{
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);

SHOP shop=new SHOP();
shop.setS_id(jsonObject.getString("s_id"));
shop.setS_name(jsonObject.getString("s_name"));
shop.setS_src(jsonObject.getString("s_src"));
shop.setS_hotel(jsonObject.getString("s_hotel"));
list.add(shop);
for(int j=0;j<list.size();j++){
System.out.println("The shop's name is "+list.get(j).getS_id());
}
}

}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch (IOException e) {
e.printStackTrace();
}

// 通知handler加载列表
Message msg = handler.obtainMessage();
msg.sendToTarget();
}
    
    };
}



下面是Adapter_Shop


package com.example.foodbcms_android.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.foodbcms_android.R;
import com.example.foodbcms_android.util.AsyncLoadImage;
import com.example.foodbcms_android.util.FileUtil;
import com.example.foodbcms_android.util.HttpDownloader;
import com.example.foodbcms_android.vo.SHOP;


public class Adapter_Shop extends BaseAdapter{
private Map<String,Object> imageViews=new HashMap<String,Object>();
private HashMap<String,Drawable> drawables=new HashMap<String,Drawable>();
private Map<String,Object> map=new HashMap<String,Object>();
private Context context;
private LayoutInflater layoutInflater;
private List<SHOP> list;
private SHOP shop;

public Adapter_Shop(Context context,List<SHOP> list){
this.context = context;
this.list = list;
}

@Override
public int getCount() {

return list.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_shop, null);
ImageView image = (ImageView)convertView.findViewById(R.id.recipeImageUrl);
TextView name = (TextView)convertView.findViewById(R.id.recipeName);
TextView rid = (TextView)convertView.findViewById(R.id.recipeId);
shop=list.get(position);
rid.setText(String.valueOf(shop.getS_id()));
name.setText(shop.getS_hotel());
String fileName = FileUtil.getFileName("http://10.0.2.2/foodBCMS/"+shop.getS_src());
imageViews.put(fileName, image);
map.put("rid", shop.getS_id());
AsyncLoadImage.loadImage(image, "http://10.0.2.2/foodBCMS/"+shop.getS_src());
new LoadImage("http://10.0.2.2/foodBCMS/"+shop.getS_src()).start();
return convertView;
}

Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {

if (msg.what == 0) {
Bundle bundle = msg.getData();

String key = bundle.getString("fileName");

if (key != null) {

ImageView iv = (ImageView)imageViews.get(key);
Drawable drawable = drawables.get(key);

if (iv != null && drawable != null) {
iv.setImageDrawable(drawable);
}

}
}
}

};
class LoadImage extends Thread {

private String url;

public LoadImage(String url) {
this.url = url;
}

@Override
public void run() {

Drawable dr = FileUtil.getDrawableFromSdcard(url);
String fileName = FileUtil.getFileName(url);
System.out.println("fileName"+fileName);
drawables.put(fileName, dr);

Message msg = handler.obtainMessage();
msg.what = 0;
Bundle data = new Bundle();
data.putString("fileName", fileName);
msg.setData(data);
msg.sendToTarget();

System.out.println("runnable is running");

}

}
}





在运行时会出现异常:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.HashMap
出现此异常的代码是:
ShopActivity.java中的HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
我今天看一个帖子GridView中给item增加事件都用到了这个代码
我还把他们的代码运行了都好用,可是一到我这里就不好用了,求大神们指点。
安卓中GridView的问题 --------------------编程问答-------------------- 谁来帮帮我~~
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,