如何从Android AsyncTask中访问我的XML布局?

时间:2016-05-23 14:54:45

标签: android android-asynctask

我在我的应用中使用AsyncTask。当一个方法在asynctask中完成时,我会显示一个自定义Toast(成功或失败)。以下课程效果很好:

public class SendForm extends AsyncTask <Void, Void, Boolean>{
    private String message;
    private String sender;
    private Context context;

    public SendForm(String message, String sender, Context context){
        this.message = message;
        this.sender = sender;
        this.context = context;
    }

    @Override
    protected Boolean doInBackground(Void... params){
        Boolean success = false;
        MyMessageSender myMessageSender = new MyMessageSender();
        try {
            myMessageSender.send(message, sender);
            success = true
        } catch(Exception e){
            Log.i.(SendForm.class.getName(), e.getMessage());
        }
        return success
    }

    @Override
    protected void onPostExecute(Boolean formSent){
        super.onPostExecute(formSent);

        LinearLayout layout = new LinearLayout(context);
        layout.setBackgroundResource(R.color.BLACK);
        layout.setPadding(4,4,4,4);
        LinearLayout container = new LinearLayout(context);
        container.setVerticalGravity(Gravity.CENTER_VERTICAL);
        container.setPadding(10,8,10,8);
        TextView textview = new TextView(context);
        textview.setTextColor(Color.BLACK);
        textview.setTextSize(24);
        textview.setGravity(Gravity.CENTER_VERTICAL);
        ImageView imageview = new ImageView(context);
        imageview.setPadding(0,0,10,0);

        if(formSent){
            textview.setText("Form Successfully Sent");
            imageview.setImageResource(R.drawable.checkmark);
            container.setBackgroundResource(R.color.WHITE);
        } else {
            textview.setText("Error Sending Form");
            imageview.setImageResource(R.drawable.xmark);
            container.setBackgroundResource(R.color.LIGHTPINK);
        }

        container.addView(imageview);
        container.addView(textview);

        layout.addView(container);

        Toast statusToast = new Toast(context);
        statusToast.setView(layout);
        statusToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
        statusToast.setDuration(Toast.LENGTH_LONG);
        statusToast.show();
    }
}

就像我说的,这很有效,但我想让它更容易维护。所以,我创建了两个XML布局form_success.xml

<?xml ?>
<LinearLayout andriod:id="@+id/form_success_layout">
    <ImageView />
    <TextView />
</LinearLayout>

和form_error.xml

<?xml ?>
<LinearLayout andriod:id="@+id/form_error_layout">
    <ImageView />
    <TextView />
</LinearLayout>

然后我将SendForm.java改为此

public class SendForm extends AsyncTask <Void, Void, Boolean>{
    private String message;
    private String sender;
    private Context context;

    public SendForm(String message, String sender, Context context){
        this.message = message;
        this.sender = sender;
        this.context = context;
    }

    @Override
    protected Boolean doInBackground(Void... params){
        Boolean success = false;
        MyMessageSender myMessageSender = new MyMessageSender();
        try {
            myMessageSender.send(message, sender);
            success = true
        } catch(Exception e){
            Log.i.(SendForm.class.getName(), e.getMessage());
        }
        return success
    }

    @Override
    protected void onPostExecute(Boolean formSent){
        super.onPostExecute(formSent);
        LinearLayout linearLayout = new LinearLayout(context);
        Toast statusToast = new Toast(context);

        if(formSent){
            linearLayout.findViewById(R.id.form_success_layout);
            statusToast.setView(linearLayout);
        } else {
            linearLayout.findViewById(R.id.form_error_layout);
            statusToast.setView(linearLayout);
        }

        statusToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
        statusToast.setDuration(Toast.LENGTH_LONG);
        statusToast.show();
    }
}

然而,这不起作用。我没有得到任何类型的错误,它只是悄然无效。

我需要做什么才能使用这两个外部XML文件,而不是像我现在拥有的那样构建Toast?

3 个答案:

答案 0 :(得分:1)

您需要夸大布局。请阅读LayoutInflater: https://developer.android.com/reference/android/view/LayoutInflater.html

答案 1 :(得分:0)

您可以通过在onPostExecuteMethod中扩充布局来实现此功能。

LinearLayour parentFrame =  (LinearLayour)findViewById(R.id.parentFrame );
     final LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View view;
      view = inflater.inflate(R.layout.form_success, null);
      parentFrame.addView(view);

然后以这种方式获取ImageView。

     LinearLayout linearLayout = (LinearLayout)parentFrame.getChild(0);
     ImageView img = (ImageView)linearLayout.getChild(0);
     TextView txt = (TextView)linearLayout.getChild(1);

其中父框架是您的活动的主框架,如下面的pseudoCode。

<LinearLayout 
id ="parentFrame"
width = "match_parent"
height = "wrap_content"/>        

答案 2 :(得分:0)

您还没有将任何子项添加到父LinearLayout,但您正在从中访问子视图。以下将返回null

linearLayout.findViewById(R.id.form_success_layout);

请改用以下内容:

int layout = formSent ? R.layout.form_success_layout : R.layout.form_error_layout;    
View view = LayoutInflater.from(context).inflate(layout,null,false);
statusToast.setView(view);