我的问题是:为什么,如果我使用LinearLayout而不是Object
我的AsyncTask
(TableWithinExpListTask<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;
}
}
我看到有关此的其他问题,但在这种情况下我不完全了解这种行为。
在类名上附加通用参数(尖括号中的内容)表示Java您希望类的用户能够指定所涉及的类型,并且您将这些名称用作“变量名”这些用户选择的类型。例如,请参见,Map<K,V>
其中K
和V
代表Map
的键和值的类型。当您将其LinearLayout
列为类型参数时,编译器认为您只是将其用作用户可以选择的其他某个类的占位符,并且它不知道如何构造一个。
您想让具体的类扩展使用泛型的类,但是您知道要在其中填充的特定类型,因此您不必将类型参数放在自己的类上,而只是放在要使用的类上。例如,如果您正在编写Map
仅String
将Integer
s映射到s的自定义类,那么您会说public class MyMap implements Map<String, Integer>
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句