我知道这是Listview和Checkbox的常见问题,我尝试从2天开始尝试,但仍无法解决,因此我在这里提出了问题。
我在Arraylist中有项目列表,例如15个(假定的)项目。在listview中有一个复选框,默认情况下,API(JSON响应)选中了11(假定)复选框 但是我只显示5个项目已选中。我尝试了很多,但找不到任何解决方案。
我创建了一个虚拟列表,其中包含来自API的11个项目,并从原始arraylist保存了5个项目。这是匹配列表视图中的项目并将此列表保存在“共享首选项”中的代码。
getViwe()函数内部的适配器类
List<SaveOrderId> getIds = prefManager.getNewOrders(context);
for (int i = 0; i < getIds.size(); i++) {
String idget = getIds.get(i).getOrderId();// it get ids from save list
for (int j = 0; j < newOrders.size(); j++) {
String bot = newOrders.get(j).getId(); // id from orginal list
if (bot.equalsIgnoreCase(idget)) {
showErrorLog(TAG, String.format("Bot id is %s and Saved Id is %s", bot, idget));
myViewHolder.add_payment.setChecked(true);
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
} else {
myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
}
myViewHolder.txt_check_status.setText("ADDED");
} else {
getPerspective().showErrorLog(TAG, "Not Equal");
myViewHolder.add_payment.setChecked(false);
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
} else {
myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
}
myViewHolder.txt_check_status.setText("ADD FOR PAYMENT");
}
}
}
适配器类代码在这里
public class NewOrderAdapter extends BaseAdapter {
private String TAG = "Current Order";
private Context context;
private List<NewOrder> newOrders;
private MyOrders myOrders;
private Perspective perspective;
private double total = 0.0;
private PrefManager prefManager;
// private List<SaveOrderId> ids;
private boolean isAdded = false;
private List<SaveOrderId> papa = new ArrayList<>();
private LayoutInflater inflater;
public NewOrderAdapter(Context context, List<NewOrder> newOrders, MyOrders myOrders) {
this.context = context;
this.newOrders = newOrders;
this.myOrders = myOrders;
prefManager = new PrefManager(this.context);
total = myOrders.total;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// ids = new ArrayList<>();
}
private void showDeleteAlert(final String orderId) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setTitle("Alert");
builder1.setMessage(context.getString(R.string.delete_order));
builder1.setCancelable(false);
builder1.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
myOrders.cancelOrder(orderId);
}
});
builder1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
@Override
public int getCount() {
return newOrders.size();
}
@Override
public Object getItem(int position) {
return newOrders.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder myViewHolder;
if (convertView == null) {
myViewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.custom_new_order_row, parent, false);
myViewHolder.img_delete = (ImageView) convertView.findViewById(R.id.img_delete);
myViewHolder.add_payment = (CheckBox) convertView.findViewById(R.id.add_payment);
myViewHolder.txt_topic_name = (TextView) convertView.findViewById(R.id.txt_topic_name);
myViewHolder.txt_order_id = (TextView) convertView.findViewById(R.id.txt_order_id);
myViewHolder.txt_delivery_date = (TextView) convertView.findViewById(R.id.txt_delivery_date);
myViewHolder.txt_words = (TextView) convertView.findViewById(R.id.txt_words);
myViewHolder.txt_order_status = (TextView) convertView.findViewById(R.id.txt_order_status);
myViewHolder.txt_offer_price = (TextView) convertView.findViewById(R.id.txt_offer_price);
myViewHolder.txt_price_tag = (TextView) convertView.findViewById(R.id.txt_price_tag);
myViewHolder.txt_check_status = (TextView) convertView.findViewById(R.id.txt_check_status);
myViewHolder.txt_total_amount = (TextView) convertView.findViewById(R.id.txt_total_amount);
myViewHolder.txt_payment_status = (TextView) convertView.findViewById(R.id.txt_payment_status);
myViewHolder.txt_paid_amount = (TextView) convertView.findViewById(R.id.txt_paid_amount);
myViewHolder.txt_view_more = (TextView) convertView.findViewById(R.id.txt_view_more);
myViewHolder.txt_order_tag = (TextView) convertView.findViewById(R.id.txt_order_tag);
myViewHolder.txt_layout_checked = (RelativeLayout) convertView.findViewById(R.id.txt_layout_checked);
myViewHolder.layout_payment_details = (RelativeLayout) convertView.findViewById(R.id.layout_payment_details);
myViewHolder.layout_paid = (LinearLayout) convertView.findViewById(R.id.layout_paid);
convertView.setTag(myViewHolder);
} else {
myViewHolder = (ViewHolder) convertView.getTag();
}
try {
myViewHolder.txt_topic_name.setText(newOrders.get(position).getTopic());
if (newOrders.get(position).getOrder_code().equalsIgnoreCase("Not allotted")
|| newOrders.get(position).getOrder_code().equalsIgnoreCase("")) {
myViewHolder.txt_order_tag.setText(context.getString(R.string.temp_id));
myViewHolder.txt_order_id.setText(newOrders.get(position).getId());
} else {
myViewHolder.txt_order_tag.setText(context.getText(R.string.order_code_));
myViewHolder.txt_order_id.setText(newOrders.get(position).getOrder_code());
}
myViewHolder.txt_words.setText(String.format("%s Words", newOrders.get(position).getWords()));
myViewHolder.txt_order_status.setText(newOrders.get(position).getProject_status());
myViewHolder.txt_offer_price.setText(String.format("%s %s %s", newOrders.get(position).getPrice_label(), newOrders.get(position).getSymbol(), newOrders.get(position).getDue()));
myViewHolder.txt_price_tag.setText(newOrders.get(position).getPayment_label());
myViewHolder.txt_total_amount.setText(String.format("%s %s", newOrders.get(position).getSymbol(), newOrders.get(position).getCost()));
String projectStatus = newOrders.get(position).getProject_status();
if (projectStatus.equalsIgnoreCase("in process")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.orange));
} else if (projectStatus.equalsIgnoreCase("delivered")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.green_text));
} else if (projectStatus.equalsIgnoreCase("pending")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.red));
} else if (projectStatus.equalsIgnoreCase("feedback")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.feedback));
} else if (projectStatus.equalsIgnoreCase("checked")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.checked));
} else if (projectStatus.equalsIgnoreCase("ready")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.ready));
} else if (projectStatus.equalsIgnoreCase("delivered draft")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.delivered_draft));
} else if (projectStatus.equalsIgnoreCase("Payment Refund")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.payment_refund));
} else if (projectStatus.equalsIgnoreCase("Not Accepted")) {
myViewHolder.txt_order_status.setTextColor(ContextCompat.getColor(context, R.color.black));
}
String payStatus = newOrders.get(position).getPay_status();
if (payStatus.equalsIgnoreCase("Pending")) {
myViewHolder.txt_payment_status.setText(payStatus);
myViewHolder.txt_payment_status.setTextColor(context.getResources().getColor(R.color.red));
}
if (payStatus.equalsIgnoreCase("Partially paid")) {
myViewHolder.txt_payment_status.setText(payStatus);
myViewHolder.txt_payment_status.setTextColor(context.getResources().getColor(R.color.orange));
}
if (payStatus.equalsIgnoreCase("Complete")) {
myViewHolder.txt_payment_status.setText(payStatus);
myViewHolder.txt_payment_status.setTextColor(context.getResources().getColor(R.color.green_text));
}
double paidAmt = newOrders.get(position).getPaid();
if (paidAmt == 0) {
myViewHolder.layout_paid.setVisibility(View.GONE);
} else {
myViewHolder.layout_paid.setVisibility(View.VISIBLE);
myViewHolder.txt_paid_amount.setText(String.format("%s %s", newOrders.get(position).getSymbol(), newOrders.get(position).getPaid()));
}
final String gateway_name = newOrders.get(position).getPayment_gateway_name();
myViewHolder.add_payment.setEnabled(false);
if (gateway_name.equalsIgnoreCase(prefManager.getCurrencyGateway())) {
if (!newOrders.get(position).getPay_status().equalsIgnoreCase("Complete")) {
double dueAmt = newOrders.get(position).getDue();
myViewHolder.layout_payment_details.setVisibility(View.VISIBLE);
if (dueAmt != 0) {
List<SaveOrderId> getIds = prefManager.getNewOrders(context);
for (int i = 0; i < getIds.size(); i++) {
String idget = getIds.get(i).getOrderId();
for (int j = 0; j < newOrders.size(); j++) {
String bhoot = newOrders.get(j).getId();
if (bhoot.equalsIgnoreCase(idget)) {
showErrorLog(TAG, String.format("Bhoot id is %s and Saved Id is %s", bhoot, idget));
myViewHolder.add_payment.setChecked(true);
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
} else {
myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
}
myViewHolder.txt_check_status.setText("ADDED");
} else {
showErrorLog(TAG, "Not Equal");
myViewHolder.add_payment.setChecked(false);
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
} else {
myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
}
myViewHolder.txt_check_status.setText("ADD FOR PAYMENT");
}
}
}
}
} else {
myViewHolder.layout_payment_details.setVisibility(View.GONE);
}
} else {
myViewHolder.add_payment.setChecked(false);
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
} else {
myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
}
myViewHolder.txt_check_status.setText("ADD FOR PAYMENT");
}
// prefManager.saveNewOrders(context, ids);
myViewHolder.txt_layout_checked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// myViewHolder.add_payment.performClick();
double dueAmt = newOrders.get(position).getDue();
if (dueAmt == 0.00) {
showErrorLog("Adapter", "No Order");
myViewHolder.add_payment.setChecked(false);
showAlert(context.getString(R.string.alert_header_warning), context.getString(R.string.alert_3_msg), "Ok");
} else {
if (!myViewHolder.add_payment.isChecked()) {
int size = prefManager.getNewOrders(context).size();
if (size < 6) {
String dis = newOrders.get(position).getId().concat(newOrders.get(position).getTopic());
if (size == 0) {
String orderid = newOrders.get(position).getId();
String payName = newOrders.get(position).getPayment_gateway_name();
SaveOrderId saveOrderId = new SaveOrderId(orderid, payName);
papa.add(saveOrderId);
prefManager.saveNewOrders(context, papa);
myViewHolder.add_payment.setChecked(true);
myViewHolder.txt_check_status.setText("ADDED");
String sym = newOrders.get(position).getSymbol();
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
} else {
myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
}
total = total + newOrders.get(position).getDue();
myOrders.setPrice(sym, total, payName, dis);
} else {
String orderid = newOrders.get(position).getId();
String payName = newOrders.get(position).getPayment_gateway_name();
List<SaveOrderId> getIds = prefManager.getNewOrders(context);
for (int i = 0; i < getIds.size(); i++) {
String name = getIds.get(i).getGateWayName();
if (name.equalsIgnoreCase(payName)) {
isAdded = true;
break;
} else {
isAdded = false;
}
}
if (isAdded) {
myViewHolder.txt_check_status.setText("ADDED");
String sym = newOrders.get(position).getSymbol();
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
} else {
myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
}
total = total + newOrders.get(position).getDue();
myOrders.setPrice(sym, total, payName, dis);
myViewHolder.add_payment.setChecked(true);
SaveOrderId saveOrderId = new SaveOrderId(orderid, payName);
papa.add(saveOrderId);
prefManager.saveNewOrders(context, papa);
} else {
showAlert(context.getString(R.string.alert_header_notic), context.getString(R.string.alert_1_msg), "Ok");
myViewHolder.add_payment.setChecked(false);
}
}
} else {
showMessage("Maximum limit reached.");
}
} else {
List<SaveOrderId> getIds = prefManager.getNewOrders(context);
String name = newOrders.get(position).getPayment_gateway_name();
String dis = newOrders.get(position).getId().concat(newOrders.get(position).getTopic());
if (getIds.size() != 0) {
for (int i = 0; i < getIds.size(); i++) {
String abc = getIds.get(i).getGateWayName();
if (abc.equalsIgnoreCase(name)) {
getIds.remove(i);
prefManager.saveNewOrders(context, getIds);
break;
}
}
}
if (prefManager.getNewOrders(context).size() == 0) {
papa.clear();
prefManager.clearOrderIds(context);
}
String sym = newOrders.get(position).getSymbol();
myViewHolder.txt_check_status.setText("ADD FOR PAYMENT");
myViewHolder.add_payment.setChecked(false);
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
} else {
myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
}
total = total - newOrders.get(position).getDue();
myOrders.setPrice(sym, total, name, dis);
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
class ViewHolder {
TextView txt_topic_name, txt_order_id, txt_delivery_date, txt_words, txt_order_status, txt_offer_price, txt_price_tag, txt_check_status,
txt_total_amount, txt_payment_status, txt_paid_amount, txt_view_more, txt_order_tag;
CheckBox add_payment;
ImageView img_delete;
RelativeLayout txt_layout_checked, layout_payment_details;
LinearLayout layout_paid;
}
}
问题在于,所有11个项目均显示为选中状态。请帮忙。