简体   繁体   中英

Close alert dialog with Listview with custom adapter

Iam working on a simple invoice app which invloes methods like selecting products. Im having the problem where i use custom adapter to listview to view my product details and since i use custom adapter listview onclicklistner is not working.

All i need is to close the alertdialog while i click and select a product from the listview.

I used customadapter to make listview dynamic.

Here is my code for alertdialog box,

    public void selectProduct(View v) {
    showProducts();
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    View convertView = (View) inflater.inflate(R.layout.select_product_list, null);
    alertDialog.setView(convertView);

    ArrayList<String> categoryListReverce = reverse(categoryList);
    categorySpinner = (Spinner) convertView.findViewById(R.id.spinner1);
    ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.orderlist_spinner_row, categoryListReverce);
    arrayAdapter.setDropDownViewResource(R.layout.orderlist_spinner_row);
    categorySpinner.setAdapter(arrayAdapter);
    categorySpinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
    ListView lv = (ListView) convertView.findViewById(R.id.listView);
    listAdapter = new CustomNewInvoiceAdapter(this, R.layout.batch_sales_list, new ArrayList<OrderListModel>());R.id.productprice});
    lv.setAdapter(listAdapter);
    textPartySearch = (EditText) convertView.findViewById(R.id.searchContainer);
    textContactSearch = (EditText) convertView.findViewById(R.id.contactno);
    footer = (LinearLayout) convertView.findViewById(R.id.footer1);
    getCashPartyName = (TextView) convertView.findViewById(R.id.partyname);
    getCashPartyContact = (TextView) convertView.findViewById(R.id.contactno);
    submit = (Button) convertView.findViewById(R.id.submit);

    final AlertDialog ad = alertDialog.show();
    textPartySearch.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            adapter2.getFilter()
                    .filter(textPartySearch.getText().toString());
        }
    });
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });

    footer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ad.dismiss();
        }
    });

}

And here is my customAdapter getView,

    @Override
public View getView(int position, View convertView, ViewGroup parent)  {
    View row = convertView;
     CustomNewInvoiceAdapter.Holder holder = null;

    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

    holder = new  CustomNewInvoiceAdapter.Holder();
    holder.pack = items.get(position);

    if (items.get(position).getType().equals("header")) {
        row = inflater.inflate(headerLayoutResourceId, parent, false);
        holder.itemName = row.findViewById(R.id.productName);
        holder.itemName.setText(String.valueOf(holder.pack.getItemName()));
        row.setTag(holder);

    } else {
        row = inflater.inflate(itemLayoutResourceId, parent, false);

        holder.itemCode = row.findViewById(R.id.itemCode);
        holder.itemName = row.findViewById(R.id.productName);
        holder.CostTxt = row.findViewById(R.id.ed_cost);
        holder.ItemDis = row.findViewById(R.id.ed_ItemDis);
        holder.unitBtn = row.findViewById(R.id.unitbtn);
        holder.stockTxt = row.findViewById(R.id.stock);
        clickUnitBtn(holder);
        if(Constants.isPriceEdit){
            holder.price = row.findViewById(R.id.productprice);
            holder.price.setClickable(false);
            holder.price.setFocusable(false);
            holder.price.setCursorVisible(false);
        }else {
            holder.price = row.findViewById(R.id.productprice);
            holder.price.setSelectAllOnFocus(true);
        }
        row.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            }
        });
        row.setTag(holder);

        setupItem(holder);
    }
    return row;
}

And here is a screenshot for reference

enter image description here

您是否能够按照代码中给出的方式关闭页脚单击时的警报对话框?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM