[英]Google-style cards (issue)
我只是一個剛開始使用教程等學習制作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;
}
}
一方面,您需要在定義fontCache Hashtable時指定類型。 由於您要通過字符串查找字樣,因此請在定義中使用以下類型:
private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();
其次,如果無法解析R.id.commandText
則它不在您的R.java資源文件中。 確保您已經在android:id
屬性設置為"@+id/commandText"
R.layout.list_item中創建了一個視圖。 如果您已經完成此操作,則可能需要清理並重建項目以強制重新創建R.java。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.