所以我有一个自定义的ArrayAdapter,它具有这样定义的getView方法
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
int orderId = getItem(position).getId();
int menuId = getItem(position).getMenuId();
int userId = getItem(position).getUserId();
String status = getItem(position).getStatus();
LayoutInflater inflater = LayoutInflater.from(activity);
convertView = inflater.inflate(resource, parent, false);
TextView idView = convertView.findViewById(R.id.order_id);
TextView nameView = convertView.findViewById(R.id.item_name);
TextView userIdView = convertView.findViewById(R.id.user_id);
TextView statusView = convertView.findViewById(R.id.status_view);
ImageButton cancelButton = convertView.findViewById(R.id.order_canceled);
ImageButton orderDoneButton = convertView.findViewById(R.id.order_done);
cancelButton.setTag(orderId);
orderDoneButton.setTag(orderId);
if(status.equals("done"))
orderDoneButton.setImageResource(android.R.drawable.checkbox_on_background);
else {
orderDoneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orderRequest.getGenericRequest().setUri("/" + v.getTag() + "?status=done");
orderRequest.put();
// ((ImageButton)v).setImageResource(android.R.drawable.checkbox_on_background);
notifyDataSetChanged();
// TODO cheating but it works
// Intent intent = activity.getIntent();
// intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
// activity.startActivity(intent);
}
});
}
notifyDataSetChanged()
并且看到数据库已更新,我的列表视图也不会自动更新。为什么?Intent
”,它以我想要的方式工作,但是我不确定这是否是一个好方法,有什么建议吗? 答案 0 :(得分:0)
这里是我平时所做的一些解释
带有适配器的设置视图
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> listPName = new ArrayList<>();
private void showBluetoothPrinters() {
arrayAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, listPName) {
@NonNull
@Override
public View getView(int position, View convertView, @NotNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = view.findViewById(android.R.id.text1);
tv.setPadding(padding * 3, 0, padding * 3, 0);
return view;
}
};
lvPrinter.setAdapter(arrayAdapter);
}
然后在更新一些数据后调用notifyDataSetChanged()
Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();
if (mPairedDevices.size() > 0) {
for (BluetoothDevice mDevice : mPairedDevices) {
listPName.add(mDevice.getName());
}
}
if (arrayAdapter != null){
arrayAdapter.notifyDataSetChanged();
}
不知道这是否是您的期望
我在适配器中添加了一些代码段调用notifydatasetchage
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
int amount = mListData.get(position);
holder.tvAmount.setText(String.valueOf(amount));
holder.tvText.setText(decimalFormat.format(amount));
holder.itemView.setOnClickListener(v -> {
if (mListener != null){
mListener.onAmountTapped(amount);
}
tempSelected = position;
notifyDataSetChanged();
});
if (position == tempSelected){
holder.containerView.setBackground(mContext.getResources().getDrawable(R.drawable.rp_shadow_select));
} else {
holder.containerView.setBackground(mContext.getResources().getDrawable(R.drawable.rp_shadow));
}
}