Android HorizontalListView onItemClick not working

2019-09-03 10:26发布

问题:

I have a problem regarding the clicking of the item. Whenever i click or touch, the application stops. I wonder what's wrong with my code. This is the part of my code. The RowItem is a POJO. Then MyCustomBaseAdapter extends 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();