我有一个像这样填充的列表视图:
lv1 = (ListView) findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("product", "Bread");
map.put("quantity", "1");
map.put("unit", "pcs");
mylist.add(map);
map = new HashMap<String, String>();
map.put("product", "Books for mom");
map.put("quantity", "14");
map.put("unit", "pcs");
mylist.add(map);
map = new HashMap<String, String>();
map.put("product", "Mineral water");
map.put("quantity", "2");
map.put("unit", "l");
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.grlists,
new String[] {"product", "quantity", "unit"}, new int[] {R.id.CPRODUCT, R.id.CQUANTITY, R.id.CUNIT});
lv1.setAdapter(mSchedule);
这是listview(grlists.xml)的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/CPRODUCT"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingLeft="5dip"
android:background="#EBDDE2"/>
<TextView android:id="@+id/CQUANTITY"
android:layout_width="30dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:textColor="#000000"
android:background="#B4CFEC"/>
<TextView android:id="@+id/CUNIT"
android:layout_width="30dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:textColor="#000000"
android:background="#A5A1A0"/>
</LinearLayout>
这给了我nullpointerexception,因为它在主布局中查找CPRODUCT textview。
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/PopStarAutograph.ttf");
TextView tv1 = (TextView) findViewById(R.id.CPRODUCT);
tv1.setTypeface(tf);
如何以编程方式更改textviews的字体类型?
答案 0 :(得分:0)
由于Dmytro Danylyk没有添加他的解决方案作为答案我添加它来关闭这个问题。然而,在过去的几个月里,我已经掌握了这个领域,所以我在发布时添加了一些不同的答案。
首先,我们需要在列表视图中显示数组。此列表视图基于从阵列接收项目的自定义适配器构建。在这个自定义适配器中,无论我们在谈论什么类型的元素,我们都可以对列表视图的每一行中的项目做任何需要。无论是textview,imageview还是复选框等等。
让我们假设我们在列表行中有5个元素:5个textviews。填充5个数组后,我们将适配器定义为:
adapter = new ListViewCustomAdapter(Calllogs.this, arr_calllog_name, arr_calllog_phone, arr_calllog_type,arr_calllog_duration, arr_calllog_date);
这就是我们将适配器设置为listview的方式:
lv1.setAdapter(adapter);
最后,我们需要创建ListViewCustomAdapter(根据需要命名):
public class ListViewCustomAdapter extends BaseAdapter
{
public String title[];
public String description[];
ArrayList<String> arr_calllog_name = new ArrayList<String>();
ArrayList<String> arr_calllog_phone = new ArrayList<String>();
ArrayList<String> arr_calllog_type = new ArrayList<String>();
ArrayList<String> arr_calllog_date = new ArrayList<String>();
ArrayList<String> arr_calllog_duration = new ArrayList<String>();
public Activity context;
public LayoutInflater inflater;
public ListViewCustomAdapter(Activity context, ArrayList<String> arr_calllog_name, ArrayList<String> arr_calllog_phone, ArrayList<String> arr_calllog_type, ArrayList<String> arr_calllog_duration, ArrayList<String> arr_calllog_date) {
super();
this.context = context;
this.arr_calllog_type = arr_calllog_type;
this.arr_calllog_name = arr_calllog_name;
this.arr_calllog_phone = arr_calllog_phone;
this.arr_calllog_date = arr_calllog_date;
this.arr_calllog_duration = arr_calllog_duration;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arr_calllog_name.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
TextView txtViewTitle;
TextView txtViewDescription;
TextView txtDate;
TextView txtDuration;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.calllog_row, null);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.calllogquery_name);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.calllogquery_phone);
holder.txtDate = (TextView) convertView.findViewById(R.id.calllogquery_date);
holder.txtDuration = (TextView) convertView.findViewById(R.id.calllogquery_duration);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
int teljes = Integer.parseInt(arr_calllog_duration.get(position));
int h = teljes / 3600;
int mar_h = teljes - h * 3600;
int m = mar_h / 60;
int s = mar_h - m * 60;
String hh, mm, ss;
if (h < 10) { hh = "0" + Integer.toString(h); }
else { hh = Integer.toString(h); }
if (m < 10) { mm = "0" + Integer.toString(m); }
else { mm = Integer.toString(m); }
if (s < 10) { ss = "0" + Integer.toString(s); }
else { ss = Integer.toString(s); }
String dur_edited = hh + ":" + mm + ":" + ss;
holder.txtViewTitle.setText(arr_calllog_name.get(position));
holder.txtViewDescription.setText(arr_calllog_phone.get(position));
holder.txtDate.setText(arr_calllog_date.get(position));
holder.txtDuration.setText(dur_edited);
return convertView;
}
}
如您所见,此适配器有5个参数,就像我们的5个数组一样。就这样。
哦,我们还需要首先将我们的适配器定义为ListViewCustomAdapter adapter;
。
您可以在onCreate()方法之外的单独的类或活动中使用此适配器类。