Android HorizontalListView onItemClick not working

≡放荡痞女 提交于 2019-12-11 16:31:58

问题


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

来源:https://stackoverflow.com/questions/15334893/android-horizontallistview-onitemclick-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!