ListView OnClick特定项目不起作用

时间:2014-03-31 19:36:22

标签: java android

我有一个ListViewAdapter,我试图设置OnItemClick然而它不能正常工作,这是我第三次尝试以不同的方式工作,但仍然没有运气,我将发布我的整个ListViewAdpater代码。

    public class ListViewAdapter extends BaseAdapter {

    Context mContext;
    String [] productTitleArray;

    LayoutInflater inflater;
    private List<ClassproductList> classproductlist = null;
    private ArrayList<ClassproductList> arraylist;

    public ListViewAdapter(Context context, List<ClassproductList> classproductlist) {
        mContext = context;
        this.classproductlist = classproductlist;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<ClassproductList>();
        this.arraylist.addAll(classproductlist);
    }

    public class ViewHolder {        
        TextView title;
        TextView description;
        ImageView photo;
    }

    @Override
    public int getCount() {
        return classproductlist.size();
    }

    @Override
    public ClassproductList getItem(int position) {
        return classproductlist.get(position);
    }

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

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.list_view_single_row, null);
            holder.title = (TextView) view.findViewById(R.id.textLarge);
            holder.description = (TextView) view.findViewById(R.id.textSmall);
            holder.photo= (ImageView) view.findViewById(R.id.imageThumb);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.title.setText(classproductlist.get(position).getTitle());
        holder.description.setText(classproductlist.get(position).getDescription());
        holder.photo.setImageResource(classproductlist.get(position).getImage());



        Resources res = mContext.getResources();
        productTitleArray = res.getStringArray(R.array.titles_product);


        ListViewAdapter myData = new ListViewAdapter(mContext, R.layout.list_view, myData);
            ListView listView = (ListView) findViewById(R.id.listview1);
            listView.setAdapter(myData);

            listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch(position) {
            case 1:
                    Log.d("Msg", "Found Product 1");
            break;
            case 2:
                    Log.d("Msg", "Found Product 2");

                   break;
            default:
            }
            }

        });

        return view;
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        classproductlist.clear();
        if (charText.length() == 0) {
            classproductlist.addAll(arraylist);
        }
        else
        {
            for (ClassproductList wp : arraylist)
            {
                if (wp.getTitle().toLowerCase(Locale.getDefault()).contains(charText))
                {
                    classproduclist.add(wp);
                    classproductlist.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }
}

问题在于此代码,但不知道如何解决此问题:

    ListViewAdapter myData = new ListViewAdapter(mContext, R.layout.list_view, myData);
    ListView listView = (ListView) findViewById(R.id.listview1);
    listView.setAdapter(myData);

    listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    switch(position) {
    case 1:
            Log.d("Msg", "Found Product 1");
    break;
    case 2:
            Log.d("Msg", "Found Product 2");

           break;
    default:
    }
    } 
});

0 个答案:

没有答案