简体   繁体   English

刷新视图?

[英]Refreshing a view?

I've got a view which uses an AsyncTask to make a dynamic table. 我有一个视图,它使用AsyncTask来创建动态表。 The only trouble is that during this I create some checkboxes, but once the user has checked them I want to disable the box. 唯一的麻烦是,在此期间,我创建了一些复选框,但是一旦用户选中了它们,我便要禁用该复选框。 But due to the dynamic way they were created I can't get the reference to set them disabled. 但是由于创建它们的动态方式,我无法获得将它们设置为禁用的参考。

So here is how I create the buttons in a loop: 所以这是我在循环中创建按钮的方式:

while(it.hasNext()){
                if(checkStatus(it.next())){
                    myCheckbox.setChecked(true);
                    myCheckbox.setEnabled(false);
                }else{
                    myCheckbox.setOnClickListener(new OnClickListener() {

                          public void onClick(View v) {
                            buyU asyncTask = new buyU();
                            asyncTask.execute("value1");
                          }
                    });

                }
}

So when the button is pressed my asyncTask runs. 因此,当按下按钮时,我的asyncTask运行。 But I can't modify the checkbox as I don't have the reference for it. 但是我无法修改该复选框,因为我没有相应的参考。 I can't pass a reference I don't think as I'm using the passing parameters to send a string with a few values. 我无法传递我不认为的引用,因为我正在使用传递参数来发送带有一些值的字符串。 So I was thinking the easiest way was to just refresh the entire page for square one again. 因此,我认为最简单的方法是再次刷新整个页面以保持正方形。

What is a good way to do this? 什么是这样做的好方法?

Or alternatively can I pass the button as an object to the asyncTask as well as the string and cast them back to their types on the other end? 或者可以将按钮作为对象传递给asyncTask以及字符串,然后将其转换回另一端的类型?

You can pass the Object , just add an instance variable and a constructor. 您可以传递Object ,只需添加实例变量和构造函数即可。 Since we are executing this from onClick() , we'll cast v to a CheckBox : 由于我们从onClick()执行此操作,因此将v转换为CheckBox

Checkbox c;

public buyU (CheckBox checkbox)
{
  c = checkbox;
}

@Override
protected void onPostExecute (Result result)
{
  c.setChecked (false);
  c.setEnabled (false);
}

Then call: 然后致电:

CheckBox check = (CheckBox) v;
buyU asyncTask = new buyU(check);
asyncTask.execute("value1");

If you're sure that the CheckBox should be disabled upon a click, you can skip the AsyncTask param passing and just set the CheckBox to disabled right in onClick() . 如果确定单击时应禁用CheckBox ,则可以跳过AsyncTask参数传递,而只需在onClick()中将CheckBox设置为Disabled。

public void onClick(View v) {
  ((CheckBox)v).setChecked (false);
  v.setEnabled (false);
  buyU asyncTask = new buyU();
  asyncTask.execute("value1");
}

I'd also be weary of that enclosing loop, make sure it's not long running if it's running in the UI Thread. 我也对这个封闭的循环感到厌倦,如果它在UI线程中运行,请确保它不会长时间运行。

As usual, Java classes start with a capital letter, buyU doesn't fit that specification. 通常,Java类以大写字母开头, buyU不符合该规范。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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