无法在自定义对话框Android中隐藏Listview中的按钮

时间:2016-04-30 04:42:17

标签: android listview adapter

在第一个活动中,我在列表视图中有一个按钮(命名为btIndividual),从那个自定义对话框布局打开。在该自定义对话框中,我在listview中有一个text和edittext字段,一个保存Button用于将数据发布到服务器。现在我希望在将数据发布到服务器后,第一个活动中名为btIndividual的按钮将不可见。

第一项活动的自定义适配器:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ProductChoosed productChoosed = productChoosedAr.get(position);


        convertView = View.inflate(SolutionActivity.this, R.layout.custom_solution_row, null);
        ImageView categoryImageView = (ImageView) convertView.findViewById(R.id.categoryImageView);
        TextView categoryNameTextView = (TextView) convertView.findViewById(R.id.categoryNameTextView);
        productsListTextView = (TextView) convertView.findViewById(R.id.productsListTextView);
        btIndividual = (Button) convertView.findViewById(R.id.btIndividual);

        String catgoryImage = "";
        String isTradeProduct = productChoosed.isTradeProduct;
        if(isTradeProduct.equals("0")){
            btIndividual.setVisibility(View.VISIBLE);
            productsListTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showTradDialog(position, productChoosedAr.get(position));
                }
            });
        }else{
            btIndividual.setVisibility(View.GONE);
        }

        btIndividual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
showIndividualTradDialog(position,productChoosedAr.get(position));
                individual_productChoosedAr.clear();
                myList.clear();
                idIndividual = "";
                mIndCount =1;
                checkHideButton = position ;
                checkButtonPosition = position;
                idIndividual = productChoosedAr.get(position).categoryId;
                GetIndividualProducts getIndividualProducts = new   GetIndividualProducts();
                getIndividualProducts.execute();
                showDialog();
                Toast toast = Toast.makeText(getApplicationContext(),"Loading...",Toast.LENGTH_LONG);
                toast.show();
            }
});

自定义对话框布局:

private void showDialog(){

    dialog1 = new Dialog(this);
    final Dialog tradDialog = new Dialog(this, android.R.style.Theme_Light_NoTitleBar);

    View view = getLayoutInflater().inflate(R.layout.trad_dialog_layout_individual, null);
    tradDialog.setCanceledOnTouchOutside(false);
    lv = (ListView) view.findViewById(R.id.productsListView);
    RelativeLayout saveBtnLayout = (RelativeLayout) view.findViewById(R.id.saveBtnLayout);
    // Change MyActivity.this and myListOfItems to your own values
    clad = new CustomListAdapterDialog(SolutionActivity.this, individual_productChoosedAr);

    lv.setAdapter(clad);
    clad.notifyDataSetChanged();
    mCount = lv.getChildCount();
    saveBtnLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int getChildCount1 = lv.getChildCount();
            System.out.print(getChildCount1);

            for (int i = 0; i < myList.size(); i++) {
//                        v = lv.getChildAt(i);
//                        etPrice = (EditText) v.findViewById(R.id.etPrice);
                if(myList.get(i).toString().equals("")){
                    ProductPrice = "NULL";
                }else {
                    ProductPrice = myList.get(i).toString();
                }
//                        if(ProductPrice.equals("")){
//                            ProductPrice = "NULL";
//                        }
                    productPriceAr.add(ProductPrice);
                }
            Toast toast = Toast.makeText(getApplicationContext(),"Please wait...",Toast.LENGTH_LONG);
            toast.show();
            SendIndividualDatatoServer sendIndividualData = new SendIndividualDatatoServer();
            sendIndividualData.execute();
        }
    });
    //lv.setOnItemClickListener(........);

    dialog1.setContentView(view);
    dialog1.show();

}

AsyncTask类,用于发布我想要禁用btIndividual按钮的数据:

 protected void onPostExecute(Void paramVoid) {
        super.onPostExecute(paramVoid);

        try {
            String typeId = "", messageReceived = "";
            JSONObject localJSONObject = new JSONObject(this.sendDataResponse);
            typeId = localJSONObject.getString("type_id");
            messageReceived = localJSONObject.getString("msg");
            if (typeId.equals("1")) {

//if i reached here i want to disable that button 
//                   if(checkHideButton == checkButtonPosition){
//                       btIndividual.setVisibility(View.GONE);
//                           customSelectedProductsAdapter.notifyDataSetChanged();
//                           customSelectedProductsAdapter.notifyDataSetInvalidated();
//                   }
                Toast toast =   Toast.makeText(SolutionActivity.this,"Individual Data   Posted",Toast.LENGTH_LONG);
                toast.show();
                dialog1.dismiss();
                customSelectedProductsAdapter.notifyDataSetChanged();
                productPriceAr.clear();
                individual_productChoosedAr.clear();

            } else
                Toast.makeText(SolutionActivity.this, messageReceived, Toast.LENGTH_SHORT).show();
            btIndividual.setVisibility(View.VISIBLE);
        } catch (Exception localException) {
            localException.printStackTrace();
            Toast.makeText(SolutionActivity.this, "Network Error Occured", Toast.LENGTH_SHORT).show();
        }

1 个答案:

答案 0 :(得分:0)

我想问题出在这里:

} else
Toast.makeText(SolutionActivity.this, messageReceived, Toast.LENGTH_SHORT).show();
btIndividual.setVisibility(View.VISIBLE);

即使您的条件为真,也可以将按钮设置为可见。

我建议修改代码如下:

protected void onPostExecute(Void paramVoid) {
    super.onPostExecute(paramVoid);

    try {

        //1. By default button is visible
        btIndividual.setVisibility(View.VISIBLE);

        String typeId = "", messageReceived = "";
        JSONObject localJSONObject = new JSONObject(this.sendDataResponse);
        typeId = localJSONObject.getString("type_id");
        messageReceived = localJSONObject.getString("msg");
        if (typeId.equals("1")) {

               if(checkHideButton == checkButtonPosition){

                   //2. if condition is true - hide the button
                   btIndividual.setVisibility(View.GONE);
                   customSelectedProductsAdapter.notifyDataSetChanged();
                   customSelectedProductsAdapter.notifyDataSetInvalidated();
               }
            Toast toast =   Toast.makeText(SolutionActivity.this,"Individual Data   Posted",Toast.LENGTH_LONG);
            toast.show();
            dialog1.dismiss();
            customSelectedProductsAdapter.notifyDataSetChanged();
            productPriceAr.clear();
            individual_productChoosedAr.clear();

        } else
            Toast.makeText(SolutionActivity.this, messageReceived, Toast.LENGTH_SHORT).show();

        //3. it is not needed
        //btIndividual.setVisibility(View.VISIBLE);
    } catch (Exception localException) {
        localException.printStackTrace();
        Toast.makeText(SolutionActivity.this, "Network Error Occured", Toast.LENGTH_SHORT).show();
    }