gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:06 25 4
gpt4 key购买 nike

谁能给我解释一下,怎么实现的?我有一个带有 ListView 和页脚的 Activity ,其中包含一些元素( 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;
}

}

最佳答案

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

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

但是,如果您想将其他内容从适配器传递回 Activity 或包含 ListViewFragment适配器,您应该创建一个简单的接口(interface)并将其设置为适配器的监听器。之后,从适配器中在您的行上设置点击事件,并使用您自己的界面通知 ActivityFragment

例如你有这样定义的接口(interface)

public interface OnItemClickedCustomAdapter {
public void onClick(ItemPosition position);
}

并且在您的Adapter 类中,您将拥有一个私有(private)成员

private OnItemClickedCustomAdapter mListener;

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

public void setOnItemClickedCustomAdapter(OnItemClickedCustomAdapter listener){
this.mListener = listener;
}

从定义了 ListViewActivityFragment 中,并且设置了适配器,您将能够调用 setOnItemClickedCustomAdapterthis 作为参数,然后就可以了。您的 Activity 现在将监听您的事件。要触发事件,只需从您的自定义适配器调用 mListener.onClick()。您可以将需要的数据传回 ActivityFragment,然后您可以从那里访问您的 HeaderFooter 直接修改上面的文字。

关于java - 从自定义适配器获取父布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28140069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com