我正在使用自定义适配器处理列表视图。适配器具有子视图编辑文本。首先,如果我要在编辑文本中输入某个值,然后在搜索项之后输入另一个要输入值的子项,则子视图将包含先前的编辑文本值。我尝试了很多事情,但进展不顺利。如果有人可以帮助我,请这样做。在此先感谢
public class ProductListAdapter extends BaseAdapter implements Filterable {
public boolean count_flag=false;
private static final String TAG = ProductListAdapter.class.getSimpleName();
private List<ProductList> productList;
private List<ProductList> productFilterList;
private LayoutInflater mInflater;
private ValueFilter valueFilter;
private Context _context;
public String first_qty="";
private DecimalFormat decimalFormat = new DecimalFormat("##.00");
private ProductPartyCategory productPartyCategory = null;
private PartyWiseProductRate partyWiseProductRate = null;
public ProductListAdapter(Context context, List<ProductList> productList) {
this.productList = productList;
this.productFilterList = productList;
mInflater = LayoutInflater.from(context);
_context = context;
getFilter();
}
@Override
public int getCount() {
int size = 0;
try {
size = productList.size();
} catch (Exception e1) {
e1.printStackTrace();
}
return size;
}
@Override
public Object getItem(int position) {
return productList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
if(getCount() > 0){
return getCount();
}else{
return super.getViewTypeCount();
}
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView==null)
{
holder=new Holder();
convertView=LayoutInflater.from(parent.getContext()).inflate(R.layout.test_layout, null);
holder.tv_no_of_product=convertView.findViewById(R.id.tv_no_of_product);
holder.btn_add_product=convertView.findViewById(R.id.btn_add_product);
holder.btn_subtract_product=convertView.findViewById(R.id.btn_subtract_product);
holder.tv_product_code=convertView.findViewById(R.id.tv_product_code);
holder.txtvwProductName = convertView.findViewById(R.id.xtxtvwProductName);
holder.txtvwProductRate = convertView.findViewById(R.id.xtxtvwProductRate);
holder.txtvwProductIds = convertView.findViewById(R.id.xtxtvwProductIds);
holder.txtvwDiscountAndTax = convertView.findViewById(R.id.xtxtvwDiscountAndTax);
holder.imgvwProduct = convertView.findViewById(R.id.ximgvwProduct);
convertView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
String imageString = productList.get(position).getImage().trim();
if(count_flag)
{
}
else {
for(int i=0;i<Utility.pro_id_list.size();i++)
{
if(productList.get(position).getProductId().equals(Utility.pro_id_list.get(i).toString()))
{
String currentString = String.valueOf(Utility.cart_list.get(i).getQuantity());
String[] separated = currentString.split("\\.");
String test=separated[0]; // this will contain "Fruit"
//String test =String.valueOf(Utility.cart_list.get(i).getQuantity());
//test=test.substring(0,1);
holder.tv_no_of_product.setText(test);
}
}
}
holder.tv_product_code.setText(productList.get(position).getProductId());
holder.txtvwProductName.setText(productList.get(position).getProductName());
holder.txtvwProductIds.setText(productList.get(position).getProductId());
holder.txtvwProductRate.setText(decimalFormat.format(Double.valueOf(productList.get(position).getRate())));
holder.txtvwDiscountAndTax.setText("Dis:" + decimalFormat.format(Double.valueOf(productList.get(position).getDiscount())) + "% CGST:" + decimalFormat.format(Double.valueOf(productList.get(position).getCGST())) + "% SGST:" + decimalFormat.format(Double.valueOf(productList.get(position).getSGST())) + "% IGST:" + decimalFormat.format(Double.valueOf(productList.get(position).getIGST())) + "%");
if(imageString.isEmpty())
{
holder.imgvwProduct.setImageResource(R.drawable.ic_menu_item);
}
else {
holder.imgvwProduct.setImageBitmap(Utility.getBitmapFromImageString(imageString));
}
holder.tv_no_of_product.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowQualityDailog(holder.tv_no_of_product,position);
}
});
holder.btn_add_product.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Utility.search_flag=false;
Utility.pro_count=1;
int initail_value= Integer.parseInt(holder.tv_no_of_product.getText().toString().trim());
++initail_value;
holder.tv_no_of_product.setText(String.valueOf(initail_value));
//Log.e("ok", String.valueOf(productList.get(position).getProductId()));
String selectedProduct =productList.get(position).toString();
try {
JSONObject joSelectedProduct = new JSONObject(selectedProduct);
String selectedProductId = Utility.getStringDataFromJsonKey(joSelectedProduct, "productId");
if(Utility.cart_list.size()>0)
{
for(int i=0;i<Utility.cart_list.size();i++)
{
if(Utility.cart_list.get(i).getId().equalsIgnoreCase(selectedProductId))
{
Product product=new Product();
product.setId(Utility.cart_list.get(i).getId());
product.setBranch(Utility.cart_list.get(i).getBranch());
product.setCGSTRate(Utility.cart_list.get(i).getCGSTRate());
product.setDiscount(Utility.cart_list.get(i).getDiscount());
product.setDiscountAmount(Utility.cart_list.get(i).getDiscountAmount());
product.setGroup(Utility.cart_list.get(i).getGroup());
product.setIGSTRate(Utility.cart_list.get(i).getIGSTRate());
product.setImage(Utility.cart_list.get(i).getImage());
product.setInclusive(Utility.cart_list.get(i).getInclusive());
product.setIsActive(Utility.cart_list.get(i).getIsActive());
product.setName(Utility.cart_list.get(i).getName());
product.setNarr(Utility.cart_list.get(i).getNarr());
product.setQuantity(Double.valueOf( holder.tv_no_of_product.getText().toString().trim()));
product.setSerialNumber(Utility.cart_list.get(i).getSerialNumber());
product.setSGSTRate(Utility.cart_list.get(i).getSGSTRate());
product.setsRate(Utility.cart_list.get(i).getsRate());
product.setTouchValue(Utility.cart_list.get(i).getTouchValue());
product.setUnit(Utility.cart_list.get(i).getUnit());
Double totalamount=Utility.calculateExclusiveTotal(product.getQuantity(),product.getsRate(), product.getCGSTRate(),product.getDiscount(),product.getSGSTRate(),product.getIGSTRate());
product.setAmount(totalamount);
Utility.cart_list.set(i,product);
break;
}
else {
if(Utility.pro_id_list.contains(selectedProductId))
{
}
else {
new LoadProductPartyData().execute(selectedProductId,selectedProduct, holder.tv_no_of_product.getText().toString().trim());
break;
}
}
}
}
else {
new LoadProductPartyData().execute(selectedProductId,selectedProduct, holder.tv_no_of_product.getText().toString().trim());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
holder.btn_subtract_product.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Utility.search_flag=false;
Utility.pro_count=1;
int initail_value= Integer.parseInt( holder.tv_no_of_product.getText().toString().trim());
if(initail_value==0)
{
}
else {
--initail_value;
holder.tv_no_of_product.setText(String.valueOf(initail_value));
}
String selectedProduct =productList.get(position).toString();
try {
JSONObject joSelectedProduct = new JSONObject(selectedProduct);
String selectedProductId = Utility.getStringDataFromJsonKey(joSelectedProduct, "productId");
if(Utility.cart_list.size()>0)
{
for(int i=0;i<Utility.cart_list.size();i++)
{
if(Utility.cart_list.get(i).getId().equals(selectedProductId))
{
Product product=new Product();
product.setId(Utility.cart_list.get(i).getId());
// product.setAmount(Utility.cart_list.get(i).getAmount());
product.setBranch(Utility.cart_list.get(i).getBranch());
product.setCGSTRate(Utility.cart_list.get(i).getCGSTRate());
product.setDiscount(Utility.cart_list.get(i).getDiscount());
product.setDiscountAmount(Utility.cart_list.get(i).getDiscountAmount());
product.setGroup(Utility.cart_list.get(i).getGroup());
product.setIGSTRate(Utility.cart_list.get(i).getIGSTRate());
product.setImage(Utility.cart_list.get(i).getImage());
product.setInclusive(Utility.cart_list.get(i).getInclusive());
product.setIsActive(Utility.cart_list.get(i).getIsActive());
product.setName(Utility.cart_list.get(i).getName());
product.setNarr(Utility.cart_list.get(i).getNarr());
product.setQuantity(Double.valueOf( holder.tv_no_of_product.getText().toString().trim()));
product.setSerialNumber(Utility.cart_list.get(i).getSerialNumber());
product.setSGSTRate(Utility.cart_list.get(i).getSGSTRate());
product.setsRate(Utility.cart_list.get(i).getsRate());
product.setTouchValue(Utility.cart_list.get(i).getTouchValue());
product.setUnit(Utility.cart_list.get(i).getUnit());
Double totalamount=Utility.calculateExclusiveTotal(product.getQuantity(),product.getsRate(), product.getCGSTRate(),product.getDiscount(),product.getSGSTRate(),product.getIGSTRate());
product.setAmount(totalamount);
Utility.cart_list.set(i,product);
break;
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return convertView;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
public void ShowQualityDailog(TextView tv_qty_count,int position)
{
AlertDialog.Builder builder=new AlertDialog.Builder(_context);
//builder.setTitle("Add Quantity");
View view=LayoutInflater.from(_context).inflate(R.layout.qty_layout,null);
builder.setView(view);
TextView tv_cancel,tv_ok;
tv_cancel=view.findViewById(R.id.tv_cancel);
tv_ok=view.findViewById(R.id.tv_ok);
EditText edt_qty;
edt_qty=view.findViewById(R.id.edt_qty);
AlertDialog alertDialog=builder.create();
alertDialog.show();
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
tv_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Utility.search_flag=false;
Utility.pro_count=1;
if(!edt_qty.getText().toString().trim().equals(""))
{
count_flag=true;
tv_qty_count.setText(edt_qty.getText().toString().trim());
int initail_value= Integer.parseInt(tv_qty_count.getText().toString().trim());
tv_qty_count.setText(String.valueOf(initail_value));
//Log.e("ok", String.valueOf(productList.get(position).getProductId()));
String selectedProduct =productList.get(position).toString();
try {
JSONObject joSelectedProduct = new JSONObject(selectedProduct);
String selectedProductId = Utility.getStringDataFromJsonKey(joSelectedProduct, "productId");
if(Utility.cart_list.size()>0)
{
for(int i=0;i<Utility.cart_list.size();i++)
{
if(Utility.cart_list.get(i).getId().equalsIgnoreCase(selectedProductId))
{
//Log.e("ok","update product");
Product product=new Product();
product.setId(Utility.cart_list.get(i).getId());
product.setBranch(Utility.cart_list.get(i).getBranch());
product.setCGSTRate(Utility.cart_list.get(i).getCGSTRate());
product.setDiscount(Utility.cart_list.get(i).getDiscount());
product.setDiscountAmount(Utility.cart_list.get(i).getDiscountAmount());
product.setGroup(Utility.cart_list.get(i).getGroup());
product.setIGSTRate(Utility.cart_list.get(i).getIGSTRate());
product.setImage(Utility.cart_list.get(i).getImage());
product.setInclusive(Utility.cart_list.get(i).getInclusive());
product.setIsActive(Utility.cart_list.get(i).getIsActive());
product.setName(Utility.cart_list.get(i).getName());
product.setNarr(Utility.cart_list.get(i).getNarr());
product.setQuantity(Double.valueOf(tv_qty_count.getText().toString().trim()));
product.setSerialNumber(Utility.cart_list.get(i).getSerialNumber());
product.setSGSTRate(Utility.cart_list.get(i).getSGSTRate());
product.setsRate(Utility.cart_list.get(i).getsRate());
product.setTouchValue(Utility.cart_list.get(i).getTouchValue());
product.setUnit(Utility.cart_list.get(i).getUnit());
Double totalamount=Utility.calculateExclusiveTotal(product.getQuantity(),product.getsRate(), product.getCGSTRate(),product.getDiscount(),product.getSGSTRate(),product.getIGSTRate());
product.setAmount(totalamount);
Utility.cart_list.set(i,product);
break;
}
else {
if(Utility.pro_id_list.contains(selectedProductId))
{
}
else {
new LoadProductPartyData().execute(selectedProductId,selectedProduct,tv_qty_count.getText().toString().trim());
break;
}
}
}
}
else {
new LoadProductPartyData().execute(selectedProductId,selectedProduct,tv_qty_count.getText().toString().trim());
}
alertDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
else {
Toast.makeText(_context,"Enter Quantity!",Toast.LENGTH_SHORT).show();
}
}
});
}
public static class Holder {
//tv_no_of_product.setOnClickListener(this);
TextView tv_product_code;
TextView tv_no_of_product,txtvwProductName,txtvwProductRate, txtvwDiscountAndTax,txtvwProductIds;
ImageView imgvwProduct;
Button btn_subtract_product,btn_add_product;
}
private class ValueFilter extends Filter {
//Invoked in a worker thread to filter the data according to the constraint.
@Override
protected FilterResults performFiltering(CharSequence constraint) {
//tv_no_of_product
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
List<ProductList> filterList = new ArrayList<>();
for (int i = 0; i < productFilterList.size(); i++) {
if (productFilterList.get(i).getProductName().toString().toLowerCase().contains(constraint.toString().toLowerCase()) || productFilterList.get(i).getProductId().toString().toLowerCase().contains(constraint.toString().toLowerCase())) {
filterList.add(productFilterList.get(i));
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = productFilterList.size();
results.values = productFilterList;
}
//
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
productList = (List<ProductList>) results.values;
//Holder.tv_product_code.setText("");
notifyDataSetChanged();
}
}
您必须更新列表和notifyadapterset()
list.clear()adapter.notifyDataSetChanged()