在获取保存响应时禁用列表视图行中的按钮?

Disable button in a Listview's row while getting response of a save?

问题: 我在 Mainactivity 中有一个 Listview。列表视图的每一行都有两个按钮,分别是 SET 和 运行。 按 SET 将带您到 SET activity,如果用户单击 SET Activity 中的保存按钮,我需要在 mainactivity 中的列表视图的相应行位置禁用 SET 按钮。

目前已完成: 为此,我在 onclicklistener 上有一个刷新功能,可以用更新的值重新查询列表。如何在 Mainactivity 中不使用按键调用刷新函数,或者有其他方法吗?

Activity 主线:

 viewHolder.ButtonSET.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    String title = v.getTag().toString();

                    if (title.equals("SET")) {

                        if (Integer.parseInt((String) viewHolder.TDNQTY.getText()) > 0) {

                            if(scanoverornot(pos)<=0) {

                                Intent s = new Intent(DN.this, SETActivity.class);
                                s.putExtra("position", pos);
                                s.putExtra("mode", "SET");

                                try{

                                    startActivityForResult(s, saverequestcode);
                                    // getContext().startActivity(s);

                                 }
                                catch(Exception e){
                                    Toast.makeText(getContext(),""+e,Toast.LENGTH_LONG).show();
                                }
                           }

                        }
                    }
                }

            });
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (data == null)
            return;

        switch (requestCode) {

            case saverequestcode:

                if (resultCode == RESULT_OK) {

                    String SItem= data.getStringExtra("SItem");
                    int SPos= data.getIntExtra("SPos", 0);
                    saved = 700;
                    Toast.makeText(getApplicationContext(), ""+ SItem+ SPos, Toast.LENGTH_LONG).show();
                    //btnvalidate.performClick();
                }
       }
                   
}

Activity 设置:

  Intent sav = new Intent();
  sav.putExtra("SItem", String.valueOf(itemno));
  sav.putExtra("SPos", String.valueOf(pos));
  setResult(RESULT_OK, sav);
  finish();

您可以使用 startActivityForResult 来达到这个目的:

startActivityForResult(new Intent(this, DestinationActivity.class), MY_RESULT);

然后在你的 MainActivity:

public int MY_RESULT = 10;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_RESULT) {
        if (resultCode == Activity.RESULT_OK) {
            //refresh the list according to your logic
        } 
    }
}

当用户点击保存按钮时不要忘记调用 setResult(Activity.RESULT_OK);

saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setResult(Activity.RESULT_OK);
        }
    });

问题行:

  1. 还要加上super.onActivityResult(requestCode, resultCode, data);

  2. 删除了 switch 大小写并使用 if 条件进行 requestCode 检查

解法:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
     
        if (data == null)
            return;

        if (requestCode == saverequestcode) {

            if (resultCode == Activity.RESULT_OK) {

                String SItem = data.getStringExtra("SItem");
                String SPos = data.getStringExtra("SPos");
                
                Toast.makeText(getApplicationContext(), "Item :" + SItem + "Position :" + SPos, Toast.LENGTH_LONG).show();
                         
            }
            if (resultCode == Activity.RESULT_CANCELED) {
                //Any methods
            }
        }
        else if (requestCode == importrequestcode){


        }
}

Activity 设置:

  Intent sav = new Intent();
  sav.putExtra("SItem", String.valueOf(itemno));
  sav.putExtra("SPos", String.valueOf(pos));
  setResult(Activity.RESULT_OK,sav);
  finish();