从自定义适配器获取父布局

时间:2015-01-25 18:46:09

标签: java android listview adapter custom-adapter

有人能解释我,如何实现? 我有一个listview和footer的活动,包含一些元素(textview)。

使用自定义适配器构建的Listview。每个listview项都有很少的元素。我的问题是:当我点击某个listview的元素时,如何在自定义适配器中更改页脚中的textview?

很多!

/ ****我的适配器**** /

public class MyListAdapter extends ArrayAdapter<Product> implements UndoAdapter {

    private final Context mContext;
    private HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
    ArrayList<Product> products = new ArrayList<Product>();
    final int INVALID_ID = -1;
    LayoutInflater lInflater;
    String imagePath;

    public MyListAdapter(Context context, int textViewResourceId, List<Product> prod) {
        //super(context, textViewResourceId, prod);
        super(prod);
        lInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
        for (int i = 0; i < prod.size(); i++) {
            //add(prod.get(i));
            mIdMap.put(prod.get(i),i);
        }
    }

    @Override
    public long getItemId(final int position) {
        //return getItem(position).hashCode();
        Product item = (Product) getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        ViewHolder holder = null;;
        Product p = getItem(position);

        if (convertView == null) {

            convertView = lInflater.inflate(R.layout.item, null);
            //convertView.setBackgroundResource(R.drawable.rounded_corners);

            int currentTheme = Utils.getCurrentTheme(convertView.getContext());
            switch (currentTheme) {

                case 0:
                        convertView.setBackgroundResource(R.drawable.rounded_corners);
                    break;
                case 1:
                        convertView.setBackgroundResource(R.drawable.border);
                    break;
                default:
                        convertView.setBackgroundResource(R.drawable.rounded_corners);
                    break;

            }

            holder = new ViewHolder();

            holder.tvDescr = (TextView) convertView.findViewById(R.id.tvDescr);
            holder.list_image = (ImageView) convertView.findViewById(R.id.list_image);
            holder.products_amount = (TextView) convertView.findViewById(R.id.amountDigits);
            holder.products_price = (TextView) convertView.findViewById(R.id.priceDigits);
            holder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
            holder.unit = (TextView) convertView.findViewById(R.id.unit);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }


        if(p.getProductImageBitmap() != null && p.getProductImageBitmap() != "") {
            Log.d("PATH -- ", p.getProductImageBitmap());
            ImageLoader imageLoader = ImageLoader.getInstance();
            DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                    .resetViewBeforeLoading(true)
                    .showImageForEmptyUri(R.drawable.ic_launcher)
                    .showImageOnFail(R.drawable.ic_launcher)
                                                                        /*.showImageOnLoading(R.id.progress_circular)*/
                    .build();
            imageLoader.displayImage(p.getProductImageBitmap(), holder.list_image, options);
        } else {
            holder.list_image.setImageResource(R.drawable.ic_launcher);
        }


        holder.tvDescr.setText(p.getProductName());

        holder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String deletedItem = getItem(position).getProductName();
                MyListAdapter.this.remove(getItem(position));

                if (MyListAdapter.this.getCount() > 0) {

                    Toast.makeText(mContext, deletedItem + " " + mContext.getString(R.string.deleted_item), Toast.LENGTH_SHORT).show();
                    MyListAdapter.this.notifyDataSetChanged();

                } else {

                    Toast.makeText(mContext,mContext.getString(R.string.sklerolist_empty), Toast.LENGTH_SHORT).show();

                }

            }
        });

        //Функционал для большой картинки продукта
        //открывается новое активити с большой картинкой
        holder.list_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imagePath = getItem(position).getProductImageBitmap();
                if(imagePath != null && imagePath != "") {
                    Pattern normalPrice = Pattern.compile("^file");
                    Matcher m2 = normalPrice.matcher(imagePath);
                    if (m2.find()) {
                        Intent myIntent = new Intent(view.getContext(), ViewImage.class).putExtra("imagePath", imagePath);
                        view.getContext().startActivity(myIntent);
                    }
                }
            }
        });

        holder.products_price.setText(fmt(p.getProductPrice()));
        holder.products_amount.setText(fmt(p.getProductAmount()));
        holder.unit.setText(p.getProductUnit());

        return convertView;
    }

    public static String fmt(double d){
        if(d == (long) d)
            return String.format("%d",(long)d);
        else
            return String.format("%s",d);
    }

    static class ViewHolder {
        ImageView list_image;
        TextView tvDescr;
        TextView products_amount;
        TextView products_price;
        TextView unit;
        ImageView ivImage;
        ProgressBar circleProgress;
    }

    @NonNull
    @Override
    public View getUndoView(final int position, final View convertView, @NonNull final ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            //view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false);
            view = lInflater.inflate(R.layout.undo_row, parent, false);
        }
        return view;
    }

    @NonNull
    @Override
    public View getUndoClickView(@NonNull final View view) {
        return view.findViewById(R.id.undo_row_undobutton);
    }

    public View getHeaderView(final int position, final View convertView, final ViewGroup parent) {
        TextView view = (TextView) convertView;
        //View view = convertView;
        if (view == null) {
            //view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_header, parent, false);
            //view = lInflater.inflate(R.layout.list_header, parent, false);
        }

        //view.setText(mContext.getString(R.string.header, getHeaderId(position)));

        return view;
    }

    public long getHeaderId(final int position) {
        return position / 10;
    }

}

1 个答案:

答案 0 :(得分:3)

您的ListView有一个监听列表元素上的点击事件。

@Override  
public void onListItemClick(ListView l, View v, int position, long id) {
    // Do something when a list item is clicked 
} 

但是如果你想将其他东西从适配器传回到Activity或包含FragmentListView的{​​{1}},你应该创建一个简单的界面并将其设置为适配器的侦听器。之后,在适配器中设置行上的点击事件,并使用您自己的界面通知AdapterActivity

例如,您的界面定义如下

Fragment

在您的public interface OnItemClickedCustomAdapter { public void onClick(ItemPosition position); } 课程中,您将拥有一名私人会员

Adapter

以及用于设置侦听器的方法

private OnItemClickedCustomAdapter mListener;

从定义了public void setOnItemClickedCustomAdapter(OnItemClickedCustomAdapter listener){ this.mListener = listener; } 的{​​{1}}或Activity开始,您的适配器设置完毕后,您就可以Fragment作为ListView参数,你去。您的活动现在将收听您的活动。要触发事件,只需从自定义适配器调用setOnItemClickedCustomAdapter即可。您可以将所需的数据传回thismListener.onClick(),然后您可以直接访问ActivityFragment,并可以更改它们。