类型参数T隐藏类型T.

时间:2013-08-08 08:33:48

标签: java android

我的问题是:为什么,如果我使用LinearLayout而不是Object作为AsyncTaskTableWithinExpListTask<Params, Progress, LinearLayout>)的“结果”,Eclipse会给我带来很多错误,比如无法实例化LinearLayout 类型?
在我看来它在LinearLayout中不再识别createFormattedCell(),我无法理解为什么。
AsyncTask声明LinearLayout中有黄色下划线,Eclipse表示:类型参数LinearLayout隐藏类型LinearLayout
请有人可以向我解释一下吗?

以下是该类的代码:

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TableWithinExpListTask<Params, Progress, LinearLayout> extends AsyncTask<Params, Progress, LinearLayout> {

    private final int TABLE_BORDER = 1;
    private final int TABLE_TEXT_PADDING = 10;


    private Context context = null;
    private String str = null;
    private boolean tableHeader = false;
    private LinearLayout column = null;

    public TableWithinExpListTask(Context context, String str, boolean tableHeader, LinearLayout column) {
        this.context = context;
        this.str = str;
        this.tableHeader = tableHeader;
        this.column = column;
    }

    @Override
    protected LinearLayout doInBackground(Params... arg0) {
        return this.createFormattedCell(this.tableHeader, this.str);
    }

    @Override
    protected void onPostExecute(LinearLayout result) {
        this.column.addView(result);
    }

    private LinearLayout createFormattedCell(boolean tabHeader, String str) {
        // Layout che circonda le textView necessario per disegnare il bordo
        // delle celle
        LinearLayout container = new LinearLayout(this.context);

        container.setPadding(TABLE_BORDER, TABLE_BORDER, 0, 0);
        container.setBackgroundColor(Color.BLACK);

        TextView textView = new TextView(this.context);

        textView.setPadding(TABLE_TEXT_PADDING, TABLE_TEXT_PADDING, TABLE_TEXT_PADDING, TABLE_TEXT_PADDING);
        textView.setBackgroundColor(Color.WHITE);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        textView.setLayoutParams(params);

        if (tabHeader) {
            textView.setTypeface(Typeface.DEFAULT_BOLD);
            textView.setBackgroundColor(this.context.getResources().getColor(R.color.light_grayish_orange));
        }
        textView.setText(str);
        container.addView(textView);

        return container;
    }


}

我看到有关此问题的其他问题,但我不完全理解这种情况。

2 个答案:

答案 0 :(得分:4)

将通用参数(尖括号中的内容)附加到类的名称告诉Java您希望类的用户能够指定所涉及的类型,并且您将这些名称用作“变量名“为用户选择的类型。例如,请参阅Map<K,V>,其中KV表示Map的键和值的类型。当您将LinearLayout列为类型参数时,编译器认为您只是将其用作用户将选择的其他类的占位符,并且它不知道如何构造它。

您希望您的具体类扩展使用泛型的类,但是您知道要在其中填充的特定类型,因此您不要将类型参数放在您自己的类上,只是在您自己的类上'正在使用。例如,如果您正在编写仅将Map映射到String的自定义Integer类,则可以说public class MyMap implements Map<String, Integer>

答案 1 :(得分:2)

将整个代码更改为:

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TableWithinExpListTask extends
        AsyncTask<Void, Void, LinearLayout> {

    private final int TABLE_BORDER = 1;
    private final int TABLE_TEXT_PADDING = 10;

    private Context context = null;
    private String str = null;
    private boolean tableHeader = false;
    private LinearLayout column = null;

    public TableWithinExpListTask(Context context, String str,
            boolean tableHeader, LinearLayout column) {
        this.context = context;
        this.str = str;
        this.tableHeader = tableHeader;
        this.column = column;
    }

    @Override
    protected LinearLayout doInBackground(Void... arg0) {
        return this.createFormattedCell(this.tableHeader, this.str);
    }

    @Override
    protected void onPostExecute(LinearLayout result) {
        this.column.addView(result);
    }

    private LinearLayout createFormattedCell(boolean tabHeader, String str) {
        // Layout che circonda le textView necessario per disegnare il bordo
        // delle celle
        LinearLayout container = new LinearLayout(this.context);

        container.setPadding(TABLE_BORDER, TABLE_BORDER, 0, 0);
        container.setBackgroundColor(Color.BLACK);

        TextView textView = new TextView(this.context);

        textView.setPadding(TABLE_TEXT_PADDING, TABLE_TEXT_PADDING,
                TABLE_TEXT_PADDING, TABLE_TEXT_PADDING);
        textView.setBackgroundColor(Color.WHITE);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        textView.setLayoutParams(params);

        if (tabHeader) {
            textView.setTypeface(Typeface.DEFAULT_BOLD);
            textView.setBackgroundColor(this.context.getResources().getColor(
                    R.color.light_grayish_orange));
        }
        textView.setText(str);
        container.addView(textView);

        return container;
    }

}

我相信你也需要在Java泛型中获得一些战利品。我会开始为此阅读Oracle tutorial