onItemClick与数组列表的toast内容

时间:2016-05-23 18:50:32

标签: android-viewholder android-toast onitemclick

我需要的是一个toast或类似内容的数组列表,当我点击LinearLayout项目时会打开。但我不知道如何使用持有人视图来实现它。

public class CanteenListAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
    private ArrayList<CanteenMenu> listData;
    private LayoutInflater layoutInflater;
    private Context context;

    public CanteenListAdapter(ArrayList<CanteenMenu> listData, Context bContext){
        this.listData = listData;
        layoutInflater = LayoutInflater.from(bContext);
        this.context = bContext;

        Collections.sort(this.listData);
    }

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

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

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

    @Override
    public View getView(int position, View cView, ViewGroup parent) {
        ViewHolder holder;
        if(cView==null){
            cView = layoutInflater.inflate(R.layout.activity_canteen_tab_listitem, null);
            holder = new ViewHolder();
            holder.date = (TextView) cView.findViewById(R.id.canteen_header);
            holder.ll = (LinearLayout) cView.findViewById(R.id.canteen_linearLayout);
            int j = 0;
                for (int i = 0; i < listData.get(position).getCategory().length; i++) {
                    TextView description = new TextView(context);
                    TextView category = new TextView(context);
                    description.setText(listData.get(position).getName()[i]);
                    category.setText(listData.get(position).getCategory()[i]);
                    holder.ll.addView(category, j);
                    holder.ll.addView(description,j + 1);
                    j += 2;
                }

            cView.setTag(holder);
        } else{
            holder = (ViewHolder) cView.getTag();
        }


        int j = 0;
        for(int i = 0; i < listData.get(position).getCategory().length; i++) {
            if(holder.ll.getChildAt(j) != null) {
                ((TextView) holder.ll.getChildAt(j)).setText(listData.get(position).getCategory()[i]);
                ((TextView) holder.ll.getChildAt(j + 1)).setText(listData.get(position).getName()[i]);
            }
            j += 2;
        }
        return cView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


    }
    static class ViewHolder {
        LinearLayout ll;
        TextView date;
        TextView name;
        TextView category;
    }
}

有人可以帮忙吗?

0 个答案:

没有答案