我正在尝试更改listview元素的字体。它是使用自定义列表视图制作的。我在尝试设置listview文本视图的字体时遇到错误。我已经按照相同的教程 site
但仍然出错。错误是我引用了null。我尝试使用取景器时代码有错误。如果我删除代码简单光标adapter.viewfinder代码工作。我创建了一个类来调用并返回它在其他地方工作的字体。下面是设置listview适配器的函数的代码。
let names = ['James', 'james', 'bob', 'JaMeS', 'Bob'];
let uNames = Object.fromEntries(names.map(s => [s.toLowerCase(), s]));
console.log(Object.values(uNames));
这是类fontstyle的代码。
private void displayListView() {
//sqlite data helper class to receive all the data
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] { CountriesDbAdapter.KEY_CODE,
CountriesDbAdapter.KEY_NAME, CountriesDbAdapter.KEY_CONTINENT,
CountriesDbAdapter.KEY_REGION };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.code, R.id.name, R.id.continent,
R.id.region, };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.country_info,
cursor, columns, to, 0);
SimpleCursorAdapter.ViewBinder binding = new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
TextView txtHeader = (TextView) findViewById(R.id.code);
txtHeader.setTypeface(FontStyle
.MonoSocialIconsFont(coursoradapter.this));
TextView txtHeader2 = (TextView) findViewById(R.id.name);
txtHeader2.setTypeface(FontStyle
.MonoSocialIconsFont(coursoradapter.this));
TextView txtHeader4 = (TextView) findViewById(R.id.continent);
txtHeader4.setTypeface(FontStyle
.MonoSocialIconsFont(coursoradapter.this));
TextView txtHeader3 = (TextView) findViewById(R.id.region);
txtHeader3.setTypeface(FontStyle
.MonoSocialIconsFont(coursoradapter.this));
return false;
};
};
dataAdapter.setViewBinder(binding);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
eclipse logcat中的错误是这样的。
package com.asolution.mathreeroute;
import android.content.Context;
import android.graphics.Typeface;
public class FontStyle {
public static Typeface MonoSocialIconsFont(Context context) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(context.getAssets(),
"MonoSocialIconsFont.ttf");
} catch (Exception e) { e.printStackTrace(); } return tf; } }
答案 0 :(得分:0)
将findViewById
功能中的所有view.findViewById
替换为setViewValue
。
<强> EDITED 强>
在false
函数中返回true
而不是setViewValue
。
在你的setValue函数中,你必须放一个switch case。
SimpleCursorAdapter.ViewBinder binding = new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
switch (view.getId()) {
case R.id.code:
TextView txtHeader = ((TextView) view);
txtHeader.setTypeface(FontStyle
.MonoSocialIconsFont(coursoradapter.this));
break;
case R.id.name:
TextView txtHeader2 = ((TextView) view);
txtHeader2.setTypeface(FontStyle
.MonoSocialIconsFont(coursoradapter.this));
break;
case R.id.continent;
TextView txtHeader4 = ((TextView) view);
txtHeader4.setTypeface(FontStyle
.MonoSocialIconsFont(coursoradapter.this));
break;
case R.id.region
TextView txtHeader3 = ((TextView) view));
txtHeader3.setTypeface(FontStyle
.MonoSocialIconsFont(coursoradapter.this));
}
return true;
}
};
答案 1 :(得分:0)
结束以这种方式更改代码
Typeface font =Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
((TextView) view).setTypeface(font);
部分有效。数据字段已更改。但是必须处理标题