Android Horizo​​ntalListView onItemClick无法正常工作

时间:2013-03-11 09:20:50

标签: android onitemclick horizontallist

我对点击该项目有疑问。每当我点击或触摸时, 申请停止。我想知道我的代码有什么问题。这是我的代码的一部分。 RowItem是一个POJO。然后MyCustomBaseAdapter扩展BaseAdapter。

public void onCreate(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.shop_main);

Intent intent = getIntent();
String categoryId = intent.getStringExtra("id");

JSONParser parser = new JSONParser(this); 
JSONObject json = parser.getJSONFromAssets("shops.json");

sItems = new ArrayList<ShopRowItem>();

try{
    shops = json.getJSONArray(SHOPS_ARR);
    for(int i = 0; i < shops.length(); i++){
        JSONObject c = shops.getJSONObject(i);

        String sName = c.getString(NAME);
        String sCatId = c.getString(ID);
        String imageUrl = c.getString(IMAGE_URL);


        if(sCatId.equalsIgnoreCase(categoryId)){
            RowItem rItem = new RowItem(imageUrl, sName, sCatId);
            shopItems.add(shop);
        }
    }
}catch(JSONException e){
    throw new RuntimeException(e);
}

hListView = (HorizontalListView) findViewById(R.id.shop_hlist_view);
final CustomBaseAdapter adapter = new CustomBaseAdapter(this, sItems);
hListView.setAdapter(adapter);

hListView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        HashMap<String, String> dataMap = (HashMap<String, String>) adapter.getItem(position);
        System.out.println("DATAMAP ID: "+dataMap.get(ID));
        Toast toast = Toast.makeText(getApplicationContext(),"NAME: "+ dataMap.get(NAME)+ " ID: "+dataMap.get(SHOP_ID),
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();

0 个答案:

没有答案