【发布时间】:2020-10-09 18:46:46
【问题描述】:
我将一个文本视图放在一个数组列表中,我必须单击该 texview 以便显示该值。我希望无需单击文本视图即可显示该值。当它的值改变时,texview中显示的是旧值,我必须点击textview才能更新值
所有显示都很好,除了:
holder.mTextprix.setText(CustomerMapActivity.calculate_item_price);
这是我的代码
负责在 CustomerActivity.class 中显示汽车类型的适配器
public class TypeAdapter extends RecyclerView.Adapter<TypeAdapter.viewHolders> {
private Context context;
private TypeObject selectedItem;
private List<TypeObject> itemArrayList;
public TypeAdapter(List<TypeObject> itemArrayList, Context context) {
this.itemArrayList = itemArrayList;
selectedItem = itemArrayList.get(0);
this.context = context;
}
@Override
public TypeAdapter.viewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutView.setLayoutParams(lp);
TypeAdapter.viewHolders rcv = new TypeAdapter.viewHolders(layoutView);
return rcv;
}
/**
* Bind view to holder, setting the text to
* the design elements
* @param position - current position of the recyclerView
*/
@Override
public void onBindViewHolder(final @NonNull viewHolders holder, int position) {
holder.mName.setText(itemArrayList.get(position).getName());
holder.mPeople.setText(String.valueOf(itemArrayList.get(position).getPeople()));
holder.mImage.setImageDrawable(itemArrayList.get(position).getImage());
holder.mTextprix.setText(CustomerMapActivity.calculate_item_price);
if(selectedItem.equals(itemArrayList.get(position))){
holder.mLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent2));
}else{
holder.mLayout.setBackgroundColor(context.getResources().getColor(R.color.white));
}
holder.mLayout.setOnClickListener(v -> {
selectedItem = itemArrayList.get(holder.getAdapterPosition());
notifyDataSetChanged();
});
}
public TypeObject getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(TypeObject selectedItem) {
this.selectedItem = selectedItem;
}
@Override
public int getItemCount() {
return this.itemArrayList.size();
}
/**
* Responsible for handling the data of each view
*/
class viewHolders extends RecyclerView.ViewHolder {
TextView mName,
mPeople,
mTextprix;
ImageView mImage;
LinearLayout mLayout;
viewHolders(View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.image);
mPeople = itemView.findViewById(R.id.people);
mName = itemView.findViewById(R.id.name);
mLayout = itemView.findViewById(R.id.layout);
mTextprix = itemView.findViewById(R.id.prixprix);
}
}
}
public class Utils {
/**
* Round a float value to a specific decimal place
* @param amount - the value to round
* @param decimalPlace - to what decimal place to round the amount to
* @return rounded number
*/
public BigDecimal round(float amount, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(amount));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd;
}
/**
* Returns array list with all of the driver rides available for this
* application.
* @param activity - activity that called this function
* @return typeArrayList - array list with all the driver types
*/
public static ArrayList<TypeObject> getTypeList(Activity activity){
ArrayList<TypeObject> typeArrayList = new ArrayList<>();
typeArrayList.add(new TypeObject("type_1", activity.getResources().getString(R.string.type_1), activity.getResources().getDrawable(R.drawable.ic_type_1), 3));
return typeArrayList;
}
public static TypeObject getTypeById(Activity activity, String id){
ArrayList<TypeObject> typeArrayList = getTypeList(activity);
for(TypeObject mType : typeArrayList){
if(mType.getId().equals(id)){
return mType;
}
}
return null;
}
}
与
public class TypeObject {
String name, id;
Drawable image;
int people;
public TypeObject(String id, String name, Drawable image, int people){
this.id = id;
this.name = name;
this.image = image;
this.people = people;
}
public String getId() {
return id;
}
public Drawable getImage() {
return image;
}
public String getName() {
return name;
}
public int getPeople() {
return people;
}
}
【问题讨论】:
-
mLayout 中文本的颜色是什么我认为它可能是白色的。如果它是白色的,请尝试将其更改为其他颜色
-
我试过了,但不是这样
-
你能用完整的适配器代码更新你的问题吗?
-
是的,我刚刚做到了。可以试试看吗?
标签: java android performance arraylist textview