我只是一个刚刚开始学习使用教程等制作Android应用程序的新手...... 我刚发现一些代码来制作Google风格的卡片,而且我编写了代码,但由于某些原因它似乎有错误。
在我的静态中,它无法从对象转换为字体",它将其称为不匹配。
此外,它无法解决" commandText" (myListItem.descText = (TextView) convertView.findViewById(R.id.commandText);
)
可能有任何帮助吗?
非常感谢,这是代码:
ArrayListAdapter.java-
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class NowArrayAdapter extends ArrayAdapter<String> {
private Context context;
private ArrayList<String> values;
private Typeface typeface;
private static Hashtable fontCache = new Hashtable();
private LayoutInflater inflater;
public class CustomListItem {
TextView descText;
}
public NowArrayAdapter(Context context, int resource, ArrayList<String> commandsList) {
//TODO Auto-generated constructor stub
super(context, R.layout.list_item, commandsList);
this.context = context;
values = new ArrayList<String>();
values.addAll(commandsList);
typeface = getTypeface(this.context, "fonts/Roboto-Light.ttf");
inflater = LayoutInflater.from(this.context);
}
static Typeface getTypeface(Context context, String font) {
Typeface typeface = fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}
public View getView(int position, View convertView, ViewGroup parent) {
CustomListItem myListItem;
String myText = getItem(position);
if(convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
myListItem = new CustomListItem();
myListItem.descText = (TextView) convertView.findViewById(R.id.commandText);
myListItem.descText.setTypeface(typeface);
convertView.setTag(myListItem);
} else {
myListItem = (CustomListItem) convertView.getTag();
}
myListItem.descText.setText(myText);
//myListItem.descText.setTextSize(14)
return convertView;
}
}
答案 0 :(得分:1)
首先,您需要在定义fontCache Hashtable时指定类型。由于您是通过字符串查找字体,因此请在定义中使用以下类型:
private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();
其次,如果R.id.commandText
无法解析,则它不会出现在您的R.java资源文件中。确保您已在R.layout.list_item中创建了一个视图,并将android:id
属性设置为"@+id/commandText"
。如果你已经这样做了,也许你需要清理和放弃重建你的项目以强制重新创建R.java。